pixelfed/app/Services/MediaStorageService.php

323 lines
9.2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Services;
2024-03-12 07:03:33 +00:00
use App\Jobs\AvatarPipeline\AvatarStorageCleanup;
use App\Jobs\MediaPipeline\MediaDeletePipeline;
use App\Media;
use App\Util\ActivityPub\Helpers;
2024-03-12 07:03:33 +00:00
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Http\File;
2024-03-12 07:03:33 +00:00
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
2024-03-12 07:03:33 +00:00
class MediaStorageService
{
2024-02-02 09:29:33 +00:00
public static function store(Media $media)
{
2024-03-12 07:03:33 +00:00
if ((bool) config_cache('pixelfed.cloud_storage') == true) {
2024-02-02 09:29:33 +00:00
(new self())->cloudStore($media);
}
}
public static function move(Media $media)
{
2024-03-12 07:03:33 +00:00
if ($media->remote_media) {
2024-02-02 09:29:33 +00:00
return;
}
2024-03-12 07:03:33 +00:00
if ((bool) config_cache('pixelfed.cloud_storage') == true) {
2024-02-02 09:29:33 +00:00
return (new self())->cloudMove($media);
}
2024-03-12 07:03:33 +00:00
2024-02-02 09:29:33 +00:00
}
public static function avatar($avatar, $local = false, $skipRecentCheck = false)
{
return (new self())->fetchAvatar($avatar, $local, $skipRecentCheck);
}
public static function head($url)
{
$c = new Client();
try {
$r = $c->request('HEAD', $url);
} catch (RequestException $e) {
return false;
}
2024-03-12 07:03:33 +00:00
$h = Arr::mapWithKeys($r->getHeaders(), function ($item, $key) {
return [strtolower($key) => last($item)];
});
2024-03-12 07:03:33 +00:00
if (! isset($h['content-length'], $h['content-type'])) {
return false;
}
$len = (int) $h['content-length'];
$mime = $h['content-type'];
2024-03-12 07:03:33 +00:00
if ($len < 10 || $len > ((config_cache('pixelfed.max_photo_size') * 1000))) {
2024-02-02 09:29:33 +00:00
return false;
}
return [
'length' => $len,
2024-03-12 07:03:33 +00:00
'mime' => $mime,
2024-02-02 09:29:33 +00:00
];
}
protected function cloudStore($media)
{
2024-03-12 07:03:33 +00:00
if ($media->remote_media == true) {
if (config('media.storage.remote.cloud')) {
2024-02-02 09:29:33 +00:00
(new self())->remoteToCloud($media);
}
} else {
(new self())->localToCloud($media);
}
}
protected function localToCloud($media)
{
$path = storage_path('app/'.$media->media_path);
$thumb = storage_path('app/'.$media->thumbnail_path);
$p = explode('/', $media->media_path);
$name = array_pop($p);
$pt = explode('/', $media->thumbnail_path);
$thumbname = array_pop($pt);
$storagePath = implode('/', $p);
$url = ResilientMediaStorageService::store($storagePath, $path, $name);
2024-03-12 07:03:33 +00:00
if ($thumb) {
2024-02-02 09:29:33 +00:00
$thumbUrl = ResilientMediaStorageService::store($storagePath, $thumb, $thumbname);
$media->thumbnail_url = $thumbUrl;
}
$media->cdn_url = $url;
$media->optimized_url = $url;
$media->replicated_at = now();
$media->save();
2024-03-12 07:03:33 +00:00
if ($media->status_id) {
Cache::forget('status:transformer:media:attachments:'.$media->status_id);
2024-02-02 09:29:33 +00:00
MediaService::del($media->status_id);
StatusService::del($media->status_id, false);
}
}
protected function remoteToCloud($media)
{
$url = $media->remote_url;
2024-03-12 07:03:33 +00:00
if (! Helpers::validateUrl($url)) {
2024-02-02 09:29:33 +00:00
return;
}
$head = $this->head($media->remote_url);
2024-03-12 07:03:33 +00:00
if (! $head) {
2024-02-02 09:29:33 +00:00
return;
}
$mimes = [
'image/jpeg',
'image/png',
2024-03-12 07:03:33 +00:00
'video/mp4',
2024-02-02 09:29:33 +00:00
];
$mime = $head['mime'];
$max_size = (int) config_cache('pixelfed.max_photo_size') * 1000;
$media->size = $head['length'];
$media->remote_media = true;
$media->save();
2024-03-12 07:03:33 +00:00
if (! in_array($mime, $mimes)) {
2024-02-02 09:29:33 +00:00
return;
}
2024-03-12 07:03:33 +00:00
if ($head['length'] >= $max_size) {
2024-02-02 09:29:33 +00:00
return;
}
switch ($mime) {
case 'image/png':
$ext = '.png';
break;
case 'image/gif':
$ext = '.gif';
break;
case 'image/jpeg':
$ext = '.jpg';
break;
case 'video/mp4':
$ext = '.mp4';
break;
}
$base = MediaPathService::get($media->profile);
2024-03-12 07:03:33 +00:00
$path = Str::random(40).$ext;
2024-02-02 09:29:33 +00:00
$tmpBase = storage_path('app/remcache/');
2024-03-12 07:03:33 +00:00
$tmpPath = $media->profile_id.'-'.$path;
$tmpName = $tmpBase.$tmpPath;
2024-02-02 09:29:33 +00:00
$data = file_get_contents($url, false, null, 0, $head['length']);
file_put_contents($tmpName, $data);
$hash = hash_file('sha256', $tmpName);
$disk = Storage::disk(config('filesystems.cloud'));
$file = $disk->putFileAs($base, new File($tmpName), $path, 'public');
$permalink = $disk->url($file);
$media->media_path = $file;
$media->cdn_url = $permalink;
$media->original_sha256 = $hash;
$media->replicated_at = now();
$media->save();
2024-03-12 07:03:33 +00:00
if ($media->status_id) {
Cache::forget('status:transformer:media:attachments:'.$media->status_id);
2024-02-02 09:29:33 +00:00
}
unlink($tmpName);
}
protected function fetchAvatar($avatar, $local = false, $skipRecentCheck = false)
{
$queue = random_int(1, 15) > 5 ? 'mmo' : 'low';
$url = $avatar->remote_url;
$driver = $local ? 'local' : config('filesystems.cloud');
2024-03-12 07:03:33 +00:00
if (empty($url) || Helpers::validateUrl($url) == false) {
2024-02-02 09:29:33 +00:00
return;
}
$head = $this->head($url);
2024-03-12 07:03:33 +00:00
if ($head == false) {
2024-02-02 09:29:33 +00:00
return;
}
$mimes = [
'application/octet-stream',
'image/jpeg',
'image/png',
];
$mime = $head['mime'];
$max_size = (int) config('pixelfed.max_avatar_size') * 1000;
2024-03-12 07:03:33 +00:00
if (! $skipRecentCheck) {
if ($avatar->last_fetched_at && $avatar->last_fetched_at->gt(now()->subMonths(3))) {
2024-02-02 09:29:33 +00:00
return;
}
}
2024-03-12 07:03:33 +00:00
Cache::forget('avatar:'.$avatar->profile_id);
2024-02-02 09:29:33 +00:00
AccountService::del($avatar->profile_id);
// handle pleroma edge case
2024-03-12 07:03:33 +00:00
if (Str::endsWith($mime, '; charset=utf-8')) {
2024-02-02 09:29:33 +00:00
$mime = str_replace('; charset=utf-8', '', $mime);
}
2024-03-12 07:03:33 +00:00
if (! in_array($mime, $mimes)) {
2024-02-02 09:29:33 +00:00
return;
}
2024-03-12 07:03:33 +00:00
if ($head['length'] >= $max_size) {
2024-02-02 09:29:33 +00:00
return;
}
2024-03-12 07:03:33 +00:00
$base = ($local ? 'public/cache/' : 'cache/').'avatars/'.$avatar->profile_id;
2024-02-02 09:29:33 +00:00
$ext = $head['mime'] == 'image/jpeg' ? 'jpg' : 'png';
2024-03-12 07:03:33 +00:00
$path = 'avatar_'.strtolower(Str::random(random_int(3, 6))).'.'.$ext;
2024-02-02 09:29:33 +00:00
$tmpBase = storage_path('app/remcache/');
2024-03-12 07:03:33 +00:00
$tmpPath = 'avatar_'.$avatar->profile_id.'-'.$path;
$tmpName = $tmpBase.$tmpPath;
2024-02-02 09:29:33 +00:00
$data = @file_get_contents($url, false, null, 0, $head['length']);
2024-03-12 07:03:33 +00:00
if (! $data) {
2024-02-02 09:29:33 +00:00
return;
}
file_put_contents($tmpName, $data);
2024-03-12 07:03:33 +00:00
$mimeCheck = Storage::mimeType('remcache/'.$tmpPath);
2024-02-02 09:29:33 +00:00
2024-03-12 07:03:33 +00:00
if (! $mimeCheck || ! in_array($mimeCheck, ['image/png', 'image/jpeg'])) {
2024-02-02 09:29:33 +00:00
$avatar->last_fetched_at = now();
$avatar->save();
unlink($tmpName);
2024-03-12 07:03:33 +00:00
2024-02-02 09:29:33 +00:00
return;
}
$disk = Storage::disk($driver);
$file = $disk->putFileAs($base, new File($tmpName), $path, 'public');
$permalink = $disk->url($file);
2024-03-12 07:03:33 +00:00
$avatar->media_path = $base.'/'.$path;
2024-02-02 09:29:33 +00:00
$avatar->is_remote = true;
2024-03-12 07:03:33 +00:00
$avatar->cdn_url = $local ? config('app.url').$permalink : $permalink;
2024-02-02 09:29:33 +00:00
$avatar->size = $head['length'];
$avatar->change_count = $avatar->change_count + 1;
$avatar->last_fetched_at = now();
$avatar->save();
2024-03-12 07:03:33 +00:00
Cache::forget('avatar:'.$avatar->profile_id);
2024-02-02 09:29:33 +00:00
AccountService::del($avatar->profile_id);
AvatarStorageCleanup::dispatch($avatar)->onQueue($queue)->delay(now()->addMinutes(random_int(3, 15)));
unlink($tmpName);
}
public static function delete(Media $media, $confirm = false)
{
2024-03-12 07:03:33 +00:00
if (! $confirm) {
2024-02-02 09:29:33 +00:00
return;
}
MediaDeletePipeline::dispatch($media)->onQueue('mmo');
}
protected function cloudMove($media)
{
2024-03-12 07:03:33 +00:00
if (! Storage::exists($media->media_path)) {
2024-02-02 09:29:33 +00:00
return 'invalid file';
}
$path = storage_path('app/'.$media->media_path);
$thumb = false;
2024-03-12 07:03:33 +00:00
if ($media->thumbnail_path) {
2024-02-02 09:29:33 +00:00
$thumb = storage_path('app/'.$media->thumbnail_path);
$pt = explode('/', $media->thumbnail_path);
$thumbname = array_pop($pt);
}
$p = explode('/', $media->media_path);
$name = array_pop($p);
$storagePath = implode('/', $p);
$url = ResilientMediaStorageService::store($storagePath, $path, $name);
2024-03-12 07:03:33 +00:00
if ($thumb) {
2024-02-02 09:29:33 +00:00
$thumbUrl = ResilientMediaStorageService::store($storagePath, $thumb, $thumbname);
$media->thumbnail_url = $thumbUrl;
}
$media->cdn_url = $url;
$media->optimized_url = $url;
$media->replicated_at = now();
$media->save();
2024-03-12 07:03:33 +00:00
if ($media->status_id) {
Cache::forget('status:transformer:media:attachments:'.$media->status_id);
2024-02-02 09:29:33 +00:00
MediaService::del($media->status_id);
StatusService::del($media->status_id, false);
}
return 'success';
}
2021-05-11 02:04:13 +00:00
}