file upload to storage
public upload ( Symfony\Component\HttpFoundation\File\UploadedFile $uploaded, string $path, string | null $name = null, string | null $disk = null, Xpressengine\User\UserInterface $user = null ) : |
||
$uploaded | Symfony\Component\HttpFoundation\File\UploadedFile | uploaded file instance |
$path | string | be saved path |
$name | string | null | be saved file name |
$disk | string | null | disk name (ex. local, ftp, s3 ...) |
$user | Xpressengine\User\UserInterface | user instance |
Результат |
public function testUploadThrownExceptionWhenFileIsInvalid() { list($handler, $repo, $auth, $keygen) = $this->getMocks(); $uploaded = m::mock('Symfony\\Component\\HttpFoundation\\File\\UploadedFile'); $uploaded->shouldReceive('isValid')->andReturn(false); $uploaded->shouldReceive('getClientOriginalName')->andReturn('foo.jpg'); $instance = new Storage($handler, $repo, $auth, $keygen); try { $instance->upload($uploaded, 'attached'); $this->assertTrue(false); } catch (\Exception $e) { $this->assertInstanceOf('Xpressengine\\Storage\\Exceptions\\InvalidFileException', $e); } }
public function testUploadThrownExceptionWhenWritingFail() { list($handler, $auth, $keygen, $distributor, $temps) = $this->getMocks(); $instance = new Storage($handler, $auth, $keygen, $distributor, $temps); $resource = file_get_contents(__DIR__ . '/sample.png'); $mockGuest = m::mock('Xpressengine\\Member\\Entities\\Guest'); $mockGuest->shouldReceive('getId')->andReturnNull(); $auth->shouldReceive('user')->andReturn($mockGuest); $uploaded = m::mock('Symfony\\Component\\HttpFoundation\\File\\UploadedFile'); $uploaded->shouldReceive('isValid')->andReturn(true); $uploaded->shouldReceive('getClientOriginalName')->andReturn('foo.jpg'); $uploaded->shouldReceive('getPathname')->andReturn(__DIR__ . '/sample.png'); $keygen->shouldReceive('generate')->once()->andReturn('made-key'); $distributor->shouldReceive('allot')->once()->with($uploaded)->andReturn('local'); $handler->shouldReceive('store')->once()->with($resource, m::on(function () { return true; }), 'local')->andReturn(false); try { $instance->upload($uploaded, 'attached'); $this->assertTrue(false); } catch (\Exception $e) { $this->assertInstanceOf('Xpressengine\\Storage\\Exceptions\\WritingFailException', $e); } }
/** * file upload * * @param Request $request request * @param EditorHandler $handler editor handler * @param Storage $storage storage * @param MediaManager $mediaManager media manager * @param string $instanceId instance id * @return RendererInterface */ public function fileUpload(Request $request, EditorHandler $handler, Storage $storage, MediaManager $mediaManager, $instanceId) { $uploadedFile = null; if ($request->file('file') !== null) { $uploadedFile = $request->file('file'); } elseif ($request->file('image') !== null) { $uploadedFile = $request->file('image'); } if ($uploadedFile === null) { throw new InvalidArgumentException(); } $config = $handler->get($instanceId)->getConfig(); if (!$config->get('uploadActive') || Gate::denies('upload', new Instance($handler->getPermKey($instanceId)))) { throw new AccessDeniedHttpException(); } if ($config->get('fileMaxSize') * 1024 * 1024 < $uploadedFile->getSize()) { throw new HttpException(Response::HTTP_REQUEST_ENTITY_TOO_LARGE, xe_trans('xe::msgMaxFileSize', ['fileMaxSize' => $config->get('fileMaxSize'), 'uploadFileName' => $uploadedFile->getClientOriginalName()])); } $extensions = array_map(function ($v) { return trim($v); }, explode(',', $config->get('extensions', ''))); if (array_search('*', $extensions) === false && !in_array(strtolower($uploadedFile->getClientOriginalExtension()), $extensions)) { throw new HttpException(Response::HTTP_NOT_ACCEPTABLE, xe_trans('xe::msgAvailableUploadingFiles', ['extensions' => $config->get('extensions'), 'uploadFileName' => $uploadedFile->getClientOriginalName()])); } $file = $storage->upload($uploadedFile, EditorHandler::FILE_UPLOAD_PATH); $media = null; $thumbnails = null; if ($mediaManager->is($file) === true) { $media = $mediaManager->make($file); $thumbnails = $mediaManager->createThumbnails($media, EditorHandler::THUMBNAIL_TYPE); $media = $media->toArray(); if (!empty($thumbnails)) { $info['thumbnails'] = $thumbnails; } } return XePresenter::makeApi(['file' => $file->toArray(), 'media' => $media, 'thumbnails' => $thumbnails]); }