|
| 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 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use PHPCR\PathNotFoundException; |
| 12 | + |
| 13 | +class NodeFileImportCommand extends Command |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var PHPCR\SessionInterface |
| 17 | + */ |
| 18 | + protected $session; |
| 19 | + |
| 20 | + protected function configure() |
| 21 | + { |
| 22 | + $this->setName('file:import'); |
| 23 | + $this->setDescription('Import a file at the given path'); |
| 24 | + $this->addArgument('path', InputArgument::REQUIRED, 'Path to import file to'); |
| 25 | + $this->addArgument('file', InputArgument::REQUIRED, 'Path to file to import'); |
| 26 | + $this->addOption('mime-type', null, InputOption::VALUE_REQUIRED, 'Mime type (optional, auto-detected)'); |
| 27 | + $this->addOption('force', null, InputOption::VALUE_NONE, 'Force overwriting any existing node'); |
| 28 | + $this->addOption('no-container', null, InputOption::VALUE_NONE, 'Do not wrap in a JCR nt:file, but write directly to the specified property'); |
| 29 | + $this->setHelp(<<<HERE |
| 30 | +Import an external file into the repository. |
| 31 | +
|
| 32 | +The file will be imported as a node of built-in type <comment>nt:file</comment>. |
| 33 | +
|
| 34 | +If a Node is specified as <info>path</info> then the filename of the imported file will be used |
| 35 | +as the new node, otherwise, if the target <info>path</info> does not exist, then it is assumed |
| 36 | +that the path is the target path for the new file, including the filename. |
| 37 | +
|
| 38 | + PHPCRSH> file:import ./ foobar.png |
| 39 | + PHPCRSH> file:import ./barfoo.png foobar.png |
| 40 | +
|
| 41 | +In the first example above will create <info>/foobar.png</info>, whereas the second will create |
| 42 | +<info>./barfoo.png</info>. |
| 43 | +
|
| 44 | +By default the file will be imported in a container, i.e. a node with type <info>nt:file</info>. In |
| 45 | +addition to the file data, the node will contain metadata. |
| 46 | +
|
| 47 | +Alternatively you can specify the <info>--no-container</info> option to import directly to a single property. |
| 48 | +
|
| 49 | +The mime-type of the file (in the case where a container is used) will be automatically determined unless |
| 50 | +specified with <info>--mime-type</info>. |
| 51 | +HERE |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function execute(InputInterface $input, OutputInterface $output) |
| 56 | + { |
| 57 | + $this->session = $this->getHelper('phpcr')->getSession(); |
| 58 | + |
| 59 | + $filePath = $input->getArgument('file'); |
| 60 | + $force = $input->getOption('force'); |
| 61 | + $noContainer = $input->getOption('no-container'); |
| 62 | + |
| 63 | + $path = $this->session->getAbsPath($input->getArgument('path')); |
| 64 | + $mimeType = $input->getOption('mime-type'); |
| 65 | + $filename = basename($filePath); |
| 66 | + |
| 67 | + if (!file_exists($filePath)) { |
| 68 | + throw new \InvalidArgumentException(sprintf( |
| 69 | + 'File "%s" does not exist.', |
| 70 | + $filePath |
| 71 | + )); |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + // first assume the user specified the path to the parent node |
| 76 | + $parentNode = $this->session->getNode($path); |
| 77 | + } catch (PathNotFoundException $e) { |
| 78 | + // if the given path does not exist, assume that the basename is the target |
| 79 | + // filename and the dirname the path to the parent node |
| 80 | + $parentPath = dirname($path); |
| 81 | + $parentNode = $this->session->getNode($parentPath); |
| 82 | + $filename = basename($path); |
| 83 | + } |
| 84 | + |
| 85 | + $fhandle = fopen($filePath, 'r'); |
| 86 | + |
| 87 | + if ($noContainer) { |
| 88 | + $this->importToProperty($fhandle, $filePath, $filename, $parentNode, $force); |
| 89 | + } else { |
| 90 | + $this->importToContainer($fhandle, $mimeType, $filePath, $filename, $parentNode, $force); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private function importToProperty($fhandle, $filePath, $filename, $parentNode, $force) |
| 95 | + { |
| 96 | + $parentNode->setProperty($filename, $fhandle, PropertyType::BINARY); |
| 97 | + } |
| 98 | + |
| 99 | + private function importToContainer($fhandle, $mimeType, $file, $filename, $parentNode, $force) |
| 100 | + { |
| 101 | + // if no mime-type specified, guess it. |
| 102 | + if (!$mimeType) { |
| 103 | + $finfo = finfo_open(FILEINFO_MIME_TYPE); |
| 104 | + $mimeType = finfo_file($finfo, $file); |
| 105 | + } |
| 106 | + |
| 107 | + // handle existing node |
| 108 | + if ($parentNode->hasNode($filename)) { |
| 109 | + if (true === $force) { |
| 110 | + $fileNode = $parentNode->getNode($filename); |
| 111 | + $this->session->removeItem($fileNode->getPath()); |
| 112 | + } else { |
| 113 | + throw new \InvalidArgumentException(sprintf( |
| 114 | + 'Node "%s" already has child "%s". Use --force to overwrite.', |
| 115 | + $parentNode->getPath(), |
| 116 | + $filename |
| 117 | + )); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + $fileNode = $parentNode->addNode($filename, 'nt:file'); |
| 122 | + $contentNode = $fileNode->addNode('jcr:content', 'nt:unstructured'); |
| 123 | + $contentNode->setProperty('jcr:data', $fhandle, PropertyType::BINARY); |
| 124 | + $contentNode->setProperty('jcr:mimeType', $mimeType); |
| 125 | + } |
| 126 | +} |
0 commit comments