1
0
Fork 0
pixelfed/app/Services/MediaService.php

88 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
use Cache;
use Illuminate\Support\Facades\Redis;
use App\Media;
use App\Status;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Transformer\Api\MediaTransformer;
2021-07-25 09:17:49 +00:00
use App\Util\Media\License;
class MediaService
{
const CACHE_KEY = 'status:transformer:media:attachments:';
public static function get($statusId)
{
2022-01-03 09:31:03 +00:00
return Cache::remember(self::CACHE_KEY.$statusId, 86400, function() use($statusId) {
$media = Media::whereStatusId($statusId)->orderBy('order')->get();
if(!$media) {
return [];
}
2022-01-03 09:31:03 +00:00
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Collection($media, new MediaTransformer());
return $fractal->createData($resource)->toArray();
});
}
2022-01-03 09:31:03 +00:00
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['type'] = $mime ? strtolower($mime[0]) : 'unknown';
return $media;
})
->filter(function($m) {
return $m && isset($m['url']);
})
->values();
return $medias->toArray();
}
public static function del($statusId)
{
return Cache::forget(self::CACHE_KEY . $statusId);
}
2021-07-25 09:17:49 +00:00
public static function activitypub($statusId)
{
$status = self::get($statusId);
if(!$status) {
return [];
}
2021-08-31 06:38:27 +00:00
return collect($status)->map(function($s) {
2021-07-25 09:17:49 +00:00
$license = isset($s['license']) && $s['license']['title'] ? $s['license']['title'] : null;
return [
'type' => 'Document',
'mediaType' => $s['mime'],
'url' => $s['url'],
'name' => $s['description'],
'blurhash' => $s['blurhash'],
'license' => $license
];
});
}
}