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-03-05 18:00:24 -08:00
|
|
|
'image' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml',
|
|
|
|
'avatar' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml',
|
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-03-05 18:00:24 -08:00
|
|
|
public function handleImages($item, $w = 550, $fieldname = 'image', $path = null)
|
2019-12-05 22:23:05 -08:00
|
|
|
{
|
2020-06-16 16:06:25 -07:00
|
|
|
\Log::debug('Handle file upload');
|
2019-12-05 22:23:05 -08:00
|
|
|
$type = strtolower(class_basename(get_class($item)));
|
|
|
|
|
|
|
|
if (is_null($path)) {
|
2020-08-03 11:17:56 -07:00
|
|
|
$path = str_plural($type);
|
2020-06-16 16:06:25 -07:00
|
|
|
\Log::info('Path is: '.$path);
|
2019-12-05 22:23:05 -08:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:00:24 -08:00
|
|
|
\Log::debug('Image path is: '.$path);
|
|
|
|
\Log::debug('Image fieldname is: '.$fieldname);
|
|
|
|
|
|
|
|
|
2019-12-05 22:23:05 -08:00
|
|
|
\Log::debug('Trying to upload to '. $path);
|
|
|
|
|
2020-03-05 18:00:24 -08:00
|
|
|
if ($this->hasFile($fieldname)) {
|
2019-12-05 22:23:05 -08:00
|
|
|
|
|
|
|
if (!config('app.lock_passwords')) {
|
|
|
|
|
2020-06-16 16:06:25 -07:00
|
|
|
if (!Storage::disk('public')->exists($path))
|
|
|
|
{
|
|
|
|
\Log::debug($path);
|
|
|
|
// Storage::disk('public')->makeDirectory($path, 775);
|
|
|
|
}
|
2019-12-05 22:23:05 -08:00
|
|
|
|
|
|
|
if (!is_dir($path)) {
|
2020-06-16 16:06:25 -07:00
|
|
|
\Log::info($path.' does not exist');
|
|
|
|
//mkdir($path);
|
2019-12-05 22:23:05 -08:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:00:24 -08:00
|
|
|
$image = $this->file($fieldname);
|
2019-12-05 22:23:05 -08:00
|
|
|
$ext = $image->getClientOriginalExtension();
|
|
|
|
$file_name = $type.'-'.str_random(18).'.'.$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);
|
2019-12-05 22:23:05 -08:00
|
|
|
|
|
|
|
if ($image->getClientOriginalExtension()!=='svg') {
|
2020-06-16 16:06:25 -07:00
|
|
|
\Log::info('Not an SVG - resize');
|
|
|
|
\Log::info('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
|
|
|
|
2020-03-05 18:00:24 -08:00
|
|
|
// If the file is an SVG, we need to clean it and NOT encode it
|
2019-12-05 22:23:05 -08:00
|
|
|
} else {
|
2020-06-16 16:06:25 -07:00
|
|
|
\Log::info('This is an SVG');
|
2019-12-05 22:23:05 -08:00
|
|
|
$sanitizer = new Sanitizer();
|
|
|
|
$dirtySVG = file_get_contents($image->getRealPath());
|
|
|
|
$cleanSVG = $sanitizer->sanitize($dirtySVG);
|
|
|
|
|
|
|
|
try {
|
2020-06-16 16:06:25 -07:00
|
|
|
\Log::info('Trying to upload to: '.$path.'/'.$file_name);
|
2020-03-05 18:00:24 -08:00
|
|
|
Storage::disk('public')->put($path.'/'.$file_name, $cleanSVG);
|
2019-12-05 22:23:05 -08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-20 23:20:34 -07:00
|
|
|
// Remove Current image if exists
|
2020-03-05 18:00:24 -08:00
|
|
|
if (($item->{$fieldname}) && (Storage::disk('public')->exists($path.'/'.$item->{$fieldname}))) {
|
2019-12-05 22:23:05 -08:00
|
|
|
try {
|
2020-04-20 23:20:34 -07:00
|
|
|
Storage::disk('public')->delete($path.'/'.$item->{$fieldname}); } catch (\Exception $e) {
|
|
|
|
\Log::debug('Could not delete old file. '.$path.'/'.$item->{$fieldname}.' does not exist?');
|
|
|
|
|
2019-12-05 22:23:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:00:24 -08:00
|
|
|
// Assign the new filename as the fieldname
|
|
|
|
$item->{$fieldname} = $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 {
|
|
|
|
\Log::debug('No image was passed - not sure what to do now.');
|
|
|
|
if ($this->input('image_delete')=='1') {
|
2019-12-05 22:23:05 -08:00
|
|
|
|
2020-08-24 18:32:15 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
Storage::disk('public')->delete($path . '/' . $item->{$fieldname});
|
|
|
|
$item->{$fieldname} = null;
|
|
|
|
} 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
|
|
|
}
|