7
7
use CodeIgniter \HTTP \Exceptions \HTTPException ;
8
8
use CodeIgniter \HTTP \RedirectResponse ;
9
9
use CodeIgniter \HTTP \ResponseInterface ;
10
+ use RuntimeException ;
10
11
use Tatter \Exports \Exceptions \ExportsException ;
12
+ use Tatter \Exports \Factories \ExporterFactory ;
11
13
use Tatter \Files \Config \Files as FilesConfig ;
12
14
use Tatter \Files \Entities \File ;
13
15
use Tatter \Files \Exceptions \FilesException ;
@@ -30,7 +32,7 @@ class Files extends Controller
30
32
/**
31
33
* Helpers to load.
32
34
*/
33
- protected $ helpers = ['alerts ' , 'files ' , 'handlers ' , ' html ' , 'preferences ' , 'text ' ];
35
+ protected $ helpers = ['alerts ' , 'files ' , 'html ' , 'preferences ' , 'text ' ];
34
36
35
37
/**
36
38
* Overriding data for views.
@@ -139,7 +141,7 @@ public function user($userId = null)
139
141
if ($ userId === null ) {
140
142
// Check for list permission
141
143
if (! $ this ->model ->mayList ()) {
142
- return $ this ->failure (403 , lang ('Permits .notPermitted ' ));
144
+ return $ this ->failure (403 , lang ('Files .notPermitted ' ));
143
145
}
144
146
145
147
$ this ->setData ([
@@ -152,7 +154,7 @@ public function user($userId = null)
152
154
elseif ((int ) $ userId !== user_id ()) {
153
155
// Check for list permission
154
156
if (! $ this ->model ->mayList ()) {
155
- return $ this ->failure (403 , lang ('Permits .notPermitted ' ));
157
+ return $ this ->failure (403 , lang ('Files .notPermitted ' ));
156
158
}
157
159
158
160
$ this ->setData ([
@@ -188,7 +190,7 @@ public function new()
188
190
{
189
191
// Check for create permission
190
192
if (! $ this ->model ->mayCreate ()) {
191
- return $ this ->failure (403 , lang ('Permits .notPermitted ' ));
193
+ return $ this ->failure (403 , lang ('Files .notPermitted ' ));
192
194
}
193
195
194
196
return view ('Tatter\Files\Views\new ' );
@@ -248,7 +250,7 @@ public function delete($fileId)
248
250
return $ this ->failure (400 , lang ('Files.noFile ' ));
249
251
}
250
252
if (! $ this ->model ->mayDelete ($ file )) {
251
- return $ this ->failure (403 , lang ('Permits .notPermitted ' ));
253
+ return $ this ->failure (403 , lang ('Files .notPermitted ' ));
252
254
}
253
255
254
256
if ($ this ->model ->delete ($ fileId )) {
@@ -296,20 +298,21 @@ public function bulk(): ResponseInterface
296
298
}
297
299
298
300
// Bulk export of some kind, match the handler
299
- if (! $ handler = handlers ('Exports ' )->where (['slug ' => $ action ])->first ()) {
300
- return $ this ->failure (400 , 'No handler found for ' . $ action );
301
+ try {
302
+ $ handler = ExporterFactory::find ($ action );
303
+ } catch (RuntimeException $ e ) {
304
+ return $ this ->failure (400 , 'No export handler found for ' . $ action );
301
305
}
302
-
303
- $ export = new $ handler ();
306
+ $ exporter = new $ handler ();
304
307
305
308
foreach ($ fileIds as $ fileId ) {
306
309
if ($ file = $ this ->model ->find ($ fileId )) {
307
- $ export ->setFile ($ file ->object ->setBasename ($ file ->filename ));
310
+ $ exporter ->setFile ($ file ->object ->setBasename ($ file ->filename ));
308
311
}
309
312
}
310
313
311
314
try {
312
- $ result = $ export ->process ();
315
+ $ result = $ exporter ->process ();
313
316
} catch (ExportsException $ e ) {
314
317
return $ this ->failure (400 , $ e ->getMessage ());
315
318
}
@@ -328,7 +331,7 @@ public function upload()
328
331
{
329
332
// Check for create permission
330
333
if (! $ this ->model ->mayCreate ()) {
331
- return $ this ->failure (403 , lang ('Permits .notPermitted ' ));
334
+ return $ this ->failure (403 , lang ('Files .notPermitted ' ));
332
335
}
333
336
334
337
// Verify upload succeeded
@@ -394,7 +397,13 @@ public function upload()
394
397
$ data ['clientname ' ] ??= $ upload ->getClientName ();
395
398
396
399
// Accept the file
397
- $ file = $ this ->model ->createFromPath ($ path ?? $ upload ->getRealPath (), $ data );
400
+ try {
401
+ $ file = $ this ->model ->createFromPath ($ path ?? $ upload ->getRealPath (), $ data );
402
+ } catch (Throwable $ e ) {
403
+ log_message ('error ' , $ e ->getMessage ());
404
+
405
+ return $ this ->failure (400 , $ e ->getMessage ());
406
+ }
398
407
399
408
// Trigger the Event with the new File
400
409
Events::trigger ('upload ' , $ file );
@@ -411,22 +420,21 @@ public function upload()
411
420
/**
412
421
* Processes Export requests.
413
422
*
414
- * @param string $slug The slug to match to Exports attribute
415
423
* @param int|string $fileId
416
424
*/
417
- public function export (string $ slug , $ fileId ): ResponseInterface
425
+ public function export (string $ handlerId , $ fileId ): ResponseInterface
418
426
{
419
427
// Match the export handler
420
- $ handler = handlers ('Exports ' )->where (['slug ' => $ slug ])->first ();
421
- if (empty ($ handler )) {
422
- alert ('warning ' , 'No handler found for ' . $ slug );
428
+ try {
429
+ $ handler = ExporterFactory::find ($ handlerId );
430
+ } catch (RuntimeException $ e ) {
431
+ alert ('warning ' , 'No export handler found for ' . $ handlerId );
423
432
424
433
return redirect ()->back ();
425
434
}
426
435
427
436
// Load the file
428
- $ file = $ this ->model ->find ($ fileId );
429
- if (empty ($ file )) {
437
+ if (empty ($ fileId ) || null === $ file = $ this ->model ->find ($ fileId )) {
430
438
alert ('warning ' , lang ('Files.noFile ' ));
431
439
432
440
return redirect ()->back ();
@@ -442,21 +450,22 @@ public function export(string $slug, $fileId): ResponseInterface
442
450
443
451
// Create the record
444
452
model (ExportModel::class)->insert ([
445
- 'handler ' => $ slug ,
453
+ 'handler ' => $ handlerId ,
446
454
'file_id ' => $ file ->id ,
447
455
'user_id ' => user_id (),
448
456
]);
449
457
450
458
// Pass to the handler
451
- $ export = new $ handler ($ file ->object );
452
- $ response = $ export ->setFilename ($ file ->filename )-> process ( );
459
+ $ exporter = new $ handler ($ file ->object );
460
+ $ exporter ->setFilename ($ file ->filename );
453
461
454
- // If the handler returned a response then we're done
455
- if ($ response instanceof ResponseInterface) {
456
- return $ response ;
462
+ try {
463
+ $ result = $ exporter ->process ();
464
+ } catch (ExportsException $ e ) {
465
+ return $ this ->failure (400 , $ e ->getMessage ());
457
466
}
458
467
459
- return redirect ()-> back () ;
468
+ return $ result ;
460
469
}
461
470
462
471
/**
@@ -466,9 +475,13 @@ public function export(string $slug, $fileId): ResponseInterface
466
475
*/
467
476
public function thumbnail ($ fileId ): ResponseInterface
468
477
{
469
- $ path = ($ file = $ this ->model ->find ($ fileId )) ? $ file ->getThumbnail () : File::locateDefaultThumbnail ();
478
+ $ path = ($ file = $ this ->model ->find ($ fileId ))
479
+ ? $ file ->getThumbnail ()
480
+ : File::locateDefaultThumbnail ();
470
481
471
- return $ this ->response ->setHeader ('Content-type ' , 'image/jpeg ' )->setBody (file_get_contents ($ path ));
482
+ return $ this ->response
483
+ ->setHeader ('Content-type ' , 'image/jpeg ' )
484
+ ->setBody (file_get_contents ($ path ));
472
485
}
473
486
474
487
/**
@@ -510,9 +523,26 @@ protected function setData(array $data, bool $overwrite = false): self
510
523
*/
511
524
protected function setDefaults (): self
512
525
{
526
+ // Get bulk support and index Exporters by the extension(s) they support
527
+ $ bulks = [];
528
+ $ exporters = [];
529
+
530
+ foreach (ExporterFactory::findAll () as $ handler ) {
531
+ $ attributes = $ handler ::attributes ();
532
+
533
+ if ($ attributes ['bulk ' ]) {
534
+ $ bulks [] = $ handler ;
535
+ }
536
+
537
+ foreach ($ attributes ['extensions ' ] as $ extension ) {
538
+ $ exporters [$ extension ][] = $ attributes ;
539
+ }
540
+ }
541
+
513
542
$ this ->setData ([
514
543
'source ' => 'index ' ,
515
544
'layout ' => 'files ' ,
545
+ 'model ' => $ this ->model ,
516
546
'files ' => null ,
517
547
'selected ' => explode (', ' , $ this ->request ->getVar ('selected ' ) ?? '' ),
518
548
'userId ' => null ,
@@ -522,8 +552,8 @@ protected function setDefaults(): self
522
552
'page ' => $ this ->request ->getVar ('page ' ),
523
553
'pager ' => null ,
524
554
'access ' => $ this ->model ->mayAdmin () ? 'manage ' : 'display ' ,
525
- 'exports ' => $ this -> getExports () ,
526
- 'bulks ' => handlers ()-> where ([ ' bulk ' => 1 ])-> findAll () ,
555
+ 'exports ' => $ exporters ,
556
+ 'bulks ' => $ bulks ,
527
557
]);
528
558
529
559
// Add preferences
@@ -563,27 +593,4 @@ protected function setPreferences(): self
563
593
564
594
return $ this ;
565
595
}
566
-
567
- /**
568
- * Gets Export handlers indexed by the extension they support.
569
- *
570
- * @return array<string, array>
571
- */
572
- protected function getExports (): array
573
- {
574
- $ exports = [];
575
-
576
- foreach (handlers ('Exports ' )->findAll () as $ class ) {
577
- $ attributes = handlers ()->getAttributes ($ class );
578
-
579
- // Add the class name for easy access later
580
- $ attributes ['class ' ] = $ class ;
581
-
582
- foreach (explode (', ' , $ attributes ['extensions ' ]) as $ extension ) {
583
- $ exports [$ extension ][] = $ attributes ;
584
- }
585
- }
586
-
587
- return $ exports ;
588
- }
589
596
}
0 commit comments