Fixed #8147 - allow webp image format for public file uploads

This commit is contained in:
snipe 2020-12-01 19:06:53 -08:00
parent bfc60864dd
commit 4ac15daee7
3 changed files with 24 additions and 17 deletions

View file

@ -27,7 +27,7 @@ class ImageUploadRequest extends Request
public function rules() public function rules()
{ {
return [ return [
'image' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml', 'image' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp',
'avatar' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml', 'avatar' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml',
]; ];
} }
@ -91,8 +91,8 @@ class ImageUploadRequest extends Request
\Log::info('File name will be: '.$file_name); \Log::info('File name will be: '.$file_name);
\Log::debug('File extension is: '. $ext); \Log::debug('File extension is: '. $ext);
if ($image->getClientOriginalExtension()!=='svg') { if (($image->getClientOriginalExtension()!=='webp') && ($image->getClientOriginalExtension()!=='svg')) {
\Log::debug('Not an SVG - resize'); \Log::debug('Not an SVG or webp - resize');
\Log::debug('Trying to upload to: '.$path.'/'.$file_name); \Log::debug('Trying to upload to: '.$path.'/'.$file_name);
$upload = Image::make($image->getRealPath())->resize(null, $w, function ($constraint) { $upload = Image::make($image->getRealPath())->resize(null, $w, function ($constraint) {
$constraint->aspectRatio(); $constraint->aspectRatio();
@ -102,9 +102,15 @@ class ImageUploadRequest extends Request
// This requires a string instead of an object, so we use ($string) // This requires a string instead of an object, so we use ($string)
Storage::disk('public')->put($path.'/'.$file_name, (string)$upload->encode()); Storage::disk('public')->put($path.'/'.$file_name, (string)$upload->encode());
} else {
// If the file is a webp, we need to just move it since webp support
// needs to be compiled into gd for resizing to be available
if ($image->getClientOriginalExtension()=='webp') {
\Log::debug('This is a webp, just move it');
Storage::disk('public')->put($path.'/'.$file_name, file_get_contents($image));
// If the file is an SVG, we need to clean it and NOT encode it // If the file is an SVG, we need to clean it and NOT encode it
} else { } else {
\Log::debug('This is an SVG'); \Log::debug('This is an SVG');
$sanitizer = new Sanitizer(); $sanitizer = new Sanitizer();
$dirtySVG = file_get_contents($image->getRealPath()); $dirtySVG = file_get_contents($image->getRealPath());
@ -118,6 +124,7 @@ class ImageUploadRequest extends Request
\Log::debug($e); \Log::debug($e);
} }
} }
}
// Remove Current image if exists // Remove Current image if exists

View file

@ -112,7 +112,7 @@
'image' => 'Image', 'image' => 'Image',
'image_delete' => 'Delete Image', 'image_delete' => 'Delete Image',
'image_upload' => 'Upload Image', 'image_upload' => 'Upload Image',
'image_filetypes_help' => 'Accepted filetypes are jpg, png, gif, and svg. Max upload size allowed is :size.', 'image_filetypes_help' => 'Accepted filetypes are jpg, webp, png, gif, and svg. Max upload size allowed is :size.',
'import' => 'Import', 'import' => 'Import',
'importing' => 'Importing', 'importing' => 'Importing',
'importing_help' => 'You can import assets, accessories, licenses, components, consumables, and users via CSV file. <br><br>The CSV should be comma-delimited and formatted with headers that match the ones in the <a href="https://snipe-it.readme.io/docs/importing" target="_new">sample CSVs in the documentation</a>.', 'importing_help' => 'You can import assets, accessories, licenses, components, consumables, and users via CSV file. <br><br>The CSV should be comma-delimited and formatted with headers that match the ones in the <a href="https://snipe-it.readme.io/docs/importing" target="_new">sample CSVs in the documentation</a>.',

View file

@ -6,7 +6,7 @@
<label class="btn btn-default" aria-hidden="true"> <label class="btn btn-default" aria-hidden="true">
{{ trans('button.select_file') }} {{ trans('button.select_file') }}
<input type="file" name="{{ (isset($fieldname) ? $fieldname : 'image') }}" class="js-uploadFile" id="uploadFile" data-maxsize="{{ \App\Helpers\Helper::file_upload_max_size() }}" accept="image/gif,image/jpeg,image/png,image/svg" style="display:none; max-width: 90%" aria-label="image" aria-hidden="true"> <input type="file" name="{{ (isset($fieldname) ? $fieldname : 'image') }}" class="js-uploadFile" id="uploadFile" data-maxsize="{{ \App\Helpers\Helper::file_upload_max_size() }}" accept="image/gif,image/jpeg,image/webp,image/png,image/svg" style="display:none; max-width: 90%" aria-label="image" aria-hidden="true">
</label> </label>
<span class='label label-default' id="uploadFile-info"></span> <span class='label label-default' id="uploadFile-info"></span>