From c894e8ceb360a56a053f4a642da6bcb21028e0b8 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 6 Jul 2021 06:25:37 +0300 Subject: [PATCH] handle files via standard field --- app/Http/Requests/ImageUploadRequest.php | 26 +++++++++++++++++------ app/Http/Traits/ConvertsBase64ToFiles.php | 19 +++++++---------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index 9a5bd14022..b068e15df7 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -5,7 +5,8 @@ namespace App\Http\Requests; use App\Models\SnipeModel; use enshrined\svgSanitize\Sanitizer; use Intervention\Image\Facades\Image; -use App\Traits\ConvertsBase64ToFiles; +use App\Http\Traits\ConvertsBase64ToFiles; +use Illuminate\Http\UploadedFile; use Storage; class ImageUploadRequest extends Request @@ -88,13 +89,24 @@ class ImageUploadRequest extends Request \Log::debug('Type is: '.$type); \Log::debug('Form fieldname is: '.$form_fieldname); \Log::debug('DB fieldname is: '.$use_db_field); - \Log::debug('Trying to upload to '.$path); - - \Log::debug($this->file()); - - if ($this->hasFile($form_fieldname)) { - if (! config('app.lock_passwords')) { + \Log::debug('Trying to upload to '. $path); + + // ConvertBase64ToFiles just changes object type, + // as it cannot currently insert files to $this->files + if ($this->offsetGet($form_fieldname) instanceof UploadedFile) { + $image=$this->offsetGet($form_fieldname); + } else { + if ($this->hasFile($form_fieldname)) { $image = $this->file($form_fieldname); + } + } + + \Log::debug($image); + + if (isset($image)) { + + if (!config('app.lock_passwords')) { + $ext = $image->getClientOriginalExtension(); $file_name = $type.'-'.$form_fieldname.'-'.str_random(10).'.'.$ext; diff --git a/app/Http/Traits/ConvertsBase64ToFiles.php b/app/Http/Traits/ConvertsBase64ToFiles.php index 13c939e4db..793746dbb6 100644 --- a/app/Http/Traits/ConvertsBase64ToFiles.php +++ b/app/Http/Traits/ConvertsBase64ToFiles.php @@ -85,17 +85,14 @@ trait ConvertsBase64ToFiles $uploadedFile = new UploadedFile($tempFilePath, $filename, null, null, true); - $body = $this->bodyParametersBag()->all(); - Arr::forget($body, $key); - $this->bodyParametersBag()->replace($body); - \Log::debug("Trait: file field $key replaced:". $request->has($key)); - - - $files = $this->uploadFilesBag()->all(); - Arr::set($files, $key, $uploadedFile); - $this->uploadFilesBag()->replace($files); - \Log::debug("Trait: file field $key inserted:". $request->hasFile($key)); - + \Log::debug("Trait: uploadedfile ". $tempFilePath); + $this->offsetUnset($key); + \Log::debug("Trait: encoded field \"$key\" removed" ); + + //Inserting new file to $this-files does not work so have to deal this after + $this->offsetSet($key,$uploadedFile); + \Log::debug("Trait: encoded field \"$key\" inserted" ); + }, null, false); }); }