File: D:/HostingSpaces/SBogers10/conmeq.komma.pro/app/Komma/Html5Upload/Html5UploadApiController.php
<?php
namespace App\Komma\Html5Upload;
use App\Komma\Documents\Kms\DocumentServiceInterface;
use App\Komma\Documents\Models\Document;
use App\Komma\Kms\Core\Attributes\Models\ImageProperty;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\FileBag;
final class HTML5UploadApiController extends Controller
{
/** @var DocumentServiceInterface */
private $documentService;
public function __construct()
{
$this->documentService = app(DocumentServiceInterface::class);
}
/**
* This method is used to upload files viaHTML 5 formdata uploads via ajax request
* Creates HTML5UploadTrackerModel instances for each uploaded file from the request.
* And then returns an array where the keys are HTML
*
* @param Request $request
* @return array
*/
function uploadedFile(Request $request) {
//Validate that is ajax request
if(!$request->ajax()) abort(400, 'This url should only be accessed via ajax requests. Set the X-Requested-With header and make sure you are POSTing to this url');
//Get the files from the request
/** @var FileBag $files */
$files = $request->file();
//Check that we received files. Else log a warning.
if(count($files) === 0) {
$message = 'HTML5UploadApiController: I suspect a too big file was uploaded since i did not get any. Please check php.ini\'s upload_max_filesize and post_max_size variables or manage them in your .htaccess / .webconfig. '.
'Also prevent calling the route that hits this controller method when the total amount of files exceeds those variables.';
Log::warning($message);
abort(400, $message);
}
//Check that there is an extraData field containing information about where to store the uploaded file and with which image properties
$extraData = $request->input('extraData');
if(!$extraData) abort(400, 'expected a formdata field with the name of extraData, containing a json object with information about image properties and the upload folder');
$extraDataArray = json_decode($extraData, true);
if(!isset($extraDataArray['imageProperties']) || !isset($extraDataArray['subFolder'])) abort(400, 'expected a formdata field with the name of extraData, containing a json object with information about image properties and the upload folder');
//Use extraData to get ImageProperty instances
$imagePropertiesAsArray = array_map(function($imagePropertyAsArray) {
return ImageProperty::fromArray($imagePropertyAsArray);
}, json_decode($extraDataArray['imageProperties'], true));
$documents = collect();
foreach($files as $file) {
/** @var UploadedFile $file */
$documents->push($this->documentService->storeHtml5Upload($file, $extraDataArray['subFolder'], $imagePropertiesAsArray));
}
return $documents->mapWithKeys(function (Document $document) {
return $document->toArray();
})->toArray();
}
}