02-02-2024

Add Temp URL for files

Add these 3 simple part:

Add this snippet to your AppServiceProvider.php

Storage::disk('local')->buildTemporaryUrlsUsing(function ($path, $expiration, $options) {
    return URL::temporarySignedRoute(
        'local.temp',
        $expiration,
        array_merge($options, ['path' => $path])
    );
});        

Place this part to your web.php

Route::get('local/temp/{path}', function (string $path) {
    if (! \request()->hasValidSignature()) {
        abort(401);
    }
    return Storage::disk('local')->download($path);
})
->where('path', '.*') // THIS WILL HANDLE SUBFOLDERS
->name('local.temp');

Now you can use this part anywhere:

$disk = Storage::disk('releases-local');
return $disk->temporaryUrl($this->path, now()->addMinutes(15));

Please note that small part, that I placed the route resolve path in my api.php, this is why you can see /api/ part in the picture.


Have a nice day! 

© 2024 PappZ. All rights reserved.