You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.0 KiB

  1. <?php
  2. namespace App;
  3. use Movim\Model;
  4. use Respect\Validation\Validator;
  5. class MessageFile extends Model
  6. {
  7. protected $appends = ['preview', 'cleansize', 'host'];
  8. public function message()
  9. {
  10. return $this->belongsTo(Message::class);
  11. }
  12. public function getCleansizeAttribute(): ?string
  13. {
  14. if ($this->size != null) {
  15. return humanSize($this->size);
  16. }
  17. return null;
  18. }
  19. public function getPreviewAttribute(): ?array
  20. {
  21. if (typeIsPicture($this->type)
  22. ) {
  23. return [
  24. 'thumb' => protectPicture($this->url),
  25. 'url' => $this->url,
  26. 'picture' => true,
  27. 'thumbnail_type' => $this->thumbnail_type,
  28. 'thumbnail_url' => $this->thumbnail_url,
  29. ];
  30. }
  31. $url = parse_url($this->url);
  32. if (\array_key_exists('host', $url)) {
  33. $this->host = $url['host'];
  34. switch ($url['host']) {
  35. case 'i.imgur.com':
  36. $thumb = getImgurThumbnail($this->url);
  37. if ($thumb) {
  38. return [
  39. 'url' => $this->url,
  40. 'thumb' => $thumb,
  41. 'picture' => true
  42. ];
  43. }
  44. break;
  45. }
  46. }
  47. return null;
  48. }
  49. public function getHostAttribute(): string
  50. {
  51. return parse_url($this->url, PHP_URL_HOST);
  52. }
  53. public function getIsPictureAttribute(): bool
  54. {
  55. return typeIsPicture($this->type);
  56. }
  57. public function getIsAudioAttribute(): bool
  58. {
  59. return typeIsAudio($this->type);
  60. }
  61. public function getIsVideoAttribute(): bool
  62. {
  63. return typeIsVideo($this->type);
  64. }
  65. public function import($file): bool
  66. {
  67. $upload = Upload::find($file->id);
  68. if ($upload && $upload->uploaded && isMimeType($upload->type)) {
  69. $this->name = (string)$upload->name;
  70. if (isset($upload->size)) {
  71. $this->size = (int)$upload->size;
  72. }
  73. $this->type = (string)$upload->type;
  74. $this->url = $upload->geturl;
  75. if (isset($file->thumbnail)
  76. && Validator::url()->isValid($file->thumbnail->uri)) {
  77. $this->thumbnail_type = (string)$file->thumbnail->type;
  78. $this->thumbnail_width = (int)$file->thumbnail->width;
  79. $this->thumbnail_height = (int)$file->thumbnail->height;
  80. $this->thumbnail_url = (string)$file->thumbnail->uri;
  81. }
  82. if (isset($file->thumbhash)) {
  83. $this->thumbnail_type = 'image/thumbhash';
  84. $this->thumbnail_url = (string)$file->thumbhash;
  85. $this->thumbnail_width = (int)$file->thumbhashWidth;
  86. $this->thumbnail_height = (int)$file->thumbhashHeight;
  87. }
  88. return true;
  89. }
  90. return false;
  91. }
  92. }