2016-12-27 17:31:53 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
2019-12-05 22:23:05 -08:00
|
|
|
use App\Models\SnipeModel;
|
|
|
|
use Intervention\Image\Facades\Image;
|
|
|
|
use enshrined\svgSanitize\Sanitizer;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Storage;
|
2016-12-27 17:31:53 -08:00
|
|
|
|
|
|
|
class ImageUploadRequest extends Request
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
2020-12-01 19:06:53 -08:00
|
|
|
'image' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp',
|
2021-05-21 16:56:11 -07:00
|
|
|
'avatar' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp',
|
2016-12-27 17:31:53 -08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function response(array $errors)
|
|
|
|
{
|
|
|
|
return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
|
|
|
|
}
|
2019-12-05 22:23:05 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle and store any images attached to request
|
|
|
|
* @param SnipeModel $item Item the image is associated with
|
|
|
|
* @param String $path location for uploaded images, defaults to uploads/plural of item type.
|
|
|
|
* @return SnipeModel Target asset is being checked out to.
|
|
|
|
*/
|
2020-08-26 11:53:36 -07:00
|
|
|
public function handleImages($item, $w = 600, $form_fieldname = null, $path = null, $db_fieldname = 'image')
|
2019-12-05 22:23:05 -08:00
|
|
|
{
|
2020-08-26 02:30:23 -07:00
|
|
|
|
2019-12-05 22:23:05 -08:00
|
|
|
$type = strtolower(class_basename(get_class($item)));
|
|
|
|
|
|
|
|
if (is_null($path)) {
|
2020-11-16 16:39:02 -08:00
|
|
|
$path = str_plural($type);
|
2020-08-26 02:30:23 -07:00
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
if ($type == 'assetmodel') {
|
2020-08-26 02:30:23 -07:00
|
|
|
$path = 'models';
|
|
|
|
}
|
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
if ($type == 'user') {
|
2020-08-26 02:30:23 -07:00
|
|
|
$path = 'avatars';
|
|
|
|
}
|
2019-12-05 22:23:05 -08:00
|
|
|
}
|
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
if (is_null($form_fieldname)) {
|
|
|
|
$form_fieldname = 'image';
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is dumb, but we need it for overriding field names for exceptions like avatars and logo uploads
|
|
|
|
if (is_null($db_fieldname)) {
|
|
|
|
$use_db_field = $form_fieldname;
|
|
|
|
} else {
|
|
|
|
$use_db_field = $db_fieldname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
\Log::info('Image path is: '.$path);
|
2020-08-26 02:30:23 -07:00
|
|
|
\Log::debug('Type is: '.$type);
|
2020-08-26 11:53:36 -07:00
|
|
|
\Log::debug('Form fieldname is: '.$form_fieldname);
|
|
|
|
\Log::debug('DB fieldname is: '.$use_db_field);
|
2019-12-05 22:23:05 -08:00
|
|
|
\Log::debug('Trying to upload to '. $path);
|
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
\Log::debug($this->file());
|
2019-12-05 22:23:05 -08:00
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
if ($this->hasFile($form_fieldname)) {
|
2019-12-05 22:23:05 -08:00
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
if (!config('app.lock_passwords')) {
|
2019-12-05 22:23:05 -08:00
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
$image = $this->file($form_fieldname);
|
2019-12-05 22:23:05 -08:00
|
|
|
$ext = $image->getClientOriginalExtension();
|
2020-08-26 12:17:36 -07:00
|
|
|
$file_name = $type.'-'.$form_fieldname.'-'.str_random(10).'.'.$ext;
|
2018-09-29 21:33:52 -07:00
|
|
|
|
2020-06-16 16:06:25 -07:00
|
|
|
\Log::info('File name will be: '.$file_name);
|
2020-08-26 12:17:36 -07:00
|
|
|
\Log::debug('File extension is: '. $ext);
|
2019-12-05 22:23:05 -08:00
|
|
|
|
2020-12-01 19:06:53 -08:00
|
|
|
if (($image->getClientOriginalExtension()!=='webp') && ($image->getClientOriginalExtension()!=='svg')) {
|
|
|
|
\Log::debug('Not an SVG or webp - resize');
|
2020-08-26 02:30:23 -07:00
|
|
|
\Log::debug('Trying to upload to: '.$path.'/'.$file_name);
|
2019-12-05 22:23:05 -08:00
|
|
|
$upload = Image::make($image->getRealPath())->resize(null, $w, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
2020-04-21 06:29:57 -07:00
|
|
|
});
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2020-03-05 18:00:24 -08:00
|
|
|
// This requires a string instead of an object, so we use ($string)
|
|
|
|
Storage::disk('public')->put($path.'/'.$file_name, (string)$upload->encode());
|
2018-09-29 21:33:52 -07:00
|
|
|
|
2019-12-05 22:23:05 -08:00
|
|
|
} else {
|
2020-12-01 19:06:53 -08:00
|
|
|
// 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
|
|
|
|
} else {
|
|
|
|
|
|
|
|
\Log::debug('This is an SVG');
|
|
|
|
$sanitizer = new Sanitizer();
|
|
|
|
$dirtySVG = file_get_contents($image->getRealPath());
|
|
|
|
$cleanSVG = $sanitizer->sanitize($dirtySVG);
|
|
|
|
|
|
|
|
try {
|
|
|
|
\Log::debug('Trying to upload to: '.$path.'/'.$file_name);
|
|
|
|
Storage::disk('public')->put($path.'/'.$file_name, $cleanSVG);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug('Upload no workie :( ');
|
|
|
|
\Log::debug($e);
|
|
|
|
}
|
2019-12-05 22:23:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-20 23:20:34 -07:00
|
|
|
// Remove Current image if exists
|
2020-08-26 11:53:36 -07:00
|
|
|
if (Storage::disk('public')->exists($path.'/'.$item->{$use_db_field})) {
|
2020-08-26 02:48:07 -07:00
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
\Log::debug('A file already exists that we are replacing - we should delete the old one.');
|
2019-12-05 22:23:05 -08:00
|
|
|
try {
|
2020-08-26 11:53:36 -07:00
|
|
|
Storage::disk('public')->delete($path.'/'.$item->{$use_db_field});
|
|
|
|
\Log::debug('Old file '.$path.'/'.$file_name.' has been deleted.');
|
2020-08-26 02:30:23 -07:00
|
|
|
} catch (\Exception $e) {
|
2020-08-26 11:53:36 -07:00
|
|
|
\Log::debug('Could not delete old file. '.$path.'/'.$file_name.' does not exist?');
|
2020-04-20 23:20:34 -07:00
|
|
|
|
2019-12-05 22:23:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
$item->{$use_db_field} = $file_name;
|
|
|
|
|
2019-12-05 22:23:05 -08:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:00:24 -08:00
|
|
|
// If the user isn't uploading anything new but wants to delete their old image, do so
|
2020-08-24 18:32:15 -07:00
|
|
|
} else {
|
2020-08-26 11:53:36 -07:00
|
|
|
\Log::debug('No file passed for '.$form_fieldname);
|
2020-08-24 18:32:15 -07:00
|
|
|
if ($this->input('image_delete')=='1') {
|
2019-12-05 22:23:05 -08:00
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
\Log::debug('Deleting image');
|
2020-08-24 18:32:15 -07:00
|
|
|
try {
|
2020-08-26 02:48:07 -07:00
|
|
|
|
2020-08-26 11:53:36 -07:00
|
|
|
Storage::disk('public')->delete($path . '/' . $item->{$use_db_field});
|
|
|
|
$item->{$use_db_field} = null;
|
2020-08-26 02:30:23 -07:00
|
|
|
|
2020-08-24 18:32:15 -07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug($e);
|
|
|
|
}
|
2019-12-05 22:23:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-21 06:29:57 -07:00
|
|
|
|
|
|
|
|
2019-12-05 22:23:05 -08:00
|
|
|
return $item;
|
|
|
|
}
|
2016-12-27 17:31:53 -08:00
|
|
|
}
|