11<?php namespace Tatter \Files \Controllers ;
22
33use CodeIgniter \Controller ;
4+ use CodeIgniter \Events \Events ;
5+ use CodeIgniter \HTTP \Exceptions \HTTPException ;
46use CodeIgniter \HTTP \RedirectResponse ;
57use CodeIgniter \HTTP \RequestInterface ;
68use CodeIgniter \HTTP \ResponseInterface ;
@@ -409,11 +411,19 @@ public function upload()
409411 $ chunkDir = WRITEPATH . 'uploads/ ' . $ uuid ;
410412 if (! is_dir ($ chunkDir ) && ! mkdir ($ chunkDir , 0775 , true ))
411413 {
412- throw FilesException:: forChunkDirFail ( $ chunkDir );
414+ return $ this -> failure ( 400 , lang ( ' Files.chunkDirFail ' , [ $ chunkDir]) );
413415 }
414416
415417 // Move the file
416- $ upload ->move ($ chunkDir , $ chunkIndex . '. ' . $ upload ->getExtension ());
418+ try
419+ {
420+ $ upload ->move ($ chunkDir , $ chunkIndex . '. ' . $ upload ->getExtension ());
421+ }
422+ catch (HTTPException $ e )
423+ {
424+ log_message ('error ' , $ e ->getMessage ());
425+ return $ this ->failure (400 , $ e ->getMessage ());
426+ }
417427
418428 // Check for more chunks
419429 if ($ chunkIndex < $ totalChunks - 1 )
@@ -423,7 +433,15 @@ public function upload()
423433 }
424434
425435 // Merge the chunks
426- $ path = $ this ->mergeChunks ($ chunkDir );
436+ try
437+ {
438+ $ path = $ this ->mergeChunks ($ chunkDir );
439+ }
440+ catch (FilesException $ e )
441+ {
442+ log_message ('error ' , $ e ->getMessage ());
443+ return $ this ->failure (400 , $ e ->getMessage ());
444+ }
427445 }
428446
429447 // Get additional post data to pass to model
@@ -434,6 +452,9 @@ public function upload()
434452 // Accept the file
435453 $ file = $ this ->model ->createFromPath ($ path ?? $ upload ->getRealPath (), $ data );
436454
455+ // Trigger the Event with the new File
456+ Events::trigger ('upload ' , $ file );
457+
437458 if ($ this ->request ->isAJAX ())
438459 {
439460 session_write_close ();
0 commit comments