Last active
February 22, 2024 13:24
-
-
Save zoparga/33ba4100cf3399a074f90f1f4e2138ed to your computer and use it in GitHub Desktop.
Laravel create temp url for files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//AppServiceProvider.php | |
Storage::disk('local')->buildTemporaryUrlsUsing(function ($path, $expiration, $options) { | |
return URL::temporarySignedRoute( | |
'local.temp', | |
$expiration, | |
array_merge($options, ['path' => $path]) | |
); | |
}); | |
// web.php | |
Route::get('local/temp/{path}', function (string $path) { | |
if (! \request()->hasValidSignature()) { | |
abort(401); | |
} | |
return Storage::disk('local')->download($path); | |
}) | |
->where('path', '.*') | |
->name('local.temp'); | |
// ANYWHERE | |
$disk = Storage::disk('releases-local'); | |
return $disk->temporaryUrl($this->path, now()->addMinutes(15)); |
Thanks for the compliment!
To be honest, laravel documentation partially mentions (https://gist.github.com/zoparga/33ba4100cf3399a074f90f1f4e2138ed?permalink_comment_id=4927572#gistcomment-4927572) but not a full implementation.
That is what I mean. The implementation was not complete. Had to figure it out from the route to controller definition
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep, had to do exactly this. Laravel has no mention of it. Thank you