@@ -34,6 +34,7 @@ class UploadHandler
3434 'min_width ' => 'Image requires a minimum width ' ,
3535 'max_height ' => 'Image exceeds maximum height ' ,
3636 'min_height ' => 'Image requires a minimum height ' ,
37+ 'mime_not_allowed ' => 'Uploaded file content not allowed ' ,
3738 'up_dir_make ' => 'The web server does not have permission to create the upload directory ' ,
3839 'up_dir_write ' => 'The web server does not have upload dir file save permission ' ,
3940 'abort ' => 'File upload aborted ' ,
@@ -50,6 +51,13 @@ public function __construct($options = null, $initialize = true, $error_messages
5051 'upload_url ' => $ this ->get_full_url ().'/files/ ' ,
5152 'input_stream ' => 'php://input ' ,
5253 'user_dirs ' => false ,
54+ 'mime_types_allowed ' => array (
55+ 'image/gif ' ,
56+ 'image/jpeg ' ,
57+ 'image/png ' ,
58+ 'video/webm ' ,
59+ 'video/mp4 ' ,
60+ ),
5361 'mkdir_mode ' => 0755 ,
5462 'param_name ' => 'files ' ,
5563 // Set the following option to 'POST', if your server does not support
@@ -320,6 +328,11 @@ protected function get_file_object($file_name) {
320328 $ file ->name ,
321329 $ version
322330 );
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+ );
323336 }
324337 }
325338 }
@@ -1031,6 +1044,23 @@ protected function is_valid_image_file($file_path) {
10311044 return $ image_info && $ image_info [0 ] && $ image_info [1 ];
10321045 }
10331046
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+
10341064 protected function handle_image_file ($ file_path , $ file ) {
10351065 $ failed_versions = array ();
10361066 foreach ($ this ->options ['image_versions ' ] as $ version => $ options ) {
@@ -1064,6 +1094,27 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
10641094 $ file ->type = $ type ;
10651095 if ($ this ->validate ($ uploaded_file , $ file , $ error , $ index )) {
10661096 $ 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+
10671118 $ upload_dir = $ this ->get_upload_path ();
10681119 if (!is_dir ($ upload_dir )) {
10691120 if (!mkdir ($ upload_dir , $ this ->options ['mkdir_mode ' ], true )) {
@@ -1077,7 +1128,7 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
10771128 $ file ->error = $ this ->get_error_message ('up_dir_write ' );
10781129 return $ file ;
10791130 }
1080- $ file_path = $ this -> get_upload_path ( $ file -> name );
1131+
10811132 $ append_file = $ content_range && is_file ($ file_path ) &&
10821133 $ file ->size > $ this ->get_file_size ($ file_path );
10831134 if ($ uploaded_file && is_uploaded_file ($ uploaded_file )) {
@@ -1102,8 +1153,19 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
11021153 $ file_size = $ this ->get_file_size ($ file_path , $ append_file );
11031154 if ($ file_size === $ file ->size ) {
11041155 $ 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 ;
11071169 }
11081170 } else {
11091171 $ file ->size = $ file_size ;
@@ -1185,6 +1247,25 @@ protected function get_file_names_params() {
11851247 return $ params ;
11861248 }
11871249
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+
11881269 protected function get_file_type ($ file_path ) {
11891270 switch (strtolower (pathinfo ($ file_path , PATHINFO_EXTENSION ))) {
11901271 case 'jpeg ' :
0 commit comments