/** * @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]; }
public function testController() { $namespace = 'BoomCMS\\Http\\Controllers\\Asset\\'; $asset = $this->getMock(Asset::class, ['getExtension']); $asset->expects($this->any())->method('getExtension')->will($this->returnValue(null)); $this->assertEquals(null, AssetHelper::controller($asset)); $pdf = $this->getMock(Asset::class, ['getExtension']); $pdf->expects($this->any())->method('getExtension')->will($this->returnValue('pdf')); $this->assertEquals($namespace . 'Pdf', AssetHelper::controller($pdf)); $image = $this->getMock(Asset::class, ['getType', 'getExtension']); $image->expects($this->any())->method('getExtension')->will($this->returnValue('png')); $image->expects($this->any())->method('getType')->will($this->returnValue('image')); $this->assertEquals($namespace . 'Image', AssetHelper::controller($image)); $tiff = $this->getMock(Asset::class, ['getType', 'getExtension']); $tiff->expects($this->any())->method('getType')->will($this->returnValue('image')); $tiff->expects($this->any())->method('getExtension')->will($this->returnValue('tiff')); $this->assertEquals($namespace . 'Tiff', AssetHelper::controller($tiff)); $video = $this->getMock(Asset::class, ['getType', 'getExtension']); $video->expects($this->any())->method('getExtension')->will($this->returnValue('mp4')); $video->expects($this->any())->method('getType')->will($this->returnValue('video')); $this->assertEquals($namespace . 'Video', AssetHelper::controller($video)); }
public function removeInvalidTypes(array $types) { $validTypes = array_keys(Asset::types()); return array_intersect($validTypes, $types); }
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]; }