diff --git a/app/Filament/Resources/BucketResource.php b/app/Filament/Resources/BucketResource.php index afcbada..77bcf63 100644 --- a/app/Filament/Resources/BucketResource.php +++ b/app/Filament/Resources/BucketResource.php @@ -52,13 +52,19 @@ public static function table(Table $table): Table default => 'danger', }), Tables\Columns\TextColumn::make('created_at') + ->dateTime() ->searchable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('updated_at') + ->dateTime() ->searchable() ->toggleable(isToggledHiddenByDefault: true), ]) ->actions([ + Tables\Actions\Action::make('files') + ->label('Files') + ->icon('heroicon-o-folder-open') + ->url(fn (Bucket $record): string => route('filament.admin.resources.buckets.files', $record)), Tables\Actions\EditAction::make(), ]) ->defaultSort('id', 'desc'); @@ -77,6 +83,7 @@ public static function getPages(): array 'index' => Pages\ListBuckets::route('/'), 'create' => Pages\CreateBucket::route('/create'), 'edit' => Pages\EditBucket::route('/{record}/edit'), + 'files' => Pages\Files::route('/{record}/files'), ]; } } diff --git a/app/Filament/Resources/BucketResource/Pages/Files.php b/app/Filament/Resources/BucketResource/Pages/Files.php new file mode 100644 index 0000000..6c638de --- /dev/null +++ b/app/Filament/Resources/BucketResource/Pages/Files.php @@ -0,0 +1,93 @@ +record = $this->resolveRecord($record); + $this->path = (string) request()->input('path', ''); + + if (Str::of($this->path)->contains('..')) { + abort(403); + } + + $parentPath = dirname($this->path); + if ($parentPath === '.') { + $parentPath = ''; + } + + if (! empty($this->path)) { + $this->breadcrumbs = collect(explode('/', $this->path))->map(function ($part, $index) { + return (object)[ + 'name' => $part, + 'path' => implode('/', array_slice(explode('/', $this->path), 0, $index + 1)), + ]; + })->toArray(); + } + + $directories = Storage::directories(bucket_relative_path($this->record->name.'/'.$this->path)); + + foreach ($directories as $key => $directory) { + $directories[$key] = (object) [ + 'name' => basename($directory), + 'path' => Str::of($directory)->after(bucket_relative_path($this->record->name))->trim('/')->__toString(), + ]; + } + + $this->directories = array_merge([(object) ['name' => '..', 'path' => $parentPath]], $directories); + + $files = Storage::files(bucket_relative_path($this->record->name.'/'.$this->path)); + + foreach ($files as $key => $file) { + $files[$key] = (object) [ + 'name' => basename($file), + 'path' => Str::of($file)->after(bucket_relative_path($this->record->name))->trim('/')->__toString(), + 'size' => format_filesize(Storage::size($file), 2), + 'last_modified' => Carbon::createFromTimestamp(Storage::lastModified($file))->format(Table::$defaultDateTimeDisplayFormat), + ]; + } + + $this->files = $files; + } + + protected function resolveRecord(int | string $key): Model + { + $record = static::getResource()::resolveRecordRouteBinding($key); + + if ($record === null) { + throw (new ModelNotFoundException())->setModel($this->getModel(), [$key]); + } + + return $record; + } + + public function getRecord(): Model + { + return $this->record; + } +} diff --git a/app/Helpers/functions.php b/app/Helpers/functions.php index 678066b..5ba826c 100644 --- a/app/Helpers/functions.php +++ b/app/Helpers/functions.php @@ -20,3 +20,15 @@ function bucket_path(string $bucket = 'public', ?string $filePath = null): strin return $rootPath.'/'.bucket_relative_path($bucket, $filePath); } + +function format_filesize($bytes, int $decimals = 0): ?string +{ + if (! $bytes) { + return null; + } + + $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + $factor = floor((\strlen($bytes) - 1) / 3); + + return round((float) sprintf("%.{$decimals}f", $bytes / (1024 ** $factor)), $decimals).' '.$size[$factor]; +} diff --git a/config/warehouse.php b/config/warehouse.php index 56fb034..bd29308 100644 --- a/config/warehouse.php +++ b/config/warehouse.php @@ -1,7 +1,7 @@ '0.1.0', + 'version' => '0.3.0', 'storage' => [ 'path' => env('WAREHOUSE_STORAGE_PATH', storage_path('app')), diff --git a/resources/views/filament/resources/bucket-resource/pages/files.blade.php b/resources/views/filament/resources/bucket-resource/pages/files.blade.php new file mode 100644 index 0000000..769a0f2 --- /dev/null +++ b/resources/views/filament/resources/bucket-resource/pages/files.blade.php @@ -0,0 +1,78 @@ + + +
+
+ +
+ + {{ $this->getRecord()->name }} + + + @foreach($breadcrumbs as $breadcrumb) + > + + {{ $breadcrumb->name }} + + @endforeach +
+ + + + Name + Size + Last Modified + + + @foreach($directories as $directory) + + + + + +
+ +
+ + +
+ +
+ + + @endforeach + + @foreach($files as $file) + + +
+ + {{ $file->name }} +
+ + +
+ {{ $file->size }} +
+ + +
+ {{ $file->last_modified }} +
+ + + @endforeach +
+ +
+
+ +