2021-01-18 04:59:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Util\ActivityPub\Helpers;
|
|
|
|
use Illuminate\Http\File;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use App\Media;
|
|
|
|
use App\Profile;
|
|
|
|
use App\User;
|
2021-01-24 20:30:31 +00:00
|
|
|
use GuzzleHttp\Client;
|
2022-12-02 06:13:44 +00:00
|
|
|
use App\Services\AccountService;
|
2021-01-26 04:54:30 +00:00
|
|
|
use App\Http\Controllers\AvatarController;
|
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2021-02-06 04:00:29 +00:00
|
|
|
use App\Jobs\MediaPipeline\MediaDeletePipeline;
|
2021-01-18 04:59:34 +00:00
|
|
|
|
|
|
|
class MediaStorageService {
|
|
|
|
|
|
|
|
public static function store(Media $media)
|
|
|
|
{
|
2021-05-19 04:45:04 +00:00
|
|
|
if(config_cache('pixelfed.cloud_storage') == true) {
|
2021-01-18 04:59:34 +00:00
|
|
|
(new self())->cloudStore($media);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-03 04:30:02 +00:00
|
|
|
public static function avatar($avatar, $local = false)
|
2021-01-26 04:54:30 +00:00
|
|
|
{
|
2022-01-03 04:30:02 +00:00
|
|
|
return (new self())->fetchAvatar($avatar, $local);
|
2021-01-26 04:54:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 20:30:31 +00:00
|
|
|
public static function head($url)
|
|
|
|
{
|
|
|
|
$c = new Client();
|
2021-01-26 04:54:30 +00:00
|
|
|
try {
|
|
|
|
$r = $c->request('HEAD', $url);
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-17 08:09:39 +00:00
|
|
|
|
2021-01-24 20:30:31 +00:00
|
|
|
$h = $r->getHeaders();
|
2021-02-17 08:09:39 +00:00
|
|
|
|
2022-01-18 00:11:16 +00:00
|
|
|
if (isset($h['content-length']) && isset($h['content-type'])) {
|
|
|
|
if(empty($h['content-length']) || empty($h['content-type'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$len = is_array($h['content-length']) ? $h['content-length'][0] : $h['content-length'];
|
|
|
|
$mime = is_array($h['content-type']) ? $h['content-type'][0] : $h['content-type'];
|
|
|
|
} else {
|
|
|
|
if (isset($h['Content-Length'], $h['Content-Type']) == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-05 00:30:05 +00:00
|
|
|
|
2022-01-18 00:11:16 +00:00
|
|
|
if(empty($h['Content-Length']) || empty($h['Content-Type']) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$len = is_array($h['Content-Length']) ? $h['Content-Length'][0] : $h['Content-Length'];
|
|
|
|
$mime = is_array($h['Content-Type']) ? $h['Content-Type'][0] : $h['Content-Type'];
|
2021-12-05 00:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if($len < 10 || $len > ((config_cache('pixelfed.max_photo_size') * 1000))) {
|
2021-02-17 08:09:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-24 20:30:31 +00:00
|
|
|
return [
|
2021-12-05 00:30:05 +00:00
|
|
|
'length' => $len,
|
|
|
|
'mime' => $mime
|
2021-01-24 20:30:31 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-01-18 04:59:34 +00:00
|
|
|
protected function cloudStore($media)
|
|
|
|
{
|
|
|
|
if($media->remote_media == true) {
|
|
|
|
(new self())->remoteToCloud($media);
|
|
|
|
} else {
|
|
|
|
(new self())->localToCloud($media);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function localToCloud($media)
|
|
|
|
{
|
|
|
|
$path = storage_path('app/'.$media->media_path);
|
2021-08-31 06:38:43 +00:00
|
|
|
$thumb = storage_path('app/'.$media->thumbnail_path);
|
2021-01-18 04:59:34 +00:00
|
|
|
|
|
|
|
$p = explode('/', $media->media_path);
|
|
|
|
$name = array_pop($p);
|
|
|
|
$pt = explode('/', $media->thumbnail_path);
|
|
|
|
$thumbname = array_pop($pt);
|
|
|
|
$storagePath = implode('/', $p);
|
2021-05-11 02:04:13 +00:00
|
|
|
|
2021-01-18 04:59:34 +00:00
|
|
|
$disk = Storage::disk(config('filesystems.cloud'));
|
|
|
|
$file = $disk->putFileAs($storagePath, new File($path), $name, 'public');
|
|
|
|
$url = $disk->url($file);
|
|
|
|
$thumbFile = $disk->putFileAs($storagePath, new File($thumb), $thumbname, 'public');
|
|
|
|
$thumbUrl = $disk->url($thumbFile);
|
|
|
|
$media->thumbnail_url = $thumbUrl;
|
|
|
|
$media->cdn_url = $url;
|
|
|
|
$media->optimized_url = $url;
|
2021-01-24 20:30:31 +00:00
|
|
|
$media->replicated_at = now();
|
2021-01-18 04:59:34 +00:00
|
|
|
$media->save();
|
|
|
|
if($media->status_id) {
|
|
|
|
Cache::forget('status:transformer:media:attachments:' . $media->status_id);
|
2022-12-18 06:18:07 +00:00
|
|
|
MediaService::del($media->status_id);
|
|
|
|
StatusService::del($media->status_id, false);
|
2021-01-18 04:59:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function remoteToCloud($media)
|
|
|
|
{
|
2021-01-24 20:30:31 +00:00
|
|
|
$url = $media->remote_url;
|
|
|
|
|
|
|
|
if(!Helpers::validateUrl($url)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$head = $this->head($media->remote_url);
|
2021-05-11 02:04:13 +00:00
|
|
|
|
2021-01-26 04:54:30 +00:00
|
|
|
if(!$head) {
|
|
|
|
return;
|
|
|
|
}
|
2021-05-11 02:04:13 +00:00
|
|
|
|
2021-01-24 20:30:31 +00:00
|
|
|
$mimes = [
|
|
|
|
'image/jpeg',
|
|
|
|
'image/png',
|
|
|
|
'video/mp4'
|
|
|
|
];
|
|
|
|
|
|
|
|
$mime = $head['mime'];
|
2021-05-11 02:04:13 +00:00
|
|
|
$max_size = (int) config_cache('pixelfed.max_photo_size') * 1000;
|
2021-01-24 20:30:31 +00:00
|
|
|
$media->size = $head['length'];
|
|
|
|
$media->remote_media = true;
|
|
|
|
$media->save();
|
|
|
|
|
|
|
|
if(!in_array($mime, $mimes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-26 04:54:30 +00:00
|
|
|
if($head['length'] >= $max_size) {
|
2021-01-24 20:30:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-25 04:07:28 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-01-24 20:30:31 +00:00
|
|
|
|
|
|
|
$base = MediaPathService::get($media->profile);
|
|
|
|
$path = Str::random(40) . $ext;
|
|
|
|
$tmpBase = storage_path('app/remcache/');
|
|
|
|
$tmpPath = $media->profile_id . '-' . $path;
|
|
|
|
$tmpName = $tmpBase . $tmpPath;
|
|
|
|
$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);
|
2021-01-26 04:54:30 +00:00
|
|
|
|
|
|
|
$media->media_path = $base . $path;
|
2021-01-24 20:30:31 +00:00
|
|
|
$media->cdn_url = $permalink;
|
|
|
|
$media->original_sha256 = $hash;
|
|
|
|
$media->replicated_at = now();
|
|
|
|
$media->save();
|
|
|
|
|
2021-01-25 04:07:28 +00:00
|
|
|
if($media->status_id) {
|
|
|
|
Cache::forget('status:transformer:media:attachments:' . $media->status_id);
|
|
|
|
}
|
2021-01-26 04:54:30 +00:00
|
|
|
|
|
|
|
unlink($tmpName);
|
|
|
|
}
|
|
|
|
|
2022-01-03 04:30:02 +00:00
|
|
|
protected function fetchAvatar($avatar, $local = false)
|
2021-01-26 04:54:30 +00:00
|
|
|
{
|
|
|
|
$url = $avatar->remote_url;
|
2022-01-03 04:30:02 +00:00
|
|
|
$driver = $local ? 'local' : config('filesystems.cloud');
|
2021-01-26 04:54:30 +00:00
|
|
|
|
2022-01-03 04:30:02 +00:00
|
|
|
if(empty($url) || Helpers::validateUrl($url) == false) {
|
2021-01-26 04:54:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$head = $this->head($url);
|
|
|
|
|
|
|
|
if($head == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mimes = [
|
2022-12-13 06:46:51 +00:00
|
|
|
'application/octet-stream',
|
2021-01-26 04:54:30 +00:00
|
|
|
'image/jpeg',
|
|
|
|
'image/png',
|
|
|
|
];
|
|
|
|
|
|
|
|
$mime = $head['mime'];
|
|
|
|
$max_size = (int) config('pixelfed.max_avatar_size') * 1000;
|
|
|
|
|
|
|
|
if($avatar->last_fetched_at && $avatar->last_fetched_at->gt(now()->subDay())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle pleroma edge case
|
|
|
|
if(Str::endsWith($mime, '; charset=utf-8')) {
|
|
|
|
$mime = str_replace('; charset=utf-8', '', $mime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!in_array($mime, $mimes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($head['length'] >= $max_size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-03 04:30:02 +00:00
|
|
|
$base = ($local ? 'public/cache/' : 'cache/') . 'avatars/' . $avatar->profile_id;
|
2021-01-26 04:54:30 +00:00
|
|
|
$ext = $head['mime'] == 'image/jpeg' ? 'jpg' : 'png';
|
2022-12-02 09:45:35 +00:00
|
|
|
$path = 'avatar_' . strtolower(Str::random(random_int(3,6))) . '.' . $ext;
|
2021-01-26 04:54:30 +00:00
|
|
|
$tmpBase = storage_path('app/remcache/');
|
|
|
|
$tmpPath = 'avatar_' . $avatar->profile_id . '-' . $path;
|
|
|
|
$tmpName = $tmpBase . $tmpPath;
|
2022-10-07 12:16:54 +00:00
|
|
|
$data = @file_get_contents($url, false, null, 0, $head['length']);
|
|
|
|
if(!$data) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-26 04:54:30 +00:00
|
|
|
file_put_contents($tmpName, $data);
|
|
|
|
|
2022-12-13 06:46:51 +00:00
|
|
|
$mimeCheck = Storage::mimeType('remcache/' . $tmpPath);
|
|
|
|
|
|
|
|
if(!$mimeCheck || !in_array($mimeCheck, ['image/png', 'image/jpeg'])) {
|
|
|
|
$avatar->last_fetched_at = now();
|
|
|
|
$avatar->save();
|
|
|
|
unlink($tmpName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-03 04:30:02 +00:00
|
|
|
$disk = Storage::disk($driver);
|
2021-01-26 04:54:30 +00:00
|
|
|
$file = $disk->putFileAs($base, new File($tmpName), $path, 'public');
|
|
|
|
$permalink = $disk->url($file);
|
|
|
|
|
2022-11-18 09:26:18 +00:00
|
|
|
$avatar->media_path = $base . '/' . $path;
|
2021-01-26 04:54:30 +00:00
|
|
|
$avatar->is_remote = true;
|
2022-10-09 04:59:33 +00:00
|
|
|
$avatar->cdn_url = $local ? config('app.url') . $permalink : $permalink;
|
2021-01-26 04:54:30 +00:00
|
|
|
$avatar->size = $head['length'];
|
|
|
|
$avatar->change_count = $avatar->change_count + 1;
|
|
|
|
$avatar->last_fetched_at = now();
|
|
|
|
$avatar->save();
|
|
|
|
|
|
|
|
Cache::forget('avatar:' . $avatar->profile_id);
|
2022-12-02 06:13:44 +00:00
|
|
|
Cache::forget(AccountService::CACHE_KEY . $avatar->profile_id);
|
2021-01-26 04:54:30 +00:00
|
|
|
|
2021-01-24 20:30:31 +00:00
|
|
|
unlink($tmpName);
|
2021-01-18 04:59:34 +00:00
|
|
|
}
|
2021-02-06 04:00:29 +00:00
|
|
|
|
|
|
|
public static function delete(Media $media, $confirm = false)
|
|
|
|
{
|
|
|
|
if(!$confirm) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-24 11:28:52 +00:00
|
|
|
MediaDeletePipeline::dispatch($media)->onQueue('mmo');
|
2021-02-06 04:00:29 +00:00
|
|
|
}
|
2021-05-11 02:04:13 +00:00
|
|
|
}
|