Skip to content

Commit 3773b18

Browse files
committed
register temporary url callback for local disks
1 parent ea3be46 commit 3773b18

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

routes/web.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
use Illuminate\Support\Facades\Route;
65
use Imahmood\FileStorage\Controllers\DownloadController;
76

8-
Route::get('/media/public/{mediaId}/{fileName}', DownloadController::class)
9-
->name('file-storage:public');
10-
11-
Route::get('/media/{mediaId}/{fileName}', DownloadController::class)
7+
Route::get('/assets/{disk}/{path}', DownloadController::class)
128
->middleware('signed')
9+
->where('path', '.*')
1310
->name('file-storage:private');

src/Controllers/DownloadController.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use Illuminate\Routing\Controller;
77
use Illuminate\Support\Facades\Storage;
8-
use Imahmood\FileStorage\Config\Configuration;
98
use Imahmood\FileStorage\Exceptions\FileNotFoundException;
109
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1110

@@ -14,16 +13,14 @@ class DownloadController extends Controller
1413
/**
1514
* @throws \Imahmood\FileStorage\Exceptions\FileNotFoundException
1615
*/
17-
public function __invoke(Configuration $config, int $mediaId, string $fileName): BinaryFileResponse
16+
public function __invoke(string $disk, string $path): BinaryFileResponse
1817
{
19-
$path = $mediaId.DIRECTORY_SEPARATOR.$fileName;
20-
21-
if (! Storage::disk($config->diskName)->exists($path)) {
18+
if (! Storage::disk($disk)->exists($path)) {
2219
throw new FileNotFoundException();
2320
}
2421

2522
return response()->file(
26-
Storage::disk($config->diskName)->path($path)
23+
Storage::disk($disk)->path($path)
2724
);
2825
}
2926
}

src/FileStorageServiceProvider.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Imahmood\FileStorage;
55

6+
use DateTimeInterface;
7+
use Illuminate\Support\Facades\Storage;
8+
use Illuminate\Support\Facades\URL;
69
use Illuminate\Support\ServiceProvider;
710
use Imahmood\FileStorage\Config\Configuration;
811
use Imahmood\FileStorage\Config\ConfigurationFactory;
@@ -33,5 +36,24 @@ public function boot(): void
3336
$this->publishes([
3437
__DIR__.'/../config/file-storage.php' => config_path('file-storage.php'),
3538
]);
39+
40+
$this->registerTemporaryUrlCallbacks();
41+
}
42+
43+
protected function registerTemporaryUrlCallbacks(): void
44+
{
45+
foreach (config('filesystems.disks') as $disk => $options) {
46+
if ($options['driver'] !== 'local') {
47+
continue;
48+
}
49+
50+
Storage::disk($disk)->buildTemporaryUrlsUsing(
51+
fn (string $path, DateTimeInterface $expiration) => URL::signedRoute(
52+
name: 'file-storage:private',
53+
parameters: [$disk, $path],
54+
expiration: $expiration,
55+
),
56+
);
57+
}
3658
}
3759
}

0 commit comments

Comments
 (0)