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; }
protected function action() { $headers = ['Content-Type' => $this->resourceDO->getMimeType()]; $filePath = $this->resourceDO->getFilePath(); $fileExists = is_file($filePath); $recreate = PrepareResourceMiddlewareAbstract::getParamFromRequest(static::RECREATE_COMMAND, $this->request); $uri = PrepareResourceMiddlewareAbstract::getParamFromRequest(static::URI_COMMAND, $this->request); $recreate = $fileExists && $recreate; $this->resourceDO->setNew(!$fileExists); if (!$fileExists || $recreate) { $this->resourceDO->setRecreate($recreate); $upload = $this->upload(); // Upload must be with high priority if ($upload) { /** @see \Zend\Diactoros\Response::$phrases */ return new FileUploadedResponse($upload, 201, $headers); } elseif ($uri) { $upload = $this->download($this->resourceDO, $uri); /** @see \Zend\Diactoros\Response::$phrases */ return new FileUploadedResponse($upload, 201, $headers); } else { $body = $this->generate($this->resourceDO); /** @see \Zend\Diactoros\Response::$phrases */ return new FileContentResponse($body, 201, $headers); } } /** @see \Zend\Diactoros\Response::$phrases */ return new EmptyResponse(304, $headers); }
/** * @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 isExist() { $filePath = realpath($this->resourceDO->getFilePath()); return $this->filesystem->has($filePath); }
/** * @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; }