From 234f72f3aa367c5f9a34c3ef77c5c2b094c148d8 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 17 Jan 2021 21:59:34 -0700 Subject: [PATCH] Update ImageOptimizePipeline, add skip_optimize and MediaStorageService support --- .../ImageOptimizePipeline/ImageOptimize.php | 2 +- .../ImageOptimizePipeline/ImageResize.php | 2 +- .../ImageOptimizePipeline/ImageUpdate.php | 21 ++---- app/Services/MediaStorageService.php | 64 +++++++++++++++++++ 4 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 app/Services/MediaStorageService.php diff --git a/app/Jobs/ImageOptimizePipeline/ImageOptimize.php b/app/Jobs/ImageOptimizePipeline/ImageOptimize.php index 0a4712bf4..6b7fd4885 100644 --- a/app/Jobs/ImageOptimizePipeline/ImageOptimize.php +++ b/app/Jobs/ImageOptimizePipeline/ImageOptimize.php @@ -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; } diff --git a/app/Jobs/ImageOptimizePipeline/ImageResize.php b/app/Jobs/ImageOptimizePipeline/ImageResize.php index 1f981ced4..15183c2e1 100644 --- a/app/Jobs/ImageOptimizePipeline/ImageResize.php +++ b/app/Jobs/ImageOptimizePipeline/ImageResize.php @@ -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; } diff --git a/app/Jobs/ImageOptimizePipeline/ImageUpdate.php b/app/Jobs/ImageOptimizePipeline/ImageUpdate.php index bcdf11c28..747820b38 100644 --- a/app/Jobs/ImageOptimizePipeline/ImageUpdate.php +++ b/app/Jobs/ImageOptimizePipeline/ImageUpdate.php @@ -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); } } diff --git a/app/Services/MediaStorageService.php b/app/Services/MediaStorageService.php new file mode 100644 index 000000000..a6a1d843f --- /dev/null +++ b/app/Services/MediaStorageService.php @@ -0,0 +1,64 @@ +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 + } +} \ No newline at end of file