Media.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use App\Util\Media\License;
  6. use Storage;
  7. use Illuminate\Support\Str;
  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. ];
  21. public function status()
  22. {
  23. return $this->belongsTo(Status::class);
  24. }
  25. public function profile()
  26. {
  27. return $this->belongsTo(Profile::class);
  28. }
  29. public function url()
  30. {
  31. if($this->cdn_url) {
  32. // return Storage::disk(config('filesystems.cloud'))->url($this->media_path);
  33. return $this->cdn_url;
  34. }
  35. if($this->remote_media && $this->remote_url) {
  36. return $this->remote_url;
  37. }
  38. return url(Storage::url($this->media_path));
  39. }
  40. public function thumbnailUrl()
  41. {
  42. if($this->thumbnail_url) {
  43. return $this->thumbnail_url;
  44. }
  45. if(!$this->remote_media && $this->thumbnail_path) {
  46. return url(Storage::url($this->thumbnail_path));
  47. }
  48. if($this->remote_media && !$this->thumbnail_path && $this->cdn_url) {
  49. return $this->cdn_url;
  50. }
  51. if($this->media_path && $this->mime && in_array($this->mime, ['image/jpeg', 'image/png'])) {
  52. return $this->remote_media || Str::startsWith($this->media_path, 'http') ?
  53. $this->media_path :
  54. url(Storage::url($this->media_path));
  55. }
  56. return url(Storage::url('public/no-preview.png'));
  57. }
  58. public function thumb()
  59. {
  60. return $this->thumbnailUrl();
  61. }
  62. public function mimeType()
  63. {
  64. if(!$this->mime) {
  65. return;
  66. }
  67. return explode('/', $this->mime)[0];
  68. }
  69. public function activityVerb()
  70. {
  71. $verb = 'Image';
  72. switch ($this->mimeType()) {
  73. case 'audio':
  74. $verb = 'Audio';
  75. break;
  76. case 'image':
  77. $verb = 'Image';
  78. break;
  79. case 'video':
  80. $verb = 'Video';
  81. break;
  82. default:
  83. $verb = 'Document';
  84. break;
  85. }
  86. return $verb;
  87. }
  88. public function getMetadata()
  89. {
  90. return json_decode($this->metadata, true, 3);
  91. }
  92. public function getModel()
  93. {
  94. if(empty($this->metadata)) {
  95. return false;
  96. }
  97. $meta = $this->getMetadata();
  98. if($meta && isset($meta['Model'])) {
  99. return $meta['Model'];
  100. }
  101. }
  102. public function getLicense()
  103. {
  104. $license = $this->license;
  105. if(!$license || strlen($license) > 2 || $license == 1) {
  106. return null;
  107. }
  108. if(!in_array($license, License::keys())) {
  109. return null;
  110. }
  111. $res = License::get()[$license];
  112. return [
  113. 'id' => $res['id'],
  114. 'title' => $res['title'],
  115. 'url' => $res['url']
  116. ];
  117. }
  118. }