F
Filament14mo ago
harps

Download private files

I'm trying to create a scenario where only logged in users can see uploaded files, I've seen it asked here a few times but I can't make sense of the answers. I have this public static function form(Form $form): Form { return $form ->schema([ Forms\Components\Select::make('client_id') ->relationship(name: 'client', titleAttribute: 'client_name'), Forms\Components\TextInput::make('project_name') ->required() ->maxLength(100), Forms\Components\FileUpload::make('shape_file') ->preserveFilenames() // directory is record id of the project ->directory(fn ($record) => 'project-shapes/' . $record->id) ->previewable(false) ->downloadable() ->disk('private') ]); } and this in the filesystem config 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', 'throw' => false, ], 'private' => [ 'driver' => 'local', 'root' => storage_path('app/private'), 'url' => env('APP_URL').'/storage', 'visibility' => 'private', 'throw' => false, ], I have public sym linked. Everything works including event listeners for deleting but I can't download the file from the form. I would also like to have an icon link in the resource table to the file. Thanks Dan
Solution:
I resolved this by adding a route to my private file paths in web.php
use App\Http\Controllers\FileController;
Route::get('/private/project-shapes/{project_id}/{file}', [FileController::class, 'downloadFile'])->middleware(['auth'])->name('file.download');
use App\Http\Controllers\FileController;
Route::get('/private/project-shapes/{project_id}/{file}', [FileController::class, 'downloadFile'])->middleware(['auth'])->name('file.download');
...
Jump to solution
1 Reply
Solution
harps
harps14mo ago
I resolved this by adding a route to my private file paths in web.php
use App\Http\Controllers\FileController;
Route::get('/private/project-shapes/{project_id}/{file}', [FileController::class, 'downloadFile'])->middleware(['auth'])->name('file.download');
use App\Http\Controllers\FileController;
Route::get('/private/project-shapes/{project_id}/{file}', [FileController::class, 'downloadFile'])->middleware(['auth'])->name('file.download');
And then my controller
class FileController extends BaseController
{
public function downloadFile(String $project_id, $file)
{
if (Auth::id()) {
return response()->download(storage_path('app/private/project-shapes/' . $project_id . '/' . $file));
}
else{
abort(404);//not found
}

}
}
class FileController extends BaseController
{
public function downloadFile(String $project_id, $file)
{
if (Auth::id()) {
return response()->download(storage_path('app/private/project-shapes/' . $project_id . '/' . $file));
}
else{
abort(404);//not found
}

}
}
Want results from more Discord servers?
Add your server