Merge branch 'feature/api-image-uploads-json' into feature/api-image-uploads

This commit is contained in:
Petri Asikainen 2021-07-06 06:55:54 +03:00
commit 2169c62700
3 changed files with 150 additions and 9 deletions

View file

@ -61,7 +61,7 @@ class RestoreFromBackup extends Command
$za = new ZipArchive();
$errcode = $za->open($filename, ZipArchive::RDONLY);
$errcode = $za->open($filename/* , ZipArchive::RDONLY */); // that constant only exists in PHP 7.4 and higher
if ($errcode !== true) {
$errors = [
ZipArchive::ER_EXISTS => "File already exists.",
@ -248,8 +248,13 @@ class RestoreFromBackup extends Command
}
fclose($pipes[0]);
fclose($sql_contents);
$this->line(stream_get_contents($pipes[1]));
fclose($pipes[1]);
$this->error(stream_get_contents($pipes[2]));
fclose($pipes[2]);
//wait, have to do fclose() on all pipes first?
$close_results = proc_close($proc_results);
if($close_results != 0) {

View file

@ -5,10 +5,14 @@ namespace App\Http\Requests;
use App\Models\SnipeModel;
use Intervention\Image\Facades\Image;
use enshrined\svgSanitize\Sanitizer;
use App\Http\Traits\ConvertsBase64ToFiles;
use Illuminate\Http\UploadedFile;
use Storage;
class ImageUploadRequest extends Request
{
use ConvertsBase64ToFiles;
/**
* Determine if the user is authorized to make this request.
*
@ -26,16 +30,27 @@ class ImageUploadRequest extends Request
*/
public function rules()
{
return [
'image' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp',
'avatar' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp',
];
return [
'image' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp',
'avatar' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp',
];
}
public function response(array $errors)
{
return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
}
/**
* Fields that should be traited from base64 to files
*/
protected function base64FileKeys(): array
{
return [
'image' => 'auto',
];
}
/**
* Handle and store any images attached to request
@ -77,14 +92,22 @@ class ImageUploadRequest extends Request
\Log::debug('Form fieldname is: '.$form_fieldname);
\Log::debug('DB fieldname is: '.$use_db_field);
\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($this->file());
if ($this->hasFile($form_fieldname)) {
if (isset($image)) {
\Log::debug($image);
if (!config('app.lock_passwords')) {
$image = $this->file($form_fieldname);
$ext = $image->getClientOriginalExtension();
$file_name = $type.'-'.$form_fieldname.'-'.str_random(10).'.'.$ext;
@ -165,4 +188,5 @@ class ImageUploadRequest extends Request
return $item;
}
}

View file

@ -0,0 +1,112 @@
<?php
namespace App\Http\Traits;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\FileBag;
use Symfony\Component\HttpFoundation\ParameterBag;
trait ConvertsBase64ToFiles
{
protected function base64FileKeys(): array
{
return [];
}
/**
* Helper method to get the body parameters bag.
*
* @return \Symfony\Component\HttpFoundation\ParameterBag
*/
private function bodyParametersBag(): ParameterBag
{
return $this->request;
}
/**
* Helper method to get the uploaded files bag.
*
* @return FileBag
*/
private function uploadFilesBag(): FileBag
{
return $this->files;
}
/**
* Pulls the Base64 contents for each file key and creates
* an UploadedFile instance from it and sets it on the
* request.
*
* @return void
*/
protected function prepareForValidation()
{
$flattened = Arr::dot($this->base64FileKeys());
Collection::make($flattened)->each(function ($filename, $key) {
rescue(function () use ($key, $filename) {
// dont process plain files
if ( $this->file($key)){
return;
}
$base64Contents = $this->input($key);
if (!$base64Contents) {
return;
}
// autogenerate filenames
if ($filename == 'auto'){
$header = explode(';', $base64Contents, 2)[0];
// Grab the image type from the header while we're at it.
$filename = $key . '.' . substr($header, strpos($header, '/')+1);
}
// Generate a temporary path to store the Base64 contents
$tempFilePath = tempnam(sys_get_temp_dir(), $filename);
// Store the contents using a stream, or by decoding manually
if (Str::startsWith($base64Contents, 'data:') && count(explode(',', $base64Contents)) > 1) {
$source = fopen($base64Contents, 'r');
$destination = fopen($tempFilePath, 'w');
stream_copy_to_stream($source, $destination);
fclose($source);
fclose($destination);
} else {
file_put_contents($tempFilePath, base64_decode($base64Contents, true));
}
$uploadedFile = new UploadedFile($tempFilePath, $filename, null, null, true);
\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);
});
}
}
/**
* This file based on https://github.com/protonemedia/laravel-mixins/tree/master/src/Request
*
* MIT License
*
* Copyright (c) Protone Media B.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/