Update ImageOptimizePipeline, add skip_optimize and MediaStorageService support

This commit is contained in:
Daniel Supernault 2021-01-17 21:59:34 -07:00
parent 4b1a0fd750
commit 234f72f3aa
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
4 changed files with 72 additions and 17 deletions

View File

@ -41,7 +41,7 @@ class ImageOptimize implements ShouldQueue
{
$media = $this->media;
$path = storage_path('app/'.$media->media_path);
if (!is_file($path)) {
if (!is_file($path) || $media->skip_optimize) {
return;
}

View File

@ -45,7 +45,7 @@ class ImageResize implements ShouldQueue
return;
}
$path = storage_path('app/'.$media->media_path);
if (!is_file($path)) {
if (!is_file($path) || $media->skip_optimize) {
return;
}

View File

@ -11,6 +11,8 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use ImageOptimizer;
use Illuminate\Http\File;
use App\Services\MediaPathService;
use App\Services\MediaStorageService;
class ImageUpdate implements ShouldQueue
{
@ -60,7 +62,9 @@ class ImageUpdate implements ShouldQueue
if (in_array($media->mime, $this->protectedMimes) == true) {
ImageOptimizer::optimize($thumb);
ImageOptimizer::optimize($path);
if(!$media->skip_optimize) {
ImageOptimizer::optimize($path);
}
}
if (!is_file($path) || !is_file($thumb)) {
@ -73,19 +77,6 @@ class ImageUpdate implements ShouldQueue
$media->size = $total;
$media->save();
if(config('pixelfed.cloud_storage') == true) {
$p = explode('/', $media->media_path);
$monthHash = $p[2];
$userHash = $p[3];
$storagePath = "public/m/{$monthHash}/{$userHash}";
$file = Storage::disk(config('filesystems.cloud'))->putFile($storagePath, new File($path), 'public');
$url = Storage::disk(config('filesystems.cloud'))->url($file);
$thumbFile = Storage::disk(config('filesystems.cloud'))->putFile($storagePath, new File($thumb), 'public');
$thumbUrl = Storage::disk(config('filesystems.cloud'))->url($thumbFile);
$media->thumbnail_url = $thumbUrl;
$media->cdn_url = $url;
$media->optimized_url = $url;
$media->save();
}
MediaStorageService::store($media);
}
}

View File

@ -0,0 +1,64 @@
<?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;
class MediaStorageService {
public static function store(Media $media)
{
if(config('pixelfed.cloud_storage') == true) {
(new self())->cloudStore($media);
}
return;
}
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);
$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);
$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;
$media->save();
if($media->status_id) {
Cache::forget('status:transformer:media:attachments:' . $media->status_id);
}
}
protected function remoteToCloud($media)
{
// todo
}
}