Misc Flysystem/image upload request fixes

This commit is contained in:
snipe 2020-08-26 11:53:36 -07:00
parent 139c874308
commit d25a05d748
No known key found for this signature in database
GPG key ID: 10BFFDA3ED34B5AC
5 changed files with 41 additions and 67 deletions

View file

@ -85,7 +85,7 @@ class AssetModelsController extends Controller
$model->fieldset_id = e($request->input('custom_fieldset'));
}
$model = $request->handleImages($model,600,null,null, public_path().'/uploads/models');
$model = $request->handleImages($model,600,'image', public_path().'/uploads/models', 'image');
// Was it created?
if ($model->save()) {

View file

@ -410,27 +410,9 @@ class SettingsController extends Controller
}
if ($request->hasFile('logo')) {
$image = $request->file('logo');
$ext = $image->getClientOriginalExtension();
$setting->logo = $file_name = 'logo-'.date('U').'.'. $ext;
$setting = $request->handleImages('logo',600,'logo','', 'logo');
if ('svg' != $image->getClientOriginalExtension()) {
$upload = Image::make($image->getRealPath())->resize(null, 150, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
}
// This requires a string instead of an object, so we use ($string)
Storage::disk('public')->put($file_name, (string) $upload->encode());
// Remove Current image if exists
if (($setting->logo) && (file_exists($file_name))) {
Storage::disk('public')->delete($file_name);
}
} elseif ('1' == $request->input('clear_logo')) {
if ('1' == $request->input('clear_logo')) {
Storage::disk('public')->delete($setting->logo);
$setting->logo = null;
$setting->brand = 1;

View file

@ -291,9 +291,11 @@ class UsersController extends Controller
$user->permissions = json_encode($permissions_array);
app('App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'image', 'avatars', 'avatar');
// Handle uploaded avatar
app('App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'avatar', 'avatars', 'avatar');
//\Log::debug(print_r($user, true));
// Was the user updated?
if ($user->save()) {

View file

@ -43,49 +43,48 @@ 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 = 600, $fieldname = 'image', $path = null, $db_fieldname = 'image')
public function handleImages($item, $w = 600, $form_fieldname = null, $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);
$path = str_plural($type);
if ($type=='assetmodel') {
\Log::debug('This is an asset model! Override the path');
if ($type == 'assetmodel') {
$path = 'models';
}
if ($type=='user') {
\Log::debug('This is a user! Override the path');
if ($type == 'user') {
$path = 'avatars';
}
}
\Log::info('Path is: '.$path);
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);
\Log::debug('Type is: '.$type);
\Log::debug('Image path is: '.$path);
\Log::debug('Image fieldname is: '.$fieldname);
\Log::debug('Form fieldname is: '.$form_fieldname);
\Log::debug('DB fieldname is: '.$use_db_field);
\Log::debug('Trying to upload to '. $path);
if ($this->hasFile($fieldname)) {
\Log::debug($this->file());
if ($this->hasFile($form_fieldname)) {
if (!config('app.lock_passwords')) {
if (!Storage::disk('public')->exists($path))
{
\Log::debug($path);
// Storage::disk('public')->makeDirectory($path, 775);
}
if (!is_dir($path)) {
\Log::info($path.' does not exist');
//mkdir($path);
}
$image = $this->file($fieldname);
$image = $this->file($form_fieldname);
$ext = $image->getClientOriginalExtension();
$file_name = $type.'-'.str_random(18).'.'.$ext;
@ -120,40 +119,32 @@ 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.');
if (Storage::disk('public')->exists($path.'/'.$item->{$use_db_field})) {
// Assign the new filename as the fieldname
if (is_null($db_fieldname)) {
$item->{$fieldname} = $file_name;
} else {
$item->{$db_fieldname} = $file_name;
}
\Log::debug('A file already exists that we are replacing - we should delete the old one.');
try {
Storage::disk('public')->delete($path.'/'.$item->{$fieldname});
Storage::disk('public')->delete($path.'/'.$item->{$use_db_field});
\Log::debug('Old file '.$path.'/'.$file_name.' has been deleted.');
} catch (\Exception $e) {
\Log::debug('Could not delete old file. '.$path.'/'.$item->{$fieldname}.' does not exist?');
\Log::debug('Could not delete old file. '.$path.'/'.$file_name.' does not exist?');
}
}
$item->{$use_db_field} = $file_name;
}
// If the user isn't uploading anything new but wants to delete their old image, do so
} else {
\Log::debug('No file passed for '.$form_fieldname);
if ($this->input('image_delete')=='1') {
\Log::debug('Deleting image');
try {
if (is_null($db_fieldname)) {
$item->{$fieldname} = null;
Storage::disk('public')->delete($path . '/' . $item->{$fieldname});
} else {
$item->{$db_fieldname} = null;
Storage::disk('public')->delete($path . '/' . $item->{$fieldname});
}
Storage::disk('public')->delete($path . '/' . $item->{$use_db_field});
$item->{$use_db_field} = null;
} catch (\Exception $e) {
\Log::debug($e);
@ -163,7 +154,6 @@ class ImageUploadRequest extends Request
}
return $item;
}
}

View file

@ -6,7 +6,7 @@
<label class="btn btn-default" aria-hidden="true">
{{ trans('button.select_file') }}
<input type="file" name="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/png,image/svg" style="display:none; max-width: 90%" aria-label="image" aria-hidden="true">
</label>
<span class='label label-default' id="uploadFile-info"></span>