snipe-it/app/Http/Traits/ConvertsBase64ToFiles.php

81 lines
3 KiB
PHP
Raw Normal View History

2021-07-02 00:17:29 -07:00
<?php
2021-07-02 00:18:18 -07:00
namespace App\Http\Traits;
2021-07-02 00:17:29 -07:00
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;
2021-07-02 00:17:29 -07:00
trait ConvertsBase64ToFiles
{
protected function base64FileKeys(): array
{
return [];
}
/**
* 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);
2021-07-05 20:25:37 -07:00
$this->offsetUnset($key);
Log::debug("Trait: encoded field \"$key\" removed" );
2021-07-05 20:25:37 -07:00
//Inserting new file to $this-files does not work so have to deal this after
$this->offsetSet($key,$uploadedFile);
Log::debug("Trait: field \"$key\" inserted as UplodedFile" );
2021-07-05 20:25:37 -07:00
2021-07-02 00:17:29 -07:00
}, null, false);
});
}
}
/**
* Loosely based on idea https://github.com/protonemedia/laravel-mixins/tree/master/src/Request
* */