15
15
*/
16
16
class UploadController extends LfmController {
17
17
18
+ private $ default_file_types = ['application/pdf ' ];
19
+ private $ default_image_types = ['image/jpeg ' , 'image/png ' , 'image/gif ' ];
20
+
18
21
/**
19
22
* Upload an image/file and (for images) create thumbnail
20
23
*
@@ -26,21 +29,17 @@ public function upload()
26
29
try {
27
30
$ res = $ this ->uploadValidator ();
28
31
if (true !== $ res ) {
29
- return " Invalid upload request " ;
32
+ return Lang:: get ( ' laravel-filemanager::lfm.error-invalid ' ) ;
30
33
}
31
34
} catch (\Exception $ e ) {
32
35
return $ e ->getMessage ();
33
36
}
34
37
35
- if (!Input::hasFile ('upload ' )) {
36
- return Lang::get ('laravel-filemanager::lfm.error-file-empty ' );
37
- }
38
-
39
38
$ file = Input::file ('upload ' );
40
39
41
40
$ new_filename = $ this ->getNewName ($ file );
42
41
43
- $ dest_path = parent ::getPath ();
42
+ $ dest_path = parent ::getPath (' directory ' );
44
43
45
44
if (File::exists ($ dest_path . $ new_filename )) {
46
45
return Lang::get ('laravel-filemanager::lfm.error-file-exist ' );
@@ -64,55 +63,37 @@ private function uploadValidator()
64
63
{
65
64
// when uploading a file with the POST named "upload"
66
65
67
- $ file_array = Input::file ();
68
66
$ expected_file_type = $ this ->file_type ;
69
67
$ is_valid = false ;
70
68
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 ' );
80
70
if (!$ file ) {
81
- throw new \Exception (' Unexpected, nothing in "upload" ' );
71
+ throw new \Exception (Lang:: get ( ' laravel-filemanager::lfm.error-file-empty ' ) );
82
72
}
83
73
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' ) );
85
75
}
86
76
87
77
$ mimetype = $ file ->getMimeType ();
88
78
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 );
96
85
}
97
86
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 ' ) ;
100
89
}
101
90
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 )) {
111
92
$ is_valid = true ;
112
93
}
113
94
114
95
if (false === $ is_valid ) {
115
- throw new \Exception (' Unexpected MimeType: ' . $ mimetype );
96
+ throw new \Exception (Lang:: get ( ' laravel-filemanager::lfm.error-mime ' ) . $ mimetype );
116
97
}
117
98
return $ is_valid ;
118
99
}
0 commit comments