handle files via standard field

This commit is contained in:
Petri Asikainen 2021-07-06 06:25:37 +03:00
parent bbe964c6b2
commit c894e8ceb3
2 changed files with 27 additions and 18 deletions

View file

@ -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;

View file

@ -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);
});
}