kopia lustrzana https://github.com/pixelfed/pixelfed
				
				
				
			Update ImageOptimizePipeline, add skip_optimize and MediaStorageService support
							rodzic
							
								
									4b1a0fd750
								
							
						
					
					
						commit
						234f72f3aa
					
				|  | @ -41,7 +41,7 @@ class ImageOptimize implements ShouldQueue | ||||||
|     { |     { | ||||||
|         $media = $this->media; |         $media = $this->media; | ||||||
|         $path = storage_path('app/'.$media->media_path); |         $path = storage_path('app/'.$media->media_path); | ||||||
|         if (!is_file($path)) { |         if (!is_file($path) || $media->skip_optimize) { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -45,7 +45,7 @@ class ImageResize implements ShouldQueue | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|         $path = storage_path('app/'.$media->media_path); |         $path = storage_path('app/'.$media->media_path); | ||||||
|         if (!is_file($path)) { |         if (!is_file($path) || $media->skip_optimize) { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -11,6 +11,8 @@ use Illuminate\Queue\InteractsWithQueue; | ||||||
| use Illuminate\Queue\SerializesModels; | use Illuminate\Queue\SerializesModels; | ||||||
| use ImageOptimizer; | use ImageOptimizer; | ||||||
| use Illuminate\Http\File; | use Illuminate\Http\File; | ||||||
|  | use App\Services\MediaPathService; | ||||||
|  | use App\Services\MediaStorageService; | ||||||
| 
 | 
 | ||||||
| class ImageUpdate implements ShouldQueue | class ImageUpdate implements ShouldQueue | ||||||
| { | { | ||||||
|  | @ -60,7 +62,9 @@ class ImageUpdate implements ShouldQueue | ||||||
| 
 | 
 | ||||||
|         if (in_array($media->mime, $this->protectedMimes) == true) { |         if (in_array($media->mime, $this->protectedMimes) == true) { | ||||||
|             ImageOptimizer::optimize($thumb); |             ImageOptimizer::optimize($thumb); | ||||||
|             ImageOptimizer::optimize($path); |             if(!$media->skip_optimize) { | ||||||
|  |                 ImageOptimizer::optimize($path); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (!is_file($path) || !is_file($thumb)) { |         if (!is_file($path) || !is_file($thumb)) { | ||||||
|  | @ -73,19 +77,6 @@ class ImageUpdate implements ShouldQueue | ||||||
|         $media->size = $total; |         $media->size = $total; | ||||||
|         $media->save(); |         $media->save(); | ||||||
| 
 | 
 | ||||||
|         if(config('pixelfed.cloud_storage') == true) { |         MediaStorageService::store($media); | ||||||
|             $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(); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -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
 | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Ładowanie…
	
		Reference in New Issue
	
	 Daniel Supernault
						Daniel Supernault