|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPCR\Shell\Console\Command\Phpcr; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Command\Command; |
| 6 | +use Symfony\Component\Console\Input\InputInterface; |
| 7 | +use Symfony\Component\Console\Output\OutputInterface; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use PHPCR\PropertyType; |
| 10 | + |
| 11 | +class NodeFileImportCommand extends Command |
| 12 | +{ |
| 13 | + protected function configure() |
| 14 | + { |
| 15 | + $this->setName('node:file:import'); |
| 16 | + $this->setDescription('Import a file at the given path'); |
| 17 | + $this->addArgument('path', InputArgument::REQUIRED, 'Path to import file to'); |
| 18 | + $this->addArgument('file', InputArgument::REQUIRED, 'Path to file to import'); |
| 19 | + $this->setHelp(<<<HERE |
| 20 | +Import an external file into the repository. |
| 21 | +
|
| 22 | +The file will be imported as a node of built-in type <comment>nt:file</comment>. The new |
| 23 | +node will be named after the file. |
| 24 | +
|
| 25 | +The mime-type will be inferred automatically.. |
| 26 | +HERE |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + public function execute(InputInterface $input, OutputInterface $output) |
| 31 | + { |
| 32 | + $session = $this->getHelper('phpcr')->getSession(); |
| 33 | + |
| 34 | + $file = $input->getArgument('file'); |
| 35 | + $path = $session->getAbsPath($input->getArgument('path')); |
| 36 | + |
| 37 | + $parentNode = $session->getNode($path); |
| 38 | + |
| 39 | + if (!file_exists($file)) { |
| 40 | + throw new \InvalidArgumentException(sprintf( |
| 41 | + 'File "%s" does not exist.', |
| 42 | + $file |
| 43 | + )); |
| 44 | + } |
| 45 | + |
| 46 | + $finfo = finfo_open(FILEINFO_MIME_TYPE); |
| 47 | + $mimeType = finfo_file($finfo, $file); |
| 48 | + |
| 49 | + $filename = basename($file); |
| 50 | + $fileNode = $parentNode->addNode($filename, 'nt:file'); |
| 51 | + $contentNode = $fileNode->addNode('jcr:content', 'nt:unstructured'); |
| 52 | + $content = file_get_contents($file); |
| 53 | + $contentNode->setProperty('jcr:data', $content, PropertyType::BINARY); |
| 54 | + $contentNode->setProperty('jcr:mimeType', $mimeType); |
| 55 | + } |
| 56 | +} |
0 commit comments