/** * Create a target file from a source file and postfix * * @param LocalFileNodeInterface $file * @param string $postfix * * @return LocalFileNodeInterface */ protected function getTargetFile(LocalFileNodeInterface $file, $postfix) { if (strlen($postfix) > 0) { $postfix = '-' . $postfix; } $pathInfo = pathinfo($file->getPath()); $extension = isset($pathInfo['extension']) ? '.' . $pathInfo['extension'] : ''; $path = sprintf('%s/%s%s%s', $pathInfo['dirname'], $pathInfo['filename'], $postfix, $extension); return $file->getClone()->setPath($path); }
/** * 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); }
/** * Find the Encoding of a specified file * * @param LocalFileNodeInterface $file * * @return null|string * @throws ProcessFailedException */ public function getEncoding(LocalFileNodeInterface $file) { $cmd = "file --brief --uncompress --mime {$file->getPath()}"; $process = $this->getProcess($cmd); $process->mustRun(); $result = $process->getOutput(); if (preg_match('/charset=([^\\s]+)/i', $result, $matches)) { $this->log(LogLevel::DEBUG, "Found the encoding for '{file}' as '{encoding}'", ['file' => $file, 'encoding' => $matches[1]]); return $matches[1]; } return null; }
/** * Find the compression of a file * * @param LocalFileNodeInterface $file * * @return string */ public function getCompression(LocalFileNodeInterface $file) { $cmd = "file --brief --uncompress --mime {$file->getPath()}"; $process = $this->getProcess($cmd); $process->mustRun(); $result = $process->getOutput(); if (preg_match('/compressed-encoding=application\\/(?:x-)?(.+?);/i', $result, $matches)) { if ($this->factory->isCompression($matches[1])) { $this->log(LogLevel::DEBUG, "Found the compression for '{file}' as '{compression}'", ['file' => $file, 'compression' => $matches[1]]); return $matches[1]; } return CompressionFactory::TYPE_UNKNOWN; } return CompressionFactory::TYPE_NONE; }
/** * Get the command line to decompress a file * * @param LocalFileNodeInterface $from * @param LocalFileNodeInterface $to * * @return string * */ public function getDecompressCommand(LocalFileNodeInterface $from, LocalFileNodeInterface $to) { return sprintf("unzip -p %s > %s", escapeshellarg($from->getPath()), escapeshellarg($to->getPath())); }