44 */
55namespace App \Http \Controllers \Dashboard ;
66
7+ use App \Exceptions \NotFoundException ;
78use Illuminate \Http \Request ;
89use Illuminate \Support \Facades \Event ;
910use Illuminate \Support \Str ;
1516use App \Services \CrudService ;
1617use TorMorten \Eventy \Facades \Events as Hook ;
1718use Illuminate \Support \Facades \Auth ;
19+ use Illuminate \Support \Facades \Cache ;
20+ use Illuminate \Support \Facades \Storage ;
1821use PhpOffice \PhpSpreadsheet \Spreadsheet ;
1922use PhpOffice \PhpSpreadsheet \Writer \Xlsx ;
2023use PhpOffice \PhpSpreadsheet \Writer \Csv ;
@@ -561,7 +564,13 @@ public function exportCrud( $namespace, Request $request )
561564 * let's define what will be the output name
562565 * of the exported file.
563566 */
564- $ fileName = 'export/ ' . Str::slug ( $ resource ->getLabels ()[ 'list_title ' ] ) . '.csv ' ;
567+ if ( ! is_dir ( storage_path ( 'app/public/exports ' ) ) ) {
568+ mkdir ( storage_path ( 'app/public/exports ' ) );
569+ }
570+
571+ $ dateFormat = Str::slug ( ns ()->date ->toDateTimeString () );
572+ $ relativePath = 'exports/ ' . Str::slug ( $ resource ->getLabels ()[ 'list_title ' ] ) . '- ' . $ dateFormat . '.csv ' ;
573+ $ fileName = storage_path ( 'app/public/ ' . $ relativePath );
565574
566575 /**
567576 * We'll prepare the writer
@@ -570,8 +579,15 @@ public function exportCrud( $namespace, Request $request )
570579 $ writer = new Csv ($ spreadsheet );
571580 $ writer ->save ( $ fileName );
572581
582+ /**
583+ * We'll hide the asset URL behind random lettes
584+ */
585+ $ hash = Str::random (20 );
586+
587+ Cache::put ( $ hash , $ relativePath , now ()->addMinutes (5 ) );
588+
573589 return [
574- 'url ' => asset ( $ fileName )
590+ 'url ' => route ( ' ns.dashboard.crud-download ' , compact ( ' hash ' ) )
575591 ];
576592 }
577593
@@ -609,4 +625,15 @@ public function canAccess( $namespace, Request $request )
609625 'message ' => __ ( 'This resource is not protected. The access is granted. ' )
610626 ]);
611627 }
628+
629+ public function downloadSavedFile ( $ hash )
630+ {
631+ $ relativePath = Cache::pull ( $ hash );
632+
633+ if ( Storage::disk ( 'public ' )->exists ( $ relativePath ) ) {
634+ return Storage::disk ( 'public ' )->download ( $ relativePath );
635+ }
636+
637+ throw new NotFoundException ( __ ( 'The requested file cannot be downloaded or has already been downloaded. ' ) );
638+ }
612639}
0 commit comments