Skip to content

Commit

Permalink
register temporary url callback for local disks
Browse files Browse the repository at this point in the history
  • Loading branch information
imahmood committed Dec 31, 2023
1 parent 5fe6ef2 commit 6134e93
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
7 changes: 2 additions & 5 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;
use Imahmood\FileStorage\Controllers\DownloadController;

Route::get('/media/public/{mediaId}/{fileName}', DownloadController::class)
->name('file-storage:public');

Route::get('/media/{mediaId}/{fileName}', DownloadController::class)
Route::get('/assets/{disk}/{path}', DownloadController::class)
->middleware('signed')
->where('path', '.*')
->name('file-storage:private');
9 changes: 3 additions & 6 deletions src/Controllers/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Storage;
use Imahmood\FileStorage\Config\Configuration;
use Imahmood\FileStorage\Exceptions\FileNotFoundException;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

Expand All @@ -14,16 +13,14 @@ class DownloadController extends Controller
/**
* @throws \Imahmood\FileStorage\Exceptions\FileNotFoundException
*/
public function __invoke(Configuration $config, int $mediaId, string $fileName): BinaryFileResponse
public function __invoke(string $disk, string $path): BinaryFileResponse
{
$path = $mediaId.DIRECTORY_SEPARATOR.$fileName;

if (! Storage::disk($config->diskName)->exists($path)) {
if (! Storage::disk($disk)->exists($path)) {
throw new FileNotFoundException();
}

return response()->file(
Storage::disk($config->diskName)->path($path)
Storage::disk($disk)->path($path)
);
}
}
22 changes: 22 additions & 0 deletions src/FileStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Imahmood\FileStorage;

use DateTimeInterface;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Imahmood\FileStorage\Config\Configuration;
use Imahmood\FileStorage\Config\ConfigurationFactory;
Expand Down Expand Up @@ -33,5 +36,24 @@ public function boot(): void
$this->publishes([
__DIR__.'/../config/file-storage.php' => config_path('file-storage.php'),
]);

$this->registerTemporaryUrlCallbacks();
}

protected function registerTemporaryUrlCallbacks(): void
{
foreach (config('filesystems.disks') as $disk => $options) {
if ($options['driver'] !== 'local') {
continue;
}

Storage::disk($disk)->buildTemporaryUrlsUsing(
fn (string $path, DateTimeInterface $expiration) => URL::signedRoute(
name: 'file-storage:private',
parameters: [$disk, $path],
expiration: $expiration,
),
);
}
}
}

0 comments on commit 6134e93

Please sign in to comment.