コード例 #1
0
 /**
  * @return ResourceDOInterface SuspectResource if it have been deleted or OriginResource if the Suspect is not equal
  */
 public function __invoke()
 {
     $originName = $this->originResourceDO->getName();
     $suspectName = $this->suspectResourceDO->getName();
     $originType = $this->originResourceDO->getType();
     $suspectType = $this->suspectResourceDO->getType();
     $originFilePath = $this->originResourceDO->getFilePath();
     $suspectFilePath = $this->suspectResourceDO->getFilePath();
     if (!$originName || !$originType) {
         throw new CommandErrorException('Cannot destroy equal resource: the origin resource is empty');
     }
     if (!$suspectName || !$suspectType) {
         throw new CommandErrorException('Cannot destroy equal resource: the suspect resource is empty');
     }
     if ($originFilePath === $suspectFilePath) {
         throw new CommandErrorException('Cannot destroy equal resource: Origin and Suspect have same paths');
     }
     // Unfortunately, this condition can not always work fine.
     // Because some Middlewares can compress, resize etc. the resource that saved before
     // and the second uploaded copy will never be equal
     if ($originType === $suspectType && $this->filesystem->has($originFilePath) === $this->filesystem->has($suspectFilePath) && $this->filesystem->getSize($originFilePath) === $this->filesystem->getSize($suspectFilePath) && $this->getFileHash($originFilePath) === $this->getFileHash($suspectFilePath)) {
         $command = new DestroyResourceCommand($this->suspectResourceDO, $this->filesystem);
         return $command(true);
     }
     return $this->originResourceDO;
 }
コード例 #2
0
 /**
  * @param string $message
  * @param integer $code
  * @param null|ResourceDOInterface $resourceDO
  * @return array
  */
 protected function getErrorArray($message, $code, ResourceDOInterface $resourceDO = null)
 {
     $error = ['error' => ['title' => $message, 'code' => $code]];
     if ($resourceDO) {
         $error['error']['detail']['resource'] = $resourceDO->toArray();
     }
     return $error;
 }
コード例 #3
0
 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);
 }
コード例 #4
0
 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);
 }
コード例 #5
0
ファイル: ActionPost.php プロジェクト: kivagant/staticus
 /**
  * @param ResourceDOInterface|ResourceImageDOInterface $resourceDO
  * @return mixed
  */
 protected function generate(ResourceDOInterface $resourceDO)
 {
     // Do not generate image when resizing or cropping is requested
     if ($this->resourceDO->getDimension()) {
         // If recreation for current size is asked, just remove previous file
         // Without this next middlewares will try to resize exist file
         if ($this->resourceDO->isRecreate()) {
             $this->filesystem->delete($this->resourceDO->getFilePath());
         }
         return null;
     }
     $query = $resourceDO->getName() . ' ' . $resourceDO->getNameAlternative();
     $content = $this->generator->generate($query);
     return $content;
 }
コード例 #6
0
ファイル: ActionPost.php プロジェクト: kivagant/staticus
 /**
  * @param ResourceDOInterface $resourceDO
  * @return mixed
  * @throws ErrorException
  */
 protected function generate(ResourceDOInterface $resourceDO)
 {
     /** @var Manager $generator */
     $generator = $this->generator;
     // If a body is exist, it will be used as the text for voicing
     $body = $resourceDO->getBody();
     // If an alternative name is exist, it will be used when body is empty
     $alternative = $resourceDO->getNameAlternative();
     // The main resource name will be used, if no body and no alt exist
     $voiceText = $body ?: ($alternative ?: $resourceDO->getName());
     $this->selectLanguage($voiceText);
     $content = $generator->read($voiceText);
     $headers = $generator->getHeaders();
     if (!array_key_exists('http_code', (array) $headers) || $headers['http_code'] != 200) {
         throw new ErrorException('Wrong http response code from voice provider ' . get_class($this->generator->getAdapter()) . ': ' . $headers['http_code'] . '; Requested text: ' . $resourceDO->getName());
     }
     return $content;
 }
コード例 #7
0
 public function __invoke()
 {
     $version = $this->resourceDO->getVersion();
     $filePath = $this->resourceDO->getFilePath();
     if (!$this->resourceDO->getName() || !$this->resourceDO->getType() || !$this->resourceDO->getBaseDirectory()) {
         throw new CommandErrorException('Cannot delete empty resource');
     }
     if ($this->filesystem->has($filePath)) {
         // Make backup of the default version
         if (ResourceDOInterface::DEFAULT_VERSION === $version) {
             $lastVersion = $this->findLastVersion();
             // But only if previous existing version is not the default and not has the same content as deleting
             if (ResourceDOInterface::DEFAULT_VERSION !== $lastVersion) {
                 $lastVersionResourceDO = clone $this->resourceDO;
                 $lastVersionResourceDO->setVersion($lastVersion);
                 $command = new DestroyEqualResourceCommand($lastVersionResourceDO, $this->resourceDO, $this->filesystem);
                 $result = $command();
                 if ($result === $this->resourceDO) {
                     // If the previous file version already the same, current version is already deleted
                     // and backup and yet another deletion is not needed anymore
                     return $this->resourceDO;
                 }
             }
             $command = new BackupResourceCommand($this->resourceDO, $this->filesystem);
             $command($lastVersion);
         }
         $this->deleteFile($filePath);
         return $this->resourceDO;
     }
     return $this->resourceDO;
 }
コード例 #8
0
 /**
  * @param bool $replace Replace exist file or just do nothing
  * @return ResourceDOInterface
  * @throws CommandErrorException
  */
 public function __invoke($replace = false)
 {
     if (!$this->originResourceDO->getName() || !$this->originResourceDO->getType()) {
         throw new CommandErrorException('Source resource cannot be empty');
     }
     if (!$this->newResourceDO->getName() || !$this->newResourceDO->getType()) {
         throw new CommandErrorException('Destination resource cannot be empty');
     }
     $originPath = $this->originResourceDO->getFilePath();
     $newPath = $this->newResourceDO->getFilePath();
     if ($originPath === $newPath) {
         throw new CommandErrorException('Source and destination paths is equal');
     }
     if (!$this->filesystem->has($originPath)) {
         throw new CommandErrorException('Origin file is not exists: ' . $originPath);
     }
     $exists = $this->filesystem->has($newPath);
     if (!$exists || $replace) {
         $this->copyFile($originPath, $newPath, $exists && $replace);
         return $this->newResourceDO;
     }
     return $this->originResourceDO;
 }
コード例 #9
0
 protected function getUri(ResourceDOInterface $resourceDO)
 {
     $uri = $resourceDO->getName() . '.' . $resourceDO->getType();
     $query = [];
     if (ResourceDOInterface::DEFAULT_VARIANT !== $resourceDO->getVariant()) {
         $query['var'] = $resourceDO->getVariant();
     }
     if (ResourceDOInterface::DEFAULT_NAME_ALTERNATIVE !== $resourceDO->getNameAlternative()) {
         $query['alt'] = $resourceDO->getNameAlternative();
     }
     if (ResourceDOInterface::DEFAULT_VERSION !== $resourceDO->getVersion()) {
         $query['v'] = $resourceDO->getVersion();
     }
     $query = http_build_query($query, null, '&', PHP_QUERY_RFC3986);
     // RFC for correct spaces
     if ($query) {
         $uri .= '?' . $query;
     }
     return $uri;
 }
コード例 #10
0
 /**
  * @param bool $byPathOnly If true, no search on disk will be executed
  * @return ResourceDOInterface
  */
 public function __invoke($byPathOnly = false)
 {
     $uuid = $this->resourceDO->getUuid();
     $type = $this->resourceDO->getType();
     $variant = $this->resourceDO->getVariant();
     $version = $this->resourceDO->getVersion();
     $baseDir = $this->resourceDO->getBaseDirectory();
     $namespace = $this->resourceDO->getNamespace();
     $filePath = $this->resourceDO->getFilePath();
     if (!$uuid || !$type || !$baseDir || !$filePath) {
         throw new CommandErrorException('Cannot destroy the empty resource');
     }
     if ($byPathOnly) {
         $this->deleteFile($filePath);
     } else {
         $command = new FindResourceOptionsCommand($this->resourceDO, $this->filesystem);
         $result = $command();
         foreach ($result as $item) {
             if ($item[ResourceDOAbstract::TOKEN_TYPE] !== $type || $item['filename'] !== $uuid || $namespace && $item[ResourceDOAbstract::TOKEN_NAMESPACE] !== $namespace) {
                 continue;
             }
             if ($version !== ResourceDOInterface::DEFAULT_VERSION) {
                 if ($variant === $item[ResourceDOAbstract::TOKEN_VARIANT] && $version === (int) $item[ResourceDOAbstract::TOKEN_VERSION]) {
                     $this->deleteFile($item['path']);
                 }
             } elseif ($variant !== ResourceDOInterface::DEFAULT_VARIANT) {
                 if ($variant === $item[ResourceDOAbstract::TOKEN_VARIANT]) {
                     $this->deleteFile($item['path']);
                 }
             } else {
                 $this->deleteFile($item['path']);
             }
         }
     }
     return $this->resourceDO;
 }
コード例 #11
0
 protected function findAllResourceOptions(ResourceDOInterface $resourceDO)
 {
     /** @var \League\Flysystem\FilesystemInterface $filesystem */
     $filesystem = $this->filesystem;
     $uuid = $resourceDO->getUuid();
     $type = $resourceDO->getType();
     $name = $resourceDO->getName();
     if (!$name || !$type) {
         throw new CommandErrorException('Can not look for options: resource is empty');
     }
     $basename = $resourceDO->getBaseDirectory();
     $namespace = $resourceDO->getNamespace();
     $path = $basename . ($namespace ? $namespace . DIRECTORY_SEPARATOR : '') . $type . DIRECTORY_SEPARATOR;
     $found = $filesystem->listContents($path, true);
     $found = array_filter($found, function ($file) use($uuid, $type) {
         return array_key_exists('filename', $file) && $file['filename'] === $uuid && array_key_exists('extension', $file) && $file['extension'] === $type && array_key_exists('type', $file) && $file['type'] === 'file';
     });
     array_walk($found, [$this, 'hydrateElementFile'], ['resourceDO' => $resourceDO]);
     $found = array_values($found);
     // reset keys
     return $found;
 }
コード例 #12
0
 protected function destroyEqual(ResourceDOInterface $resourceDO, ResourceDOInterface $backupResourceVerDO)
 {
     /** @var ResourceImageDOInterface $resourceDO */
     return ResourceImageDO::DEFAULT_DIMENSION === $resourceDO->getDimension() ? parent::destroyEqual($resourceDO, $backupResourceVerDO) : $resourceDO;
 }
コード例 #13
0
 /**
  * @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;
 }
コード例 #14
0
 protected function isExist()
 {
     $filePath = realpath($this->resourceDO->getFilePath());
     return $this->filesystem->has($filePath);
 }
コード例 #15
0
 /**
  * @param ResourceDOInterface $resourceDO
  * @param string|resource|Stream $content
  * @return ResourceDOInterface
  * @throws \RuntimeException if the upload was not successful.
  * @throws \InvalidArgumentException if the $path specified is invalid.
  * @throws \RuntimeException on any error during the move operation, or on
  */
 protected function save(ResourceDOInterface $resourceDO, $content)
 {
     $backupResourceVerDO = null;
     $filePath = $resourceDO->getFilePath();
     $this->createDirectory(dirname($filePath));
     // backups don't needs if this is a 'new creation' command
     if ($resourceDO->isRecreate()) {
         $backupResourceVerDO = $this->backup($resourceDO);
     }
     if ($content instanceof UploadedFileInterface) {
         $this->uploadFile($content, $resourceDO->getMimeType(), $filePath);
     } else {
         $this->writeFile($filePath, $content);
     }
     $responseDO = $resourceDO;
     if ($backupResourceVerDO instanceof ResourceDOInterface && $backupResourceVerDO->getVersion() !== ResourceDOInterface::DEFAULT_VERSION) {
         // If the newly created file is the same as the previous version, remove backup immediately
         $responseDO = $this->destroyEqual($resourceDO, $backupResourceVerDO);
     }
     if ($responseDO === $resourceDO) {
         // cleanup postprocessing cache folders
         // - if it is a new file creation (remove possible garbage after other operations)
         // - or if the basic file is replaced and not equal to the previous version
         $this->afterSave($resourceDO);
     }
     if ($this->config->get('staticus.magic_defaults.allow')) {
         $this->copyFileToDefaults($resourceDO);
     }
     return $resourceDO;
 }