|
@@ -2,88 +2,91 @@
|
|
|
|
|
|
namespace App\Services;
|
|
namespace App\Services;
|
|
|
|
|
|
-use Cache;
|
|
|
|
-use Illuminate\Support\Facades\Redis;
|
|
|
|
use App\Media;
|
|
use App\Media;
|
|
-use App\Status;
|
|
|
|
|
|
+use App\Transformer\Api\MediaTransformer;
|
|
|
|
+use Cache;
|
|
use League\Fractal;
|
|
use League\Fractal;
|
|
use League\Fractal\Serializer\ArraySerializer;
|
|
use League\Fractal\Serializer\ArraySerializer;
|
|
-use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
-use App\Transformer\Api\MediaTransformer;
|
|
|
|
-use App\Util\Media\License;
|
|
|
|
|
|
|
|
class MediaService
|
|
class MediaService
|
|
{
|
|
{
|
|
- const CACHE_KEY = 'status:transformer:media:attachments:';
|
|
|
|
|
|
+ const CACHE_KEY = 'status:transformer:media:attachments:';
|
|
|
|
+
|
|
|
|
+ public static function get($statusId)
|
|
|
|
+ {
|
|
|
|
+ return Cache::remember(self::CACHE_KEY.$statusId, 21600, function () use ($statusId) {
|
|
|
|
+ $media = Media::whereStatusId($statusId)->orderBy('order')->get();
|
|
|
|
+ if (! $media) {
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
+ $fractal = new Fractal\Manager;
|
|
|
|
+ $fractal->setSerializer(new ArraySerializer);
|
|
|
|
+ $resource = new Fractal\Resource\Collection($media, new MediaTransformer);
|
|
|
|
+
|
|
|
|
+ return $fractal->createData($resource)->toArray();
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static function getMastodon($id)
|
|
|
|
+ {
|
|
|
|
+ $media = self::get($id);
|
|
|
|
+ if (! $media) {
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
+ $medias = collect($media)
|
|
|
|
+ ->map(function ($media) {
|
|
|
|
+ $mime = $media['mime'] ? explode('/', $media['mime']) : false;
|
|
|
|
+ unset(
|
|
|
|
+ $media['optimized_url'],
|
|
|
|
+ $media['license'],
|
|
|
|
+ $media['is_nsfw'],
|
|
|
|
+ $media['orientation'],
|
|
|
|
+ $media['filter_name'],
|
|
|
|
+ $media['filter_class'],
|
|
|
|
+ $media['mime'],
|
|
|
|
+ $media['hls_manifest']
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ $media['type'] = $mime ? strtolower($mime[0]) : 'unknown';
|
|
|
|
|
|
- public static function get($statusId)
|
|
|
|
- {
|
|
|
|
- return Cache::remember(self::CACHE_KEY.$statusId, 21600, function() use($statusId) {
|
|
|
|
- $media = Media::whereStatusId($statusId)->orderBy('order')->get();
|
|
|
|
- if(!$media) {
|
|
|
|
- return [];
|
|
|
|
- }
|
|
|
|
- $fractal = new Fractal\Manager();
|
|
|
|
- $fractal->setSerializer(new ArraySerializer());
|
|
|
|
- $resource = new Fractal\Resource\Collection($media, new MediaTransformer());
|
|
|
|
- return $fractal->createData($resource)->toArray();
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
+ return $media;
|
|
|
|
+ })
|
|
|
|
+ ->filter(function ($m) {
|
|
|
|
+ return $m && isset($m['url']);
|
|
|
|
+ })
|
|
|
|
+ ->values();
|
|
|
|
|
|
- public static function getMastodon($id)
|
|
|
|
- {
|
|
|
|
- $media = self::get($id);
|
|
|
|
- if(!$media) {
|
|
|
|
- return [];
|
|
|
|
- }
|
|
|
|
- $medias = collect($media)
|
|
|
|
- ->map(function($media) {
|
|
|
|
- $mime = $media['mime'] ? explode('/', $media['mime']) : false;
|
|
|
|
- unset(
|
|
|
|
- $media['optimized_url'],
|
|
|
|
- $media['license'],
|
|
|
|
- $media['is_nsfw'],
|
|
|
|
- $media['orientation'],
|
|
|
|
- $media['filter_name'],
|
|
|
|
- $media['filter_class'],
|
|
|
|
- $media['mime'],
|
|
|
|
- $media['hls_manifest']
|
|
|
|
- );
|
|
|
|
|
|
+ return $medias->toArray();
|
|
|
|
+ }
|
|
|
|
|
|
- $media['type'] = $mime ? strtolower($mime[0]) : 'unknown';
|
|
|
|
- return $media;
|
|
|
|
- })
|
|
|
|
- ->filter(function($m) {
|
|
|
|
- return $m && isset($m['url']);
|
|
|
|
- })
|
|
|
|
- ->values();
|
|
|
|
|
|
+ public static function del($statusId)
|
|
|
|
+ {
|
|
|
|
+ return Cache::forget(self::CACHE_KEY.$statusId);
|
|
|
|
+ }
|
|
|
|
|
|
- return $medias->toArray();
|
|
|
|
- }
|
|
|
|
|
|
+ public static function activitypub($statusId, $fresh = false)
|
|
|
|
+ {
|
|
|
|
+ if ($fresh) {
|
|
|
|
+ self::del($statusId);
|
|
|
|
+ }
|
|
|
|
|
|
- public static function del($statusId)
|
|
|
|
- {
|
|
|
|
- return Cache::forget(self::CACHE_KEY . $statusId);
|
|
|
|
- }
|
|
|
|
|
|
+ $status = self::get($statusId);
|
|
|
|
+ if (! $status) {
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
|
|
- public static function activitypub($statusId)
|
|
|
|
- {
|
|
|
|
- $status = self::get($statusId);
|
|
|
|
- if(!$status) {
|
|
|
|
- return [];
|
|
|
|
- }
|
|
|
|
|
|
+ return collect($status)->map(function ($s) {
|
|
|
|
+ $license = isset($s['license']) && $s['license']['title'] ? $s['license']['title'] : null;
|
|
|
|
|
|
- return collect($status)->map(function($s) {
|
|
|
|
- $license = isset($s['license']) && $s['license']['title'] ? $s['license']['title'] : null;
|
|
|
|
- return [
|
|
|
|
- 'type' => 'Document',
|
|
|
|
- 'mediaType' => $s['mime'],
|
|
|
|
- 'url' => $s['url'],
|
|
|
|
- 'name' => $s['description'],
|
|
|
|
- 'summary' => $s['description'],
|
|
|
|
- 'blurhash' => $s['blurhash'],
|
|
|
|
- 'license' => $license
|
|
|
|
- ];
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
+ return [
|
|
|
|
+ 'type' => 'Document',
|
|
|
|
+ 'mediaType' => $s['mime'],
|
|
|
|
+ 'url' => $s['url'],
|
|
|
|
+ 'name' => $s['description'],
|
|
|
|
+ 'summary' => $s['description'],
|
|
|
|
+ 'blurhash' => $s['blurhash'],
|
|
|
|
+ 'license' => $license,
|
|
|
|
+ ];
|
|
|
|
+ });
|
|
|
|
+ }
|
|
}
|
|
}
|