public function testTypeFromMimetype() { foreach ($this->types as $type => $mimetypes) { foreach ($mimetypes as $mimetype) { $this->assertEquals($type, AssetHelper::typeFromMimetype($mimetype)); } } }
/** * @param Request $request * * @return JsonResponse|array */ public function store(Request $request, Site $site) { $this->authorize('uploadAssets', $site); $assetIds = []; list($validFiles, $errors) = $this->validateAssetUpload($request); foreach ($validFiles as $file) { $asset = new Asset(); $asset->setTitle($file->getClientOriginalName())->setType(AssetHelper::typeFromMimetype($file->getMimeType())); $assetIds[] = AssetFacade::save($asset)->getId(); AssetFacade::createVersionFromFile($asset, $file); } return count($errors) ? new JsonResponse($errors, 500) : $assetIds; }
protected function validateAssetUpload(Request $request) { $validFiles = $errors = []; foreach ($request->file() as $files) { foreach ($files as $file) { if (!$file->isValid()) { $errors[] = $file->getErrorMessage(); continue; } $type = AssetHelper::typeFromMimetype($file->getMimetype()); if ($type === null) { $errors[] = Lang::get('boomcms::asset.unsupported', ['filename' => $file->getClientOriginalName(), 'mimetype' => $file->getMimetype()]); continue; } $validFiles[] = $file; } } return [$validFiles, $errors]; }
protected function validateFileUpload() { $validFiles = $errors = []; foreach ($this->request->file() as $files) { foreach ($files as $i => $file) { if (!$file->isValid()) { $errors[] = $file->getErrorMessage(); continue; } $type = AssetHelper::typeFromMimetype($file->getMimetype()); if ($type === null) { $errors[] = "File {$file->getClientOriginalName()} is of an unsuported type: {$file->getMimetype()}"; continue; } $validFiles[] = $file; } } return [$validFiles, $errors]; }