1515 */
1616class UploadController extends LfmController {
1717
18+ private $ default_file_types = ['application/pdf ' ];
19+ private $ default_image_types = ['image/jpeg ' , 'image/png ' , 'image/gif ' ];
20+
1821 /**
1922 * Upload an image/file and (for images) create thumbnail
2023 *
@@ -26,21 +29,17 @@ public function upload()
2629 try {
2730 $ res = $ this ->uploadValidator ();
2831 if (true !== $ res ) {
29- return " Invalid upload request " ;
32+ return Lang:: get ( ' laravel-filemanager::lfm.error-invalid ' ) ;
3033 }
3134 } catch (\Exception $ e ) {
3235 return $ e ->getMessage ();
3336 }
3437
35- if (!Input::hasFile ('upload ' )) {
36- return Lang::get ('laravel-filemanager::lfm.error-file-empty ' );
37- }
38-
3938 $ file = Input::file ('upload ' );
4039
4140 $ new_filename = $ this ->getNewName ($ file );
4241
43- $ dest_path = parent ::getPath ();
42+ $ dest_path = parent ::getPath (' directory ' );
4443
4544 if (File::exists ($ dest_path . $ new_filename )) {
4645 return Lang::get ('laravel-filemanager::lfm.error-file-exist ' );
@@ -64,55 +63,37 @@ private function uploadValidator()
6463 {
6564 // when uploading a file with the POST named "upload"
6665
67- $ file_array = Input::file ();
6866 $ expected_file_type = $ this ->file_type ;
6967 $ is_valid = false ;
7068
71- if (!is_array ($ file_array )) {
72- throw new \Exception ('Incorrect file_array ' );
73- }
74-
75- if (!array_key_exists ('upload ' , $ file_array )) {
76- throw new \Exception ('name: "upload" not exists ' );
77- }
78-
79- $ file = $ file_array ['upload ' ];
69+ $ file = Input::file ('upload ' );
8070 if (!$ file ) {
81- throw new \Exception (' Unexpected, nothing in "upload" ' );
71+ throw new \Exception (Lang:: get ( ' laravel-filemanager::lfm.error-file-empty ' ) );
8272 }
8373 if (!$ file instanceof UploadedFile) {
84- throw new \Exception (' The uploaded file should be an instance of UploadedFile ' );
74+ throw new \Exception (Lang:: get ( ' laravel-filemanager::lfm.error- instance' ) );
8575 }
8676
8777 $ mimetype = $ file ->getMimeType ();
8878
89- // File MimeTypes Check
90- $ valid_file_mimetypes = Config::get (
91- 'lfm.valid_file_mimetypes ' ,
92- ['application/pdf ' ]
93- );
94- if (!is_array ($ valid_file_mimetypes )) {
95- throw new \Exception ('valid_file_mimetypes is not set correctly ' );
79+ if ($ expected_file_type === 'Files ' ) {
80+ $ config_name = 'lfm.valid_file_mimetypes ' ;
81+ $ valid_mimetypes = Config::get ($ config_name , $ this ->default_file_types );
82+ } else {
83+ $ config_name = 'lfm.valid_image_mimetypes ' ;
84+ $ valid_mimetypes = Config::get ($ config_name , $ this ->default_image_types );
9685 }
9786
98- if (in_array ( $ mimetype , $ valid_file_mimetypes ) && $ expected_file_type === ' Files ' ) {
99- $ is_valid = true ;
87+ if (! is_array ( $ valid_mimetypes ) ) {
88+ throw new \ Exception ( ' Config : ' . $ config_name . ' is not set correctly ' ) ;
10089 }
10190
102- // Image MimeTypes Check
103- $ valid_image_mimetypes = Config::get (
104- 'lfm.valid_image_mimetypes ' ,
105- ['image/jpeg ' , 'image/png ' , 'image/gif ' ]
106- );
107- if (!is_array ($ valid_image_mimetypes )) {
108- throw new \Exception ('valid_image_mimetypes is not set correctly ' );
109- }
110- if (in_array ($ mimetype , $ valid_image_mimetypes )) {
91+ if (in_array ($ mimetype , $ valid_mimetypes )) {
11192 $ is_valid = true ;
11293 }
11394
11495 if (false === $ is_valid ) {
115- throw new \Exception (' Unexpected MimeType: ' . $ mimetype );
96+ throw new \Exception (Lang:: get ( ' laravel-filemanager::lfm.error-mime ' ) . $ mimetype );
11697 }
11798 return $ is_valid ;
11899 }
0 commit comments