From b9f65243a38d4297f63ced379f8d2ddfd55c4850 Mon Sep 17 00:00:00 2001 From: richard512 Date: Sat, 3 Sep 2016 11:53:36 -0700 Subject: [PATCH 1/3] Added file permissions problem detection --- server/php/UploadHandler.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/server/php/UploadHandler.php b/server/php/UploadHandler.php index b3bb507e4..57a50a5d6 100755 --- a/server/php/UploadHandler.php +++ b/server/php/UploadHandler.php @@ -34,6 +34,8 @@ class UploadHandler 'min_width' => 'Image requires a minimum width', 'max_height' => 'Image exceeds maximum height', 'min_height' => 'Image requires a minimum height', + 'up_dir_make' => 'The web server does not have permission to create the upload directory', + 'up_dir_write' => 'The web server does not have upload dir file save permission', 'abort' => 'File upload aborted', 'image_resize' => 'Failed to resize image' ); @@ -1064,7 +1066,16 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro $this->handle_form_data($file, $index); $upload_dir = $this->get_upload_path(); if (!is_dir($upload_dir)) { - mkdir($upload_dir, $this->options['mkdir_mode'], true); + if (!mkdir($upload_dir, $this->options['mkdir_mode'], true)) { + unlink($uploaded_file); + $file->error = $this->get_error_message('up_dir_make'); + return $file; + } + } + if (!is_writable($upload_dir)) { + unlink($uploaded_file); + $file->error = $this->get_error_message('up_dir_write'); + return $file; } $file_path = $this->get_upload_path($file->name); $append_file = $content_range && is_file($file_path) && From 414b826dbcabae48cd34e28ed6f8481fa0c8ed5d Mon Sep 17 00:00:00 2001 From: richard512 Date: Sat, 3 Sep 2016 13:36:23 -0700 Subject: [PATCH 2/3] added a bit of video processing via ffmpeg --- server/php/UploadHandler.php | 87 ++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 3 deletions(-) diff --git a/server/php/UploadHandler.php b/server/php/UploadHandler.php index 57a50a5d6..2158dcd55 100755 --- a/server/php/UploadHandler.php +++ b/server/php/UploadHandler.php @@ -34,6 +34,7 @@ class UploadHandler 'min_width' => 'Image requires a minimum width', 'max_height' => 'Image exceeds maximum height', 'min_height' => 'Image requires a minimum height', + 'mime_not_allowed' => 'Uploaded file content not allowed', 'up_dir_make' => 'The web server does not have permission to create the upload directory', 'up_dir_write' => 'The web server does not have upload dir file save permission', 'abort' => 'File upload aborted', @@ -50,6 +51,13 @@ public function __construct($options = null, $initialize = true, $error_messages 'upload_url' => $this->get_full_url().'/files/', 'input_stream' => 'php://input', 'user_dirs' => false, + 'mime_types_allowed' => array( + 'image/gif', + 'image/jpeg', + 'image/png', + 'video/webm', + 'video/mp4', + ), 'mkdir_mode' => 0755, 'param_name' => 'files', // Set the following option to 'POST', if your server does not support @@ -320,6 +328,11 @@ protected function get_file_object($file_name) { $file->name, $version ); + } else if (is_file($this->get_upload_path($file_name.'.png', $version))) { + $file->{$version.'Url'} = $this->get_download_url( + $file->name.'.png', + $version + ); } } } @@ -1031,6 +1044,23 @@ protected function is_valid_image_file($file_path) { return $image_info && $image_info[0] && $image_info[1]; } + protected function handle_video_file($file_path, $file) { + /* + if ffmpeg exists on the system + use ffmpeg to create a PNG thumbnail from the uploaded video + */ + if (`which ffmpeg`) { + $thumbfile = $file->name . '.png'; + shell_exec('ffmpeg -ss 3 -i "'.$file_path.'" -frames:v 1 files/thumbnail/"'.$thumbfile.'"'); + if (file_exists('files/thumbnail/'.$thumbnail)) { + $file->{'thumbnailUrl'} = $this->get_download_url( + $thumbfile, + 'thumbnail' + ); + } + } + } + protected function handle_image_file($file_path, $file) { $failed_versions = array(); foreach ($this->options['image_versions'] as $version => $options) { @@ -1064,6 +1094,27 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro $file->type = $type; if ($this->validate($uploaded_file, $file, $error, $index)) { $this->handle_form_data($file, $index); + + $file_path = $this->get_upload_path($file->name); + $mime_type = $this->get_mime_type($uploaded_file); + + + $arrmime = explode('/', $mime_type); + $mimeTopLevel = ''; + if ($arrmime) { + $mimeTopLevel = $arrmime[0]; + } + + if (!$this->options['mime_types_allowed']) { + // all mime types allowed + } else if (in_array($mime_type, $this->options['mime_types_allowed'])) { + // mime type is specifically allowed + } else { + unlink($uploaded_file); + $file->error = $this->get_error_message('mime_not_allowed'); + return $file; + } + $upload_dir = $this->get_upload_path(); if (!is_dir($upload_dir)) { if (!mkdir($upload_dir, $this->options['mkdir_mode'], true)) { @@ -1077,7 +1128,7 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro $file->error = $this->get_error_message('up_dir_write'); return $file; } - $file_path = $this->get_upload_path($file->name); + $append_file = $content_range && is_file($file_path) && $file->size > $this->get_file_size($file_path); if ($uploaded_file && is_uploaded_file($uploaded_file)) { @@ -1102,8 +1153,19 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro $file_size = $this->get_file_size($file_path, $append_file); if ($file_size === $file->size) { $file->url = $this->get_download_url($file->name); - if ($this->is_valid_image_file($file_path)) { - $this->handle_image_file($file_path, $file); + + switch ($mimeTopLevel) { + case 'video': + $this->handle_video_file($file_path, $file); + break; + case 'image': + if ($this->is_valid_image_file($file_path)) { + $this->handle_image_file($file_path, $file); + } + break; + default: + # code... + break; } } else { $file->size = $file_size; @@ -1185,6 +1247,25 @@ protected function get_file_names_params() { return $params; } + protected function get_mime_type($tmpname) { + $mimetype = ''; + if (function_exists('finfo_fopen')) { + //echo 'using finfo_fopen'; + $finfo = new finfo(); + $mimetype = $finfo->file($tmpname,FILEINFO_MIME_TYPE); + } elseif (function_exists('mime_content_type')) { + //echo 'using mime_content_type'; + $mimetype = mime_content_type($tmpname); + } elseif (function_exists('getimagesize')) { + //echo 'using getimagesize'; + $mimetype = getimagesize($tmpname); + } elseif (function_exists('exif_imagetype')) { + //echo 'using exif_imagetype'; + $mimetype = exif_imagetype($tmpname); + } + return $mimetype; + } + protected function get_file_type($file_path) { switch (strtolower(pathinfo($file_path, PATHINFO_EXTENSION))) { case 'jpeg': From eb2f4a87995988daf86bfdb0605dd20b01788331 Mon Sep 17 00:00:00 2001 From: richard512 Date: Sat, 3 Sep 2016 13:46:27 -0700 Subject: [PATCH 3/3] made .preview img max dimensions 80x80 --- css/style.css | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/css/style.css b/css/style.css index c33c03d40..4d30115d0 100644 --- a/css/style.css +++ b/css/style.css @@ -13,3 +13,8 @@ body { padding-top: 60px; } + +.preview img { + max-width: 80px; + max-height: 80px; +} \ No newline at end of file