Media.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use Storage;
  6. class Media extends Model
  7. {
  8. use SoftDeletes;
  9. /**
  10. * The attributes that should be mutated to dates.
  11. *
  12. * @var array
  13. */
  14. protected $dates = ['deleted_at'];
  15. public function status()
  16. {
  17. return $this->belongsTo(Status::class);
  18. }
  19. public function profile()
  20. {
  21. return $this->belongsTo(Profile::class);
  22. }
  23. public function url()
  24. {
  25. if($this->cdn_url) {
  26. return $this->cdn_url;
  27. }
  28. if($this->remote_media && $this->remote_url) {
  29. return $this->remote_url;
  30. }
  31. return url(Storage::url($this->media_path));
  32. }
  33. public function thumbnailUrl()
  34. {
  35. if($this->thumbnail_url) {
  36. return $this->thumbnail_url;
  37. }
  38. if(!$this->remote_media && $this->thumbnail_path) {
  39. return url(Storage::url($this->thumbnail_path));
  40. }
  41. return url(Storage::url('public/no-preview.png'));
  42. }
  43. public function thumb()
  44. {
  45. return $this->thumbnailUrl();
  46. }
  47. public function mimeType()
  48. {
  49. return explode('/', $this->mime)[0];
  50. }
  51. public function activityVerb()
  52. {
  53. $verb = 'Image';
  54. switch ($this->mimeType()) {
  55. case 'audio':
  56. $verb = 'Audio';
  57. break;
  58. case 'image':
  59. $verb = 'Image';
  60. break;
  61. case 'video':
  62. $verb = 'Video';
  63. break;
  64. default:
  65. $verb = 'Document';
  66. break;
  67. }
  68. return $verb;
  69. }
  70. public function getMetadata()
  71. {
  72. return json_decode($this->metadata, true, 3);
  73. }
  74. public function getModel()
  75. {
  76. if(empty($this->metadata)) {
  77. return false;
  78. }
  79. $meta = $this->getMetadata();
  80. if($meta && isset($meta['Model'])) {
  81. return $meta['Model'];
  82. }
  83. }
  84. }