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; }
/** * @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; }
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); }
/** * @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; }
/** * @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; }
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; }
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; }
/** * @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; }