protected function action()
 {
     $headers = ['Content-Type' => $this->resourceDO->getMimeType()];
     $filePath = $this->resourceDO->getFilePath();
     $filename = $this->resourceDO->getName() . '.' . $this->resourceDO->getType();
     if ($this->filesystem->has($filePath)) {
         return $this->XAccelRedirect(realpath($filePath), $filename, false);
     }
     /** @see \Zend\Diactoros\Response::$phrases */
     return new EmptyResponse(404, $headers);
 }
 protected function action()
 {
     $headers = ['Content-Type' => $this->resourceDO->getMimeType()];
     $destroy = PrepareResourceMiddlewareAbstract::getParamFromRequest('destroy', $this->request);
     if ($destroy) {
         $command = new DestroyResourceCommand($this->resourceDO, $this->filesystem);
         $command();
     } else {
         $command = new DeleteSafetyResourceCommand($this->resourceDO, $this->filesystem);
         $command();
     }
     /** @see \Zend\Diactoros\Response::$phrases */
     return new EmptyResponse(204, $headers);
 }
 /**
  * @param ResourceDOInterface $resourceDO
  * @param string $uri
  * @return DownloadedFile
  * @throws ErrorException
  * @throws \Exception
  */
 protected function download(ResourceDOInterface $resourceDO, $uri)
 {
     // ------------
     // @todo refactoring: move downloading code from here to separate service!
     // ------------
     set_time_limit(self::CURL_TIMEOUT);
     $dir = DATA_DIR . 'download' . DIRECTORY_SEPARATOR;
     $file = $this->resourceDO->getUuid() . '_' . time() . '_' . mt_rand(100, 200) . '.tmp';
     if (!@mkdir($dir) && !is_dir($dir)) {
         throw new ErrorException('Can\'t create the directory: ' . $dir);
     }
     if (is_file($file)) {
         if (!unlink($file)) {
             throw new ErrorException('Can\'t remove old file: ' . $dir . $file);
         }
     }
     $resource = fopen($dir . $file, 'w+');
     if (!$resource) {
         throw new ErrorException('Can\'t create the file for writting: ' . $dir . $file);
     }
     $uriEnc = str_replace(' ', '%20', $uri);
     $headers = ["Accept: " . $resourceDO->getMimeType(), "Cache-Control: no-cache", "Pragma: no-cache"];
     $curlHandle = curl_init($uriEnc);
     if (!$curlHandle) {
         throw new ErrorException('Curl error for uri: ' . $uri . ': cannot create resource');
     }
     curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curlHandle, CURLOPT_TIMEOUT, static::CURL_TIMEOUT);
     // Save curl result to the file
     curl_setopt($curlHandle, CURLOPT_FILE, $resource);
     curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($curlHandle, CURLOPT_MAXREDIRS, 2);
     curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
     // get curl response
     curl_exec($curlHandle);
     if (curl_errno($curlHandle)) {
         $error = curl_error($curlHandle);
         curl_close($curlHandle);
         fclose($resource);
         throw new ErrorException('Curl error for uri: ' . $uri . '; ' . $error);
     }
     $size = (int) curl_getinfo($curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
     curl_close($curlHandle);
     fclose($resource);
     // ------------
     $downloaded = new DownloadedFile($dir . $file, $size, UPLOAD_ERR_OK, $resourceDO->getName() . '.' . $resourceDO->getType(), $resourceDO->getMimeType());
     return $downloaded;
 }
 protected function isExist()
 {
     $filePath = realpath($this->resourceDO->getFilePath());
     return $this->filesystem->has($filePath);
 }