Media.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. class Media extends Model
  8. {
  9. use SoftDeletes;
  10. /**
  11. * The attributes that should be mutated to dates.
  12. *
  13. * @var array
  14. */
  15. protected $dates = ['deleted_at'];
  16. protected $casts = [
  17. 'srcset' => 'array'
  18. ];
  19. public function status()
  20. {
  21. return $this->belongsTo(Status::class);
  22. }
  23. public function profile()
  24. {
  25. return $this->belongsTo(Profile::class);
  26. }
  27. public function url()
  28. {
  29. if($this->cdn_url) {
  30. return $this->cdn_url;
  31. }
  32. if($this->remote_media && $this->remote_url) {
  33. return $this->remote_url;
  34. }
  35. return url(Storage::url($this->media_path));
  36. }
  37. public function thumbnailUrl()
  38. {
  39. if($this->thumbnail_url) {
  40. return $this->thumbnail_url;
  41. }
  42. if(!$this->remote_media && $this->thumbnail_path) {
  43. return url(Storage::url($this->thumbnail_path));
  44. }
  45. return url(Storage::url('public/no-preview.png'));
  46. }
  47. public function thumb()
  48. {
  49. return $this->thumbnailUrl();
  50. }
  51. public function mimeType()
  52. {
  53. return explode('/', $this->mime)[0];
  54. }
  55. public function activityVerb()
  56. {
  57. $verb = 'Image';
  58. switch ($this->mimeType()) {
  59. case 'audio':
  60. $verb = 'Audio';
  61. break;
  62. case 'image':
  63. $verb = 'Image';
  64. break;
  65. case 'video':
  66. $verb = 'Video';
  67. break;
  68. default:
  69. $verb = 'Document';
  70. break;
  71. }
  72. return $verb;
  73. }
  74. public function getMetadata()
  75. {
  76. return json_decode($this->metadata, true, 3);
  77. }
  78. public function getModel()
  79. {
  80. if(empty($this->metadata)) {
  81. return false;
  82. }
  83. $meta = $this->getMetadata();
  84. if($meta && isset($meta['Model'])) {
  85. return $meta['Model'];
  86. }
  87. }
  88. public function getLicense()
  89. {
  90. $license = $this->license;
  91. if(!$license || strlen($license) > 2 || $license == 1) {
  92. return null;
  93. }
  94. if(!in_array($license, License::keys())) {
  95. return null;
  96. }
  97. $res = License::get()[$license];
  98. return [
  99. 'id' => $res['id'],
  100. 'title' => $res['title'],
  101. 'url' => $res['url']
  102. ];
  103. }
  104. }