/**
  * Post a new media and return it's temporary identifier
  *
  * @param Request $request
  *
  * @return JsonResponse
  */
 public function postAction(Request $request)
 {
     $file = $request->files->get('file');
     $violations = $this->validator->validate($file);
     if (count($violations) > 0) {
         $errors = [];
         foreach ($violations as $violation) {
             $errors[$violation->getPropertyPath()] = ['message' => $violation->getMessage(), 'invalid_value' => $violation->getInvalidValue()];
         }
         return new JsonResponse($errors, 400);
     }
     $pathData = $this->pathGenerator->generate($file);
     try {
         $movedFile = $file->move($this->uploadDir . DIRECTORY_SEPARATOR . $pathData['path'], $pathData['file_name']);
     } catch (FileException $e) {
         //TODO: a message goes here
         return new JsonResponse(null, 400);
     }
     return new JsonResponse(['originalFilename' => $file->getClientOriginalName(), 'filePath' => $movedFile->getPathname()]);
 }
 /**
  * Post a new media and return it's temporary identifier
  *
  * @param Request $request
  *
  * @return JsonResponse
  */
 public function postAction(Request $request)
 {
     /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
     $file = $request->files->get('file');
     $violations = $this->validator->validate($file);
     if (count($violations) > 0) {
         $errors = [];
         foreach ($violations as $violation) {
             $errors[$violation->getPropertyPath()] = ['message' => $violation->getMessage(), 'invalid_value' => $violation->getInvalidValue()];
         }
         return new JsonResponse($errors, 400);
     }
     $pathData = $this->pathGenerator->generate($file);
     try {
         $movedFile = $file->move($this->uploadDir . DIRECTORY_SEPARATOR . $pathData['path'], $pathData['file_name']);
     } catch (FileException $e) {
         //TODO: more specific message if debug mode is on?
         return new JsonResponse("Unable to create target-directory, or moving file.", 400);
     }
     return new JsonResponse(['originalFilename' => $file->getClientOriginalName(), 'filePath' => $movedFile->getPathname()]);
 }
 /**
  * {@inheritdoc}
  */
 public function store(\SplFileInfo $localFile, $destFsAlias)
 {
     $filesystem = $this->mountManager->getFilesystem($destFsAlias);
     $storageData = $this->pathGenerator->generate($localFile);
     $file = $this->factory->create($localFile, $storageData, $destFsAlias);
     $error = sprintf('Unable to move the file "%s" to the "%s" filesystem.', $localFile->getPathname(), $destFsAlias);
     if (false === ($resource = fopen($localFile->getPathname(), 'r'))) {
         throw new FileTransferException($error);
     }
     try {
         $isFileWritten = $filesystem->writeStream($file->getKey(), $resource);
     } catch (FileExistsException $e) {
         throw new FileTransferException($error, $e->getCode(), $e);
     }
     if (false === $isFileWritten) {
         throw new FileTransferException($error);
     }
     $this->saver->save($file, ['flush_only_object' => true]);
     $this->deleteRawFile($localFile);
     return $file;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function createFromRawFile(\SplFileInfo $rawFile, $destFsAlias)
 {
     $pathInfo = $this->pathGenerator->generate($rawFile);
     $sha1 = sha1_file($rawFile->getPathname());
     if ($rawFile instanceof UploadedFile) {
         $originalFilename = $rawFile->getClientOriginalName();
         $extension = $rawFile->getClientOriginalExtension();
     } else {
         $originalFilename = $rawFile->getFilename();
         $extension = $rawFile->getExtension();
     }
     $size = filesize($rawFile->getPathname());
     $mimeType = MimeTypeGuesser::getInstance()->guess($rawFile->getPathname());
     $file = new $this->fileClass();
     $file->setKey($pathInfo['path'] . $pathInfo['file_name']);
     $file->setMimeType($mimeType);
     $file->setOriginalFilename($originalFilename);
     $file->setSize($size);
     $file->setExtension($extension);
     $file->setHash($sha1);
     $file->setStorage($destFsAlias);
     return $file;
 }