Example #1
0
 /**
  * Compress a file and return the new file
  *
  * @param LocalFileNodeInterface $node
  * @param array                  $options
  *
  * @return LocalFileNodeInterface
  */
 public function compress(LocalFileNodeInterface $node, array $options = [])
 {
     $pathInfo = pathinfo($node->getPath());
     if (!$node->exists()) {
         throw new InvalidArgumentException("The file: {$node} does not exist");
     }
     $outputFile = $node->getClone()->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.' . $this->getExtension())->setCompression($this->getName());
     $this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", ['file' => $node, 'target' => $outputFile, 'compression' => $this->getName()]);
     $cmd = $this->getCompressCommand($node, $outputFile);
     $keepOld = isset($options['keepOldFile']) ? $options['keepOldFile'] : true;
     return $this->processFile($node, $outputFile, $cmd, $keepOld);
 }
Example #2
0
 /**
  * @param LocalFileNodeInterface $file
  * @param LocalFileNodeInterface $output
  * @param string                 $cmd
  * @param bool                   $keepOld
  *
  * @return LocalFileNodeInterface
  */
 protected function processFile(LocalFileNodeInterface $file, LocalFileNodeInterface $output, $cmd, $keepOld = true)
 {
     $process = $this->getProcess($cmd);
     $process->run();
     if (!$process->isSuccessful() || !$output->exists()) {
         throw new ProcessFailedException($process);
     }
     if ($file->exists() && !$keepOld) {
         $this->log(LogLevel::DEBUG, "Deleting old file: '{file}'", ['file' => $file]);
         $file->delete();
     }
     return $output;
 }