diff --git a/app/Filament/Resources/TestingTempResource.php b/app/Filament/Resources/TestingTempResource.php index 32c6ed2..49c5cf4 100644 --- a/app/Filament/Resources/TestingTempResource.php +++ b/app/Filament/Resources/TestingTempResource.php @@ -30,6 +30,19 @@ class TestingTempResource extends Resource ->schema([ Forms\Components\TextInput::make('name') ->label('File Name'), + Forms\Components\Select::make('selected_file') + ->label('Select Uploaded File') + ->options(function () { + return collect(Storage::disk('local')->files('uploads')) + ->mapWithKeys(function ($file) { + return [ + $file => basename($file), // value => label + ]; + }) + ->toArray(); + }) + ->searchable() + ->reactive(), Forms\Components\FileUpload::make('attachment') ->label('Upload') // ->acceptedFileTypes(['application/pdf']) @@ -41,6 +54,7 @@ class TestingTempResource extends Resource Forms\Components\Actions::make([ Action::make('uploadNow') ->label('File Upload') + ->color('success') ->action(function ($get) { $uploadedFiles = $get('attachment'); @@ -76,41 +90,94 @@ class TestingTempResource extends Resource } }), + // Action::make('downloadAttachment') + // ->label('Download File') + // ->action(function ($get) { + + // $fileName = basename($get('selected_file')); + + // //dd($fileName); + + // // if (!$fileName) { + // // Notification::make() + // // ->title('Enter file name to download') + // // ->danger() + // // ->send(); + // // return; + // // } + + // $files = Storage::disk('local')->files('uploads'); + + // foreach ($files as $file) { + + // if (pathinfo($file, PATHINFO_FILENAME) === $fileName) { + // dd($fileName); + // Notification::make() + // ->title("File downloaded successfully") + // ->success() + // ->send(); + + // return response()->download( + // Storage::disk('local')->path($file) + // ); + // } + // } + + // // Notification::make() + // // ->title('File not found') + // // ->danger() + // // ->send(); + // }), Action::make('downloadAttachment') ->label('Download File') ->action(function ($get) { - $fileName = $get('name'); + $filePath = $get('selected_file'); // uploads/filename.pdf - if (!$fileName) { + if (!$filePath || !Storage::disk('local')->exists($filePath)) { Notification::make() - ->title('Enter file name to download') + ->title('File not found') ->danger() ->send(); + return; } - $files = Storage::disk('local')->files('uploads'); + Notification::make() + ->title('File downloaded successfully') + ->success() + ->send(); - foreach ($files as $file) { - if (pathinfo($file, PATHINFO_FILENAME) === $fileName) { + return response()->download( + Storage::disk('local')->path($filePath) + ); + }), + Action::make('deleteAttachment') + ->label('Delete File') + ->color('danger') + ->requiresConfirmation() + ->action(function ($get) { + + $filePath = $get('selected_file'); // uploads/filename.pdf + + if (!$filePath || !Storage::disk('local')->exists($filePath)) { Notification::make() - ->title("File downloaded successfully") - ->success() + ->title('File not found') + ->danger() ->send(); - return response()->download( - Storage::disk('local')->path($file) - ); + return; } - } - Notification::make() - ->title('File not found') - ->danger() - ->send(); - }), + Storage::disk('local')->delete($filePath); + + Notification::make() + ->title('File deleted successfully') + ->success() + ->send(); + }), + ]), Forms\Components\Hidden::make('created_by') ->label('Created By')