Skip to content

Commit ce30e7b

Browse files
committed
Preview private library files through an authenticated route.
1 parent 7cc3279 commit ce30e7b

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

‎src/Models/LibraryItem.php‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Tapp\FilamentLibrary\Events\LibraryItemRejected;
2222
use Tapp\FilamentLibrary\FilamentLibraryPlugin;
2323
use Tapp\FilamentLibrary\Models\Traits\BelongsToTenant;
24+
use Tapp\FilamentLibrary\Support\LibraryStorage;
2425

2526
/**
2627
* @property int $id
@@ -579,7 +580,8 @@ public function getDisplayIcon(): string
579580
*/
580581
public function registerMediaCollections(): void
581582
{
582-
// Using default collection - no need to register custom collection
583+
$this->addMediaCollection('default')
584+
->useDisk(LibraryStorage::disk());
583585
}
584586

585587
/**
@@ -601,30 +603,23 @@ public function getPreviewMedia(): ?Media
601603
}
602604

603605
/**
604-
* Get a secure URL for the file with temporary URL fallback.
606+
* URL for an authenticated inline preview.
607+
*
608+
* Private files are streamed by the application after the library policy
609+
* allows the view. A public disk URL is not used: it 404s without a
610+
* storage symlink and would be world-readable with one.
611+
*
612+
* @param int|null $expirationMinutes Kept for existing callers. The preview is session-scoped.
605613
*/
606614
public function getSecureUrl(?int $expirationMinutes = null): string
607615
{
608616
$media = $this->getPreviewMedia();
609617

610-
if (! $media) {
618+
if (! $media || ($expirationMinutes !== null && $expirationMinutes < 1)) {
611619
return '';
612620
}
613621

614-
$expirationMinutes = $expirationMinutes ?? config('filament-library.url.temporary_expiration_minutes', 60);
615-
616-
try {
617-
return $media->getTemporaryUrl(now()->addMinutes($expirationMinutes));
618-
} catch (\Exception $e) {
619-
$url = $media->getUrl();
620-
621-
// Ensure HTTPS for security
622-
if (str_starts_with($url, 'http://')) {
623-
$url = str_replace('http://', 'https://', $url);
624-
}
625-
626-
return $url;
627-
}
622+
return route('filament-library.files.show', ['media' => $media]);
628623
}
629624

630625
/**

0 commit comments

Comments
 (0)