|
7 | 7 | use Symfony\Component\Console\Output\OutputInterface;
|
8 | 8 | use Symfony\Component\Console\Input\InputArgument;
|
9 | 9 | use PHPCR\PropertyType;
|
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use PHPCR\PathNotFoundException; |
10 | 12 |
|
11 | 13 | class NodeFileImportCommand extends Command
|
12 | 14 | {
|
| 15 | + /** |
| 16 | + * @var PHPCR\SessionInterface |
| 17 | + */ |
| 18 | + protected $session; |
| 19 | + |
13 | 20 | protected function configure()
|
14 | 21 | {
|
15 |
| - $this->setName('node:file:import'); |
| 22 | + $this->setName('file:import'); |
16 | 23 | $this->setDescription('Import a file at the given path');
|
17 | 24 | $this->addArgument('path', InputArgument::REQUIRED, 'Path to import file to');
|
18 | 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'); |
19 | 29 | $this->setHelp(<<<HERE
|
20 | 30 | Import an external file into the repository.
|
21 | 31 |
|
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. |
| 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. |
24 | 46 |
|
25 |
| -The mime-type will be inferred automatically.. |
| 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>. |
26 | 51 | HERE
|
27 | 52 | );
|
28 | 53 | }
|
29 | 54 |
|
30 | 55 | public function execute(InputInterface $input, OutputInterface $output)
|
31 | 56 | {
|
32 |
| - $session = $this->getHelper('phpcr')->getSession(); |
| 57 | + $this->session = $this->getHelper('phpcr')->getSession(); |
33 | 58 |
|
34 |
| - $file = $input->getArgument('file'); |
35 |
| - $path = $session->getAbsPath($input->getArgument('path')); |
| 59 | + $filePath = $input->getArgument('file'); |
| 60 | + $force = $input->getOption('force'); |
| 61 | + $noContainer = $input->getOption('no-container'); |
36 | 62 |
|
37 |
| - $parentNode = $session->getNode($path); |
| 63 | + $path = $this->session->getAbsPath($input->getArgument('path')); |
| 64 | + $mimeType = $input->getOption('mime-type'); |
| 65 | + $filename = basename($filePath); |
38 | 66 |
|
39 |
| - if (!file_exists($file)) { |
| 67 | + if (!file_exists($filePath)) { |
40 | 68 | throw new \InvalidArgumentException(sprintf(
|
41 | 69 | 'File "%s" does not exist.',
|
42 |
| - $file |
| 70 | + $filePath |
43 | 71 | ));
|
44 | 72 | }
|
45 | 73 |
|
46 |
| - $finfo = finfo_open(FILEINFO_MIME_TYPE); |
47 |
| - $mimeType = finfo_file($finfo, $file); |
| 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 | + } |
48 | 120 |
|
49 |
| - $filename = basename($file); |
50 | 121 | $fileNode = $parentNode->addNode($filename, 'nt:file');
|
51 | 122 | $contentNode = $fileNode->addNode('jcr:content', 'nt:unstructured');
|
52 |
| - $content = file_get_contents($file); |
53 |
| - $contentNode->setProperty('jcr:data', $content, PropertyType::BINARY); |
| 123 | + $contentNode->setProperty('jcr:data', $fhandle, PropertyType::BINARY); |
54 | 124 | $contentNode->setProperty('jcr:mimeType', $mimeType);
|
55 | 125 | }
|
56 | 126 | }
|
0 commit comments