From 4973cb46118eed5b383e26696a367ba05392641a Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 22 May 2023 18:10:55 -0600 Subject: [PATCH] Update MediaController, add fallback for local files that are later stored on S3 but still are referenced in cached objects remotely --- app/Http/Controllers/MediaController.php | 20 ++++++++++++-------- routes/web.php | 1 + 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/MediaController.php b/app/Http/Controllers/MediaController.php index 4416837cc..b10e75795 100644 --- a/app/Http/Controllers/MediaController.php +++ b/app/Http/Controllers/MediaController.php @@ -3,18 +3,10 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; -use Auth, Storage, URL; use App\Media; -use Image as Intervention; -use App\Jobs\ImageOptimizePipeline\ImageOptimize; class MediaController extends Controller { - public function __construct() - { - $this->middleware('auth'); - } - public function index(Request $request) { //return view('settings.drive.index'); @@ -24,4 +16,16 @@ class MediaController extends Controller { abort(400, 'Endpoint deprecated'); } + + public function fallbackRedirect(Request $request, $pid, $mhash, $uhash, $f) + { + abort_if(!config_cache('pixelfed.cloud_storage'), 404); + $path = 'public/m/_v2/' . $pid . '/' . $mhash . '/' . $uhash . '/' . $f; + $media = Media::whereProfileId($pid) + ->whereMediaPath($path) + ->whereNotNull('cdn_url') + ->firstOrFail(); + + return redirect()->away($media->cdn_url); + } } diff --git a/routes/web.php b/routes/web.php index 7a9352201..3c84ea808 100644 --- a/routes/web.php +++ b/routes/web.php @@ -616,6 +616,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::get('auth/invite/a/{code}', 'AdminInviteController@index'); Route::post('api/v1.1/auth/invite/admin/re', 'AdminInviteController@apiRegister')->middleware('throttle:5,1440'); + Route::get('storage/m/_v2/{pid}/{mhash}/{uhash}/{f}', 'MediaController@fallbackRedirect'); Route::get('stories/{username}', 'ProfileController@stories'); Route::get('p/{id}', 'StatusController@shortcodeRedirect'); Route::get('c/{collection}', 'CollectionController@show');