1313 */
1414class UploadController extends LfmController
1515{
16+
17+ protected $ errors ;
18+
19+ public function __construct ()
20+ {
21+ parent ::__construct ();
22+ $ this ->errors = [];
23+ }
24+
1625 /**
17- * Upload an image/file and (for images) create thumbnail.
26+ * Upload files
1827 *
19- * @param UploadRequest $request
28+ * @param void
2029 * @return string
2130 */
2231 public function upload ()
2332 {
2433 $ files = request ()->file ('upload ' );
25- $ error_bag = [];
26- foreach (is_array ($ files ) ? $ files : [$ files ] as $ file ) {
27- $ validation_message = $ this ->uploadValidator ($ file );
28- $ new_filename = $ this ->proceedSingleUpload ($ file );
29-
30- if ($ validation_message !== 'pass ' ) {
31- array_push ($ error_bag , $ validation_message );
32- } elseif ($ new_filename == 'invalid ' ) {
33- array_push ($ error_bag , $ response );
34+
35+ // single file
36+ if (!is_array ($ files )) {
37+ $ file = $ files ;
38+ if (!$ this ->fileIsValid ($ file )) {
39+ return $ this ->errors ;
3440 }
41+
42+ if (!$ this ->proceedSingleUpload ($ file )) {
43+ return $ this ->errors ;
44+ }
45+
46+ // upload via ckeditor 'Upload' tab
47+ $ new_filename = $ this ->getNewName ($ file );
48+ return $ this ->useFile ($ new_filename );
3549 }
3650
37- if (is_array ($ files )) {
38- $ response = count ($ error_bag ) > 0 ? $ error_bag : parent ::$ success_response ;
39- } else { // upload via ckeditor 'Upload' tab
40- $ response = $ this ->useFile ($ new_filename );
51+
52+ // Multiple files
53+ foreach ($ files as $ file ) {
54+ if (!$ this ->fileIsValid ($ file )) {
55+ continue ;
56+ }
57+ $ this ->proceedSingleUpload ($ file );
4158 }
4259
43- return $ response ;
60+ return count ( $ this -> errors ) > 0 ? $ this -> errors : parent :: $ success_response ;
4461 }
4562
4663 private function proceedSingleUpload ($ file )
4764 {
48- $ validation_message = $ this ->uploadValidator ($ file );
49- if ($ validation_message !== 'pass ' ) {
50- return $ validation_message ;
51- }
52-
5365 $ new_filename = $ this ->getNewName ($ file );
5466 $ new_file_path = parent ::getCurrentPath ($ new_filename );
5567
5668 event (new ImageIsUploading ($ new_file_path ));
5769 try {
58- if (parent ::fileIsImage ($ file ) && parent ::imageShouldHaveThumb ($ file )) {
70+ if (parent ::fileIsImage ($ file )) {
71+ // Process & compress the image
5972 Image::make ($ file ->getRealPath ())
6073 ->orientate () //Apply orientation from exif data
6174 ->save ($ new_file_path , 90 );
6275
63- $ this ->makeThumb ($ new_filename );
64- }
65- {
66- chmod ($ file ->getRealPath (), config ('lfm.create_file_mode ' , 0644 ));
67- File::move ($ file ->getRealPath (), $ new_file_path );
76+ // Generate a thumbnail
77+ if (parent ::imageShouldHaveThumb ($ file )) {
78+ $ this ->makeThumb ($ new_filename );
79+ }
6880 }
81+
82+ // Create (move) the file
83+ chmod ($ file ->getRealPath (), config ('lfm.create_file_mode ' , 0644 ));
84+ File::move ($ file ->getRealPath (), $ new_file_path );
6985 } catch (\Exception $ e ) {
70- return parent ::error ('invalid ' );
86+ array_push ($ this ->errors , parent ::error ('invalid ' ));
87+ // FIXME: Exception must be logged.
88+ return false ;
7189 }
90+
91+ // TODO should be "FileWasUploaded"
7292 event (new ImageWasUploaded (realpath ($ new_file_path )));
7393
74- return $ new_filename ;
94+ return true ;
7595 }
7696
77- private function uploadValidator ($ file )
97+ private function fileIsValid ($ file )
7898 {
79- $ is_valid = false ;
80- $ force_invalid = false ;
81-
8299 if (empty ($ file )) {
83- return parent ::error ('file-empty ' );
84- } elseif (! $ file instanceof UploadedFile) {
85- return parent ::error ('instance ' );
86- } elseif ($ file ->getError () == UPLOAD_ERR_INI_SIZE ) {
100+ array_push ($ this ->errors , parent ::error ('file-empty ' ));
101+ return false ;
102+ }
103+
104+ if (! $ file instanceof UploadedFile) {
105+ array_push ($ this ->errors , parent ::error ('instance ' ));
106+ return false ;
107+ }
108+
109+ if ($ file ->getError () == UPLOAD_ERR_INI_SIZE ) {
87110 $ max_size = ini_get ('upload_max_filesize ' );
111+ array_push ($ this ->errors , parent ::error ('file-size ' , ['max ' => $ max_size ]));
112+ return false ;
113+ }
88114
89- return parent ::error ('file-size ' , ['max ' => $ max_size ]);
90- } elseif ($ file ->getError () != UPLOAD_ERR_OK ) {
91- return 'File failed to upload. Error code: ' . $ file ->getError ();
115+ if ($ file ->getError () != UPLOAD_ERR_OK ) {
116+ $ msg = 'File failed to upload. Error code: ' . $ file ->getError ();
117+ array_push ($ this ->errors , $ msg );
118+ return false ;
92119 }
93120
94121 $ new_filename = $ this ->getNewName ($ file );
95122
96123 if (File::exists (parent ::getCurrentPath ($ new_filename ))) {
97- return parent ::error ('file-exist ' );
124+ array_push ($ this ->errors , parent ::error ('file-exist ' ));
125+ return false ;
98126 }
99127
100128 $ mimetype = $ file ->getMimeType ();
101129
102- // size to kb unit is needed
103- $ file_size = $ file ->getSize () / 1000 ;
130+ // Bytes to KB
131+ $ file_size = $ file ->getSize () / 1024 ;
104132 $ type_key = parent ::currentLfmType ();
105133
106134 if (config ('lfm.should_validate_mime ' , false )) {
107135 $ mine_config = 'lfm.valid_ ' . $ type_key . '_mimetypes ' ;
108136 $ valid_mimetypes = config ($ mine_config , []);
109137 if (false === in_array ($ mimetype , $ valid_mimetypes )) {
110- return parent ::error ('mime ' ) . $ mimetype ;
138+ array_push ($ this ->errors , parent ::error ('mime ' ) . $ mimetype );
139+ return false ;
111140 }
112141 }
113142
114143 if (config ('lfm.should_validate_size ' , false )) {
115144 $ max_size = config ('lfm.max_ ' . $ type_key . '_size ' , 0 );
116145 if ($ file_size > $ max_size ) {
117- return parent ::error ('size ' ) . $ mimetype ;
146+ array_push ($ this ->errors , parent ::error ('size ' ));
147+ return false ;
118148 }
119149 }
120150
121- return ' pass ' ;
151+ return true ;
122152 }
123153
124154 protected function replaceInsecureSuffix ($ name )
@@ -128,7 +158,7 @@ protected function replaceInsecureSuffix($name)
128158
129159 private function getNewName ($ file )
130160 {
131- $ new_filename = parent ::translateFromUtf8 (trim ($ this ->_pathinfo ($ file ->getClientOriginalName (), PATHINFO_FILENAME )));
161+ $ new_filename = parent ::translateFromUtf8 (trim ($ this ->pathinfo ($ file ->getClientOriginalName (), PATHINFO_FILENAME )));
132162 if (config ('lfm.rename_file ' ) === true ) {
133163 $ new_filename = uniqid ();
134164 } elseif (config ('lfm.alphanumeric_filename ' ) === true ) {
@@ -172,7 +202,7 @@ function getUrlParam(paramName) {
172202 </script> " ;
173203 }
174204
175- private function _pathinfo ($ path , $ options = null )
205+ private function pathinfo ($ path , $ options = null )
176206 {
177207 $ path = urlencode ($ path );
178208 $ parts = is_null ($ options ) ? pathinfo ($ path ) : pathinfo ($ path , $ options );
0 commit comments