mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
Try without trait
This commit is contained in:
parent
948a741935
commit
2325b1d8c2
|
@ -10,7 +10,6 @@ use Storage;
|
||||||
|
|
||||||
class ImageUploadRequest extends Request
|
class ImageUploadRequest extends Request
|
||||||
{
|
{
|
||||||
use ConvertsBase64ToFiles;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the user is authorized to make this request.
|
* Determine if the user is authorized to make this request.
|
||||||
|
@ -40,7 +39,10 @@ class ImageUploadRequest extends Request
|
||||||
{
|
{
|
||||||
return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
|
return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fields that should be traited from base64 to files
|
||||||
|
*/
|
||||||
protected function base64FileKeys(): array
|
protected function base64FileKeys(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
@ -161,4 +163,86 @@ class ImageUploadRequest extends Request
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
$body = $this->bodyParametersBag()->all();
|
||||||
|
Arr::forget($body, $key);
|
||||||
|
$this->bodyParametersBag()->replace($body);
|
||||||
|
|
||||||
|
$files = $this->uploadFilesBag()->all();
|
||||||
|
Arr::set($files, $key, $uploadedFile);
|
||||||
|
$this->uploadFilesBag()->replace($files);
|
||||||
|
}, null, false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue