1
0

Media.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App;
  3. use App\Util\Media\License;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. use Illuminate\Support\Str;
  7. use Storage;
  8. class Media extends Model
  9. {
  10. use SoftDeletes;
  11. /**
  12. * The attributes that should be mutated to dates.
  13. *
  14. * @var array
  15. */
  16. protected $guarded = [];
  17. protected $casts = [
  18. 'srcset' => 'array',
  19. 'deleted_at' => 'datetime',
  20. 'skip_optimize' => 'boolean',
  21. 'replicated_at' => 'datetime',
  22. ];
  23. public function status()
  24. {
  25. return $this->belongsTo(Status::class);
  26. }
  27. public function profile()
  28. {
  29. return $this->belongsTo(Profile::class);
  30. }
  31. public function url()
  32. {
  33. if ($this->cdn_url) {
  34. // return Storage::disk(config('filesystems.cloud'))->url($this->media_path);
  35. return $this->cdn_url;
  36. }
  37. if ($this->remote_media && $this->remote_url) {
  38. return $this->remote_url;
  39. }
  40. return url(Storage::url($this->media_path));
  41. }
  42. public function thumbnailUrl()
  43. {
  44. if ($this->thumbnail_url) {
  45. return $this->thumbnail_url;
  46. }
  47. if (! $this->remote_media && $this->thumbnail_path) {
  48. return url(Storage::url($this->thumbnail_path));
  49. }
  50. if (! $this->thumbnail_path && $this->cdn_url) {
  51. return $this->cdn_url;
  52. }
  53. if ($this->media_path && $this->mime && in_array($this->mime, ['image/jpeg', 'image/png', 'image/jpg'])) {
  54. return $this->remote_media || Str::startsWith($this->media_path, 'http') ?
  55. $this->media_path :
  56. url(Storage::url($this->media_path));
  57. }
  58. return url(Storage::url('public/no-preview.png'));
  59. }
  60. public function thumb()
  61. {
  62. return $this->thumbnailUrl();
  63. }
  64. public function mimeType()
  65. {
  66. if (! $this->mime) {
  67. return;
  68. }
  69. return explode('/', $this->mime)[0];
  70. }
  71. public function activityVerb()
  72. {
  73. $verb = 'Document';
  74. switch ($this->mimeType()) {
  75. case 'audio':
  76. $verb = 'Audio';
  77. break;
  78. case 'image':
  79. $verb = 'Document';
  80. break;
  81. case 'video':
  82. $verb = 'Video';
  83. break;
  84. default:
  85. $verb = 'Document';
  86. break;
  87. }
  88. return $verb;
  89. }
  90. public function mediaType()
  91. {
  92. $verb = 'Document';
  93. switch ($this->mimeType()) {
  94. case 'audio':
  95. $verb = 'Audio';
  96. break;
  97. case 'image':
  98. $verb = 'Image';
  99. break;
  100. case 'video':
  101. $verb = 'Video';
  102. break;
  103. default:
  104. $verb = 'Image';
  105. break;
  106. }
  107. return $verb;
  108. }
  109. public function getMetadata()
  110. {
  111. return json_decode($this->metadata, true, 3);
  112. }
  113. public function getModel()
  114. {
  115. if (empty($this->metadata)) {
  116. return false;
  117. }
  118. $meta = $this->getMetadata();
  119. if ($meta && isset($meta['Model'])) {
  120. return $meta['Model'];
  121. }
  122. }
  123. public function getLicense()
  124. {
  125. $license = $this->license;
  126. if (! $license || strlen($license) > 2 || $license == 1) {
  127. return null;
  128. }
  129. if (! in_array($license, License::keys())) {
  130. return null;
  131. }
  132. $res = License::get()[$license];
  133. return [
  134. 'id' => $res['id'],
  135. 'title' => $res['title'],
  136. 'url' => $res['url'],
  137. ];
  138. }
  139. }