From f385f3e9289f298fd6dc2c73ddf0fcbcc78305e1 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 26 Aug 2020 02:30:23 -0700 Subject: [PATCH] Fixed user image upload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: We should probably rename the avatar field on the user’s table, to make it more consistent with the other model images --- .../Controllers/Users/UsersController.php | 6 ++- app/Http/Requests/ImageUploadRequest.php | 53 ++++++++++++++----- .../forms/edit/image-upload.blade.php | 2 +- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/Users/UsersController.php b/app/Http/Controllers/Users/UsersController.php index 2e1c7ca317..1d06b1dc03 100755 --- a/app/Http/Controllers/Users/UsersController.php +++ b/app/Http/Controllers/Users/UsersController.php @@ -128,7 +128,7 @@ class UsersController extends Controller // 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 ($request->filled('groups')) { @@ -208,6 +208,7 @@ class UsersController extends Controller */ public function update(SaveUserRequest $request, $id = null) { + // We need to reverse the UI specific logic for our // permissions here before we update the user. $permissions = $request->input('permissions', array()); @@ -290,7 +291,8 @@ class UsersController extends Controller $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? diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index acf51970b5..104a960be4 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -43,20 +43,31 @@ class ImageUploadRequest extends Request * @param String $path location for uploaded images, defaults to uploads/plural of item type. * @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'); + + $type = strtolower(class_basename(get_class($item))); if (is_null($path)) { $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 fieldname is: '.$fieldname); - - \Log::debug('Trying to upload to '. $path); if ($this->hasFile($fieldname)) { @@ -81,8 +92,8 @@ class ImageUploadRequest extends Request \Log::info('File name will be: '.$file_name); if ($image->getClientOriginalExtension()!=='svg') { - \Log::info('Not an SVG - resize'); - \Log::info('Trying to upload to: '.$path.'/'.$file_name); + \Log::debug('Not an SVG - resize'); + \Log::debug('Trying to upload to: '.$path.'/'.$file_name); $upload = Image::make($image->getRealPath())->resize(null, $w, function ($constraint) { $constraint->aspectRatio(); $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 } else { - \Log::info('This is an SVG'); + \Log::debug('This is an SVG'); $sanitizer = new Sanitizer(); $dirtySVG = file_get_contents($image->getRealPath()); $cleanSVG = $sanitizer->sanitize($dirtySVG); try { - \Log::info('Trying to upload to: '.$path.'/'.$file_name); - Storage::disk('public')->put($path.'/'.$file_name, $cleanSVG); + \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); } } @@ -109,26 +121,40 @@ class ImageUploadRequest extends Request // Remove Current image if exists 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 { - 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?'); } } // Assign the new filename as the fieldname - $item->{$fieldname} = $file_name; + if (is_null($db_fieldname)) { + $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 } else { + \Log::debug($item->{$fieldname}); \Log::debug('No image was passed - not sure what to do now.'); if ($this->input('image_delete')=='1') { - try { Storage::disk('public')->delete($path . '/' . $item->{$fieldname}); - $item->{$fieldname} = null; + + if (is_null($db_fieldname)) { + $item->{$fieldname} = null; + } else { + $item->{$db_fieldname} = null; + } + } catch (\Exception $e) { \Log::debug($e); } @@ -137,6 +163,7 @@ class ImageUploadRequest extends Request } + return $item; } } diff --git a/resources/views/partials/forms/edit/image-upload.blade.php b/resources/views/partials/forms/edit/image-upload.blade.php index 3a2efadc46..d0c89bee99 100644 --- a/resources/views/partials/forms/edit/image-upload.blade.php +++ b/resources/views/partials/forms/edit/image-upload.blade.php @@ -2,7 +2,7 @@
- +