/**
  * Handle the upload request.
  *
  * @param Request                               $request
  * @param \Symfony\Component\Form\FormInterface $form
  * @param array                                 $config
  *
  * @throws \SRIO\RestUploadBundle\Exception\UploadException
  * @return UploadResult
  */
 public function handleRequest(Request $request, FormInterface $form = null, array $config = array())
 {
     try {
         $processor = $this->getProcessor($request, $config);
         return $processor->handleUpload($request, $form, $config);
     } catch (UploadException $e) {
         if ($form != null) {
             $form->addError(new FormError($e->getMessage()));
         }
         $result = new UploadResult();
         $result->setException($e);
         $result->setForm($form);
         return $result;
     }
 }
 /**
  * {@inheritDoc}
  *
  * @param  Request                                                     $request
  * @throws \Exception|\SRIO\RestUploadBundle\Exception\UploadException
  * @return \SRIO\RestUploadBundle\Upload\UploadResult
  */
 public function handleRequest(Request $request)
 {
     // Check that needed headers exists
     $this->checkHeaders($request);
     // Create the response
     $result = new UploadResult();
     $result->setRequest($request);
     $result->setConfig($this->config);
     $result->setForm($this->form);
     // Submit form data
     if ($this->form != null) {
         // Get formData
         $formData = $this->getFormData($request);
         $formData = $this->createFormData($formData);
         $this->form->submit($formData);
     }
     if ($this->form === null || $this->form->isValid()) {
         list($contentType, $content) = $this->getContent($request);
         $file = $this->storageHandler->store($result, $content, array(FileStorage::METADATA_CONTENT_TYPE => $contentType));
         $result->setFile($file);
     }
     return $result;
 }
 /**
  * @param  \Symfony\Component\HttpFoundation\Request                   $request
  * @throws \Exception|\SRIO\RestUploadBundle\Exception\UploadException
  * @param  Request                                                     $request
  * @return UploadResult
  */
 public function handleRequest(Request $request)
 {
     // Check that needed headers exists
     $this->checkHeaders($request, array('Content-Type'));
     $result = new UploadResult();
     $result->setForm($this->form);
     $result->setRequest($request);
     $result->setConfig($this->config);
     // Submit form data
     if ($this->form !== null) {
         $formData = $this->createFormData($request->query->all());
         $this->form->submit($formData);
     }
     if ($this->form == null || $this->form->isValid()) {
         $content = $request->getContent();
         // Nothing to store
         if (empty($content)) {
             throw new UploadException('There is no content to upload');
         }
         $file = $this->storageHandler->store($result, $content, array(FileStorage::METADATA_CONTENT_TYPE => $request->headers->get('Content-Type')));
         $result->setFile($file);
     }
     return $result;
 }
 /**
  * @param  Request                                                     $request
  * @return \SRIO\RestUploadBundle\Upload\UploadResult
  * @throws \Exception|\SRIO\RestUploadBundle\Exception\UploadException
  */
 public function handleRequest(Request $request)
 {
     // Check that needed headers exists
     $this->checkHeaders($request, array('Content-Length', 'Content-Type'));
     if (!$request->files->has($this->config[self::KEY_FIELD_FILE])) {
         throw new UploadException(sprintf('%s file not found', $this->config[self::KEY_FIELD_FILE]));
     }
     $response = new UploadResult();
     $response->setRequest($request);
     $response->setConfig($this->config);
     if ($this->form != null) {
         $response->setForm($this->form);
         if (!$request->request->has($this->config[self::KEY_FIELD_FORM])) {
             throw new UploadException(sprintf('%s request field not found in (%s)', $this->config[self::KEY_FIELD_FORM], implode(', ', $request->request->keys())));
         }
         $submittedValue = $request->request->get($this->config[self::KEY_FIELD_FORM]);
         if (is_string($submittedValue)) {
             $submittedValue = json_decode($submittedValue, true);
             if (!$submittedValue) {
                 throw new UploadException('Unable to decode JSON');
             }
         } elseif (!is_array($submittedValue)) {
             throw new UploadException('Unable to parse form data');
         }
         // Submit form data
         $formData = $this->createFormData($submittedValue);
         $this->form->submit($formData);
         if (!$this->form->isValid()) {
             return $response;
         }
     }
     /** @var $uploadedFile \Symfony\Component\HttpFoundation\File\UploadedFile */
     $uploadedFile = $request->files->get($this->config[self::KEY_FIELD_FILE]);
     $contents = file_get_contents($uploadedFile->getPathname());
     $file = $this->storageHandler->store($response, $contents, array(FileStorage::METADATA_CONTENT_TYPE => $uploadedFile->getMimeType()));
     $response->setFile($file);
     return $response;
 }
 /**
  * Return the upload status.
  *
  * @param  \SRIO\RestUploadBundle\Upload\UploadContext $context
  * @param  ResumableUploadSession                      $uploadSession
  * @param  \Gaufrette\File                             $file
  * @param  array                                       $range
  * @return UploadResult
  */
 protected function requestUploadStatus(UploadContext $context, ResumableUploadSession $uploadSession, File $file, array $range)
 {
     if (!$file->exists()) {
         $length = 0;
     } else {
         $length = $file->getSize();
     }
     $response = new Response(null, $length == $range['total'] ? 201 : 308);
     if ($length < 1) {
         $length = 1;
     }
     $response->headers->set('Range', '0-' . ($length - 1));
     $result = new UploadResult();
     $result->setResponse($response);
     return $result;
 }