@@ -34,6 +34,7 @@ class UploadHandler
34
34
'min_width ' => 'Image requires a minimum width ' ,
35
35
'max_height ' => 'Image exceeds maximum height ' ,
36
36
'min_height ' => 'Image requires a minimum height ' ,
37
+ 'mime_not_allowed ' => 'Uploaded file content not allowed ' ,
37
38
'up_dir_make ' => 'The web server does not have permission to create the upload directory ' ,
38
39
'up_dir_write ' => 'The web server does not have upload dir file save permission ' ,
39
40
'abort ' => 'File upload aborted ' ,
@@ -50,6 +51,13 @@ public function __construct($options = null, $initialize = true, $error_messages
50
51
'upload_url ' => $ this ->get_full_url ().'/files/ ' ,
51
52
'input_stream ' => 'php://input ' ,
52
53
'user_dirs ' => false ,
54
+ 'mime_types_allowed ' => array (
55
+ 'image/gif ' ,
56
+ 'image/jpeg ' ,
57
+ 'image/png ' ,
58
+ 'video/webm ' ,
59
+ 'video/mp4 ' ,
60
+ ),
53
61
'mkdir_mode ' => 0755 ,
54
62
'param_name ' => 'files ' ,
55
63
// Set the following option to 'POST', if your server does not support
@@ -320,6 +328,11 @@ protected function get_file_object($file_name) {
320
328
$ file ->name ,
321
329
$ version
322
330
);
331
+ } else if (is_file ($ this ->get_upload_path ($ file_name .'.png ' , $ version ))) {
332
+ $ file ->{$ version .'Url ' } = $ this ->get_download_url (
333
+ $ file ->name .'.png ' ,
334
+ $ version
335
+ );
323
336
}
324
337
}
325
338
}
@@ -1031,6 +1044,23 @@ protected function is_valid_image_file($file_path) {
1031
1044
return $ image_info && $ image_info [0 ] && $ image_info [1 ];
1032
1045
}
1033
1046
1047
+ protected function handle_video_file ($ file_path , $ file ) {
1048
+ /*
1049
+ if ffmpeg exists on the system
1050
+ use ffmpeg to create a PNG thumbnail from the uploaded video
1051
+ */
1052
+ if (`which ffmpeg `) {
1053
+ $ thumbfile = $ file ->name . '.png ' ;
1054
+ shell_exec ('ffmpeg -ss 3 -i " ' .$ file_path .'" -frames:v 1 files/thumbnail/" ' .$ thumbfile .'" ' );
1055
+ if (file_exists ('files/thumbnail/ ' .$ thumbnail )) {
1056
+ $ file ->{'thumbnailUrl ' } = $ this ->get_download_url (
1057
+ $ thumbfile ,
1058
+ 'thumbnail '
1059
+ );
1060
+ }
1061
+ }
1062
+ }
1063
+
1034
1064
protected function handle_image_file ($ file_path , $ file ) {
1035
1065
$ failed_versions = array ();
1036
1066
foreach ($ this ->options ['image_versions ' ] as $ version => $ options ) {
@@ -1064,6 +1094,27 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
1064
1094
$ file ->type = $ type ;
1065
1095
if ($ this ->validate ($ uploaded_file , $ file , $ error , $ index )) {
1066
1096
$ this ->handle_form_data ($ file , $ index );
1097
+
1098
+ $ file_path = $ this ->get_upload_path ($ file ->name );
1099
+ $ mime_type = $ this ->get_mime_type ($ uploaded_file );
1100
+
1101
+
1102
+ $ arrmime = explode ('/ ' , $ mime_type );
1103
+ $ mimeTopLevel = '' ;
1104
+ if ($ arrmime ) {
1105
+ $ mimeTopLevel = $ arrmime [0 ];
1106
+ }
1107
+
1108
+ if (!$ this ->options ['mime_types_allowed ' ]) {
1109
+ // all mime types allowed
1110
+ } else if (in_array ($ mime_type , $ this ->options ['mime_types_allowed ' ])) {
1111
+ // mime type is specifically allowed
1112
+ } else {
1113
+ unlink ($ uploaded_file );
1114
+ $ file ->error = $ this ->get_error_message ('mime_not_allowed ' );
1115
+ return $ file ;
1116
+ }
1117
+
1067
1118
$ upload_dir = $ this ->get_upload_path ();
1068
1119
if (!is_dir ($ upload_dir )) {
1069
1120
if (!mkdir ($ upload_dir , $ this ->options ['mkdir_mode ' ], true )) {
@@ -1077,7 +1128,7 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
1077
1128
$ file ->error = $ this ->get_error_message ('up_dir_write ' );
1078
1129
return $ file ;
1079
1130
}
1080
- $ file_path = $ this -> get_upload_path ( $ file -> name );
1131
+
1081
1132
$ append_file = $ content_range && is_file ($ file_path ) &&
1082
1133
$ file ->size > $ this ->get_file_size ($ file_path );
1083
1134
if ($ uploaded_file && is_uploaded_file ($ uploaded_file )) {
@@ -1102,8 +1153,19 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
1102
1153
$ file_size = $ this ->get_file_size ($ file_path , $ append_file );
1103
1154
if ($ file_size === $ file ->size ) {
1104
1155
$ file ->url = $ this ->get_download_url ($ file ->name );
1105
- if ($ this ->is_valid_image_file ($ file_path )) {
1106
- $ this ->handle_image_file ($ file_path , $ file );
1156
+
1157
+ switch ($ mimeTopLevel ) {
1158
+ case 'video ' :
1159
+ $ this ->handle_video_file ($ file_path , $ file );
1160
+ break ;
1161
+ case 'image ' :
1162
+ if ($ this ->is_valid_image_file ($ file_path )) {
1163
+ $ this ->handle_image_file ($ file_path , $ file );
1164
+ }
1165
+ break ;
1166
+ default :
1167
+ # code...
1168
+ break ;
1107
1169
}
1108
1170
} else {
1109
1171
$ file ->size = $ file_size ;
@@ -1185,6 +1247,25 @@ protected function get_file_names_params() {
1185
1247
return $ params ;
1186
1248
}
1187
1249
1250
+ protected function get_mime_type ($ tmpname ) {
1251
+ $ mimetype = '' ;
1252
+ if (function_exists ('finfo_fopen ' )) {
1253
+ //echo 'using finfo_fopen';
1254
+ $ finfo = new finfo ();
1255
+ $ mimetype = $ finfo ->file ($ tmpname ,FILEINFO_MIME_TYPE );
1256
+ } elseif (function_exists ('mime_content_type ' )) {
1257
+ //echo 'using mime_content_type';
1258
+ $ mimetype = mime_content_type ($ tmpname );
1259
+ } elseif (function_exists ('getimagesize ' )) {
1260
+ //echo 'using getimagesize';
1261
+ $ mimetype = getimagesize ($ tmpname );
1262
+ } elseif (function_exists ('exif_imagetype ' )) {
1263
+ //echo 'using exif_imagetype';
1264
+ $ mimetype = exif_imagetype ($ tmpname );
1265
+ }
1266
+ return $ mimetype ;
1267
+ }
1268
+
1188
1269
protected function get_file_type ($ file_path ) {
1189
1270
switch (strtolower (pathinfo ($ file_path , PATHINFO_EXTENSION ))) {
1190
1271
case 'jpeg ' :
0 commit comments