Hey guys, I was digging in related topics, to solve this issue for the client, and in the end, I could not find the solution I wanted so I came up with a simple hack myself, which worked for me and might work for you too.
so first you need to have files in a private directory not public and they need to be accessed with the controller, and you need to have this, protecting files: strpos(url()->previous(), 'whatever URL you want'), this way files can only be accessed coming from URL you want.
this is what controller function looks like:
public function fileStorageServe($file) {
$request = Request::capture();
$token = $request->header('token');
if (strpos(url()->previous(), 'previous url you want')) {
$filePath = '/videos/' . $file;
// we check for the existing of the file
if (!\Storage::disk('local')->exists($filePath)){ // note that disk()->exists() expect a relative path, from your disk root path. so in our example we pass directly the path (/…/laravelProject/storage/app) is the default one (referenced with the helper storage_path('app')
return '404'; // we redirect to 404 page if it doesn't exist
}
//file exist let serve it
// if there is parameters [you can change the files, depending on them. ex serving different content to different regions, or to mobile and desktop …etc] // repetitive things can be handled through helpers [make helpers]
return response()->file(storage_path('app'.DIRECTORY_SEPARATOR.($filePath))); // the response()->file() will add the necessary headers in our place (no headers are needed to be provided for images (it's done automatically) expected hearder is of form => ['Content-Type' => 'image/png'];
// big note here don't use Storage::url() // it's not working correctly.
}
return 'Error';
}
and this is the route:
Route::get('/storage/videos/{filePath}', 'VideoController@fileStorageServe')
->where(['filePath' => '.*'])
->middleware('auth')
hope this helps.