Я использую библиотеку spatie для загрузки некоторых файлов, которые будут сохранены в папке хранилища. Чего я хочу добиться на данный момент, так это просмотреть эти файлы или изображения, когда я являюсь аутентифицированным пользователем. Я попытался создать символическую ссылку с помощью этой команды,
php artisan storage:link
Но это делает эти файлы общедоступными. Я только хочу просмотреть этот файл, только когда пользователь является аутентифицированным пользователем. Пока это то, что я сделал, но кажется, что я что-то упускаю.
МАРШРУТ :
Route::get('/storage/{filePath}', 'ComplaintsController@fileStorageServe')->where(['filePath' => '.*'])->name('complaints.viewfile');
КОНТРОЛЛЕР:
public function fileStorageServe($file) {
// know you can have a mapping so you dont keep the sme names as in local (you can not precsise the same structor as the storage, you can do anything)
// any permission handling or anything else
// 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')
abort('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.
}
ПОСМОТРЕТЬ :
@foreach($complaint->attachments as $attachment)
<a href="{{url('complaints.viewfile', $attachment->getUrl())}}" target="_blank">{{ $attachment->file_name }}</a>
@endforeach
Когда я нажимаю на ссылку, она дает мне что-то вроде этого.
http://127.0.0.1:8000/complaints.viewfile/http%3A%2F%2F127.0.0.1%3A8000%2Fstorage%2F3%2F60580fdeadc55_worship.jpg
Пожалуйста помоги. Спасибо...