Fixed user image upload

TODO: We should probably rename the avatar field on the user’s table, to make it more consistent with the other model images
This commit is contained in:
snipe 2020-08-26 02:30:23 -07:00
parent de21f00771
commit f385f3e928
No known key found for this signature in database
GPG key ID: 10BFFDA3ED34B5AC
3 changed files with 45 additions and 16 deletions

View file

@ -128,7 +128,7 @@ class UsersController extends Controller
// we have to invoke the // we have to invoke the
app('App\Http\Requests\ImageUploadRequest')->handleImages($user); app('App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'image', 'avatars', 'avatar');
if ($user->save()) { if ($user->save()) {
if ($request->filled('groups')) { if ($request->filled('groups')) {
@ -208,6 +208,7 @@ class UsersController extends Controller
*/ */
public function update(SaveUserRequest $request, $id = null) public function update(SaveUserRequest $request, $id = null)
{ {
// We need to reverse the UI specific logic for our // We need to reverse the UI specific logic for our
// permissions here before we update the user. // permissions here before we update the user.
$permissions = $request->input('permissions', array()); $permissions = $request->input('permissions', array());
@ -290,7 +291,8 @@ class UsersController extends Controller
$user->permissions = json_encode($permissions_array); $user->permissions = json_encode($permissions_array);
app('App\Http\Requests\ImageUploadRequest')->handleImages($user); app('App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'image', 'avatars', 'avatar');
// Was the user updated? // Was the user updated?

View file

@ -43,20 +43,31 @@ class ImageUploadRequest extends Request
* @param String $path location for uploaded images, defaults to uploads/plural of item type. * @param String $path location for uploaded images, defaults to uploads/plural of item type.
* @return SnipeModel Target asset is being checked out to. * @return SnipeModel Target asset is being checked out to.
*/ */
public function handleImages($item, $w = 550, $fieldname = 'image', $path = null) public function handleImages($item, $w = 600, $fieldname = 'image', $path = null, $db_fieldname = 'image')
{ {
\Log::debug('Handle file upload'); \Log::debug('Handle file upload');
$type = strtolower(class_basename(get_class($item))); $type = strtolower(class_basename(get_class($item)));
if (is_null($path)) { if (is_null($path)) {
$path = str_plural($type); $path = str_plural($type);
\Log::info('Path is: '.$path);
if ($type=='assetmodel') {
\Log::debug('This is an asset model! Override the path');
$path = 'models';
} }
if ($type=='user') {
\Log::debug('This is a user! Override the path');
$path = 'avatars';
}
}
\Log::info('Path is: '.$path);
\Log::debug('Type is: '.$type);
\Log::debug('Image path is: '.$path); \Log::debug('Image path is: '.$path);
\Log::debug('Image fieldname is: '.$fieldname); \Log::debug('Image fieldname is: '.$fieldname);
\Log::debug('Trying to upload to '. $path); \Log::debug('Trying to upload to '. $path);
if ($this->hasFile($fieldname)) { if ($this->hasFile($fieldname)) {
@ -81,8 +92,8 @@ class ImageUploadRequest extends Request
\Log::info('File name will be: '.$file_name); \Log::info('File name will be: '.$file_name);
if ($image->getClientOriginalExtension()!=='svg') { if ($image->getClientOriginalExtension()!=='svg') {
\Log::info('Not an SVG - resize'); \Log::debug('Not an SVG - resize');
\Log::info('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();
$constraint->upsize(); $constraint->upsize();
@ -93,15 +104,16 @@ class ImageUploadRequest extends Request
// 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::info('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());
$cleanSVG = $sanitizer->sanitize($dirtySVG); $cleanSVG = $sanitizer->sanitize($dirtySVG);
try { try {
\Log::info('Trying to upload to: '.$path.'/'.$file_name); \Log::debug('Trying to upload to: '.$path.'/'.$file_name);
Storage::disk('public')->put($path.'/'.$file_name, $cleanSVG); Storage::disk('public')->put($path.'/'.$file_name, $cleanSVG);
} catch (\Exception $e) { } catch (\Exception $e) {
\Log::debug('Upload no workie :( ');
\Log::debug($e); \Log::debug($e);
} }
} }
@ -109,26 +121,40 @@ class ImageUploadRequest extends Request
// Remove Current image if exists // Remove Current image if exists
if (($item->{$fieldname}) && (Storage::disk('public')->exists($path.'/'.$item->{$fieldname}))) { if (($item->{$fieldname}) && (Storage::disk('public')->exists($path.'/'.$item->{$fieldname}))) {
\Log::debug('A file already exists that we are replacing - we should delete the old one.');
try { try {
Storage::disk('public')->delete($path.'/'.$item->{$fieldname}); } catch (\Exception $e) { Storage::disk('public')->delete($path.'/'.$item->{$fieldname});
} catch (\Exception $e) {
\Log::debug('Could not delete old file. '.$path.'/'.$item->{$fieldname}.' does not exist?'); \Log::debug('Could not delete old file. '.$path.'/'.$item->{$fieldname}.' does not exist?');
} }
} }
// Assign the new filename as the fieldname // Assign the new filename as the fieldname
if (is_null($db_fieldname)) {
$item->{$fieldname} = $file_name; $item->{$fieldname} = $file_name;
} else {
$item->{$db_fieldname} = $file_name;
}
} }
// If the user isn't uploading anything new but wants to delete their old image, do so // If the user isn't uploading anything new but wants to delete their old image, do so
} else { } else {
\Log::debug($item->{$fieldname});
\Log::debug('No image was passed - not sure what to do now.'); \Log::debug('No image was passed - not sure what to do now.');
if ($this->input('image_delete')=='1') { if ($this->input('image_delete')=='1') {
try { try {
Storage::disk('public')->delete($path . '/' . $item->{$fieldname}); Storage::disk('public')->delete($path . '/' . $item->{$fieldname});
if (is_null($db_fieldname)) {
$item->{$fieldname} = null; $item->{$fieldname} = null;
} else {
$item->{$db_fieldname} = null;
}
} catch (\Exception $e) { } catch (\Exception $e) {
\Log::debug($e); \Log::debug($e);
} }
@ -137,6 +163,7 @@ class ImageUploadRequest extends Request
} }
return $item; return $item;
} }
} }

View file

@ -2,7 +2,7 @@
<label class="col-md-3 control-label" for="image">{{ trans('general.image_upload') }}</label> <label class="col-md-3 control-label" for="image">{{ trans('general.image_upload') }}</label>
<div class="col-md-9"> <div class="col-md-9">
<input type="file" id="image" name="image" aria-label="image" class="sr-only"> <input type="file" id="image" name="{{ (isset($fieldname) ? $fieldname : 'image') }}" aria-label="image" class="sr-only">
<label class="btn btn-default" aria-hidden="true"> <label class="btn btn-default" aria-hidden="true">
{{ trans('button.select_file') }} {{ trans('button.select_file') }}