GroupMediaService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Services\Groups;
  3. use Cache;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Support\Facades\Redis;
  6. use Illuminate\Support\Facades\Storage;
  7. use App\Models\GroupMedia;
  8. use App\Profile;
  9. use App\Status;
  10. use League\Fractal;
  11. use League\Fractal\Serializer\ArraySerializer;
  12. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  13. use App\Services\HashidService;
  14. class GroupMediaService
  15. {
  16. const CACHE_KEY = 'groups:media:';
  17. public static function path($gid, $pid, $sid = false)
  18. {
  19. if(!$gid || !$pid) {
  20. return;
  21. }
  22. $groupHashid = HashidService::encode($gid);
  23. $monthHash = HashidService::encode(date('Y').date('n'));
  24. $pid = HashidService::encode($pid);
  25. $sid = $sid ? HashidService::encode($sid) : false;
  26. $path = $sid ?
  27. "public/g1/{$groupHashid}/{$pid}/{$monthHash}/{$sid}" :
  28. "public/g1/{$groupHashid}/{$pid}/{$monthHash}";
  29. return $path;
  30. }
  31. public static function get($statusId)
  32. {
  33. return Cache::remember(self::CACHE_KEY.$statusId, 21600, function() use($statusId) {
  34. $media = GroupMedia::whereStatusId($statusId)->orderBy('order')->get();
  35. if(!$media) {
  36. return [];
  37. }
  38. $medias = $media->map(function($media) {
  39. return [
  40. 'id' => (string) $media->id,
  41. 'type' => 'Document',
  42. 'url' => $media->url(),
  43. 'preview_url' => $media->url(),
  44. 'remote_url' => $media->url,
  45. 'description' => $media->cw_summary,
  46. 'blurhash' => $media->blurhash ?? 'U4Rfzst8?bt7ogayj[j[~pfQ9Goe%Mj[WBay'
  47. ];
  48. });
  49. return $medias->toArray();
  50. });
  51. }
  52. public static function getMastodon($id)
  53. {
  54. $media = self::get($id);
  55. if(!$media) {
  56. return [];
  57. }
  58. $medias = collect($media)
  59. ->map(function($media) {
  60. $mime = $media['mime'] ? explode('/', $media['mime']) : false;
  61. unset(
  62. $media['optimized_url'],
  63. $media['license'],
  64. $media['is_nsfw'],
  65. $media['orientation'],
  66. $media['filter_name'],
  67. $media['filter_class'],
  68. $media['mime'],
  69. $media['hls_manifest']
  70. );
  71. $media['type'] = $mime ? strtolower($mime[0]) : 'unknown';
  72. return $media;
  73. })
  74. ->filter(function($m) {
  75. return $m && isset($m['url']);
  76. })
  77. ->values();
  78. return $medias->toArray();
  79. }
  80. public static function del($statusId)
  81. {
  82. return Cache::forget(self::CACHE_KEY . $statusId);
  83. }
  84. public static function activitypub($statusId)
  85. {
  86. $status = self::get($statusId);
  87. if(!$status) {
  88. return [];
  89. }
  90. return collect($status)->map(function($s) {
  91. $license = isset($s['license']) && $s['license']['title'] ? $s['license']['title'] : null;
  92. return [
  93. 'type' => 'Document',
  94. 'mediaType' => $s['mime'],
  95. 'url' => $s['url'],
  96. 'name' => $s['description'],
  97. 'summary' => $s['description'],
  98. 'blurhash' => $s['blurhash'],
  99. 'license' => $license
  100. ];
  101. });
  102. }
  103. }