Skip to content

Commit fa13ea3

Browse files
committed
Added ability to import a file into the repository
1 parent 586b757 commit fa13ea3

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed
19.1 KB
Loading
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Feature: Import an external file as to a node
2+
In order to import an external file into the system
3+
As a user that is logged into the shell
4+
I should be able to run a command which does that
5+
6+
Background:
7+
Given that I am logged in as "testuser"
8+
And the "cms.xml" fixtures are loaded
9+
10+
Scenario: Import a file
11+
Given the file "phpcr.png" contains the contents of "files/phpcrlogos.png"
12+
Given the current node is "/"
13+
And I execute the "node:file:import . phpcr.png" command
14+
Then the command should not fail
15+
And I save the session
16+
Then the command should not fail
17+
And there should exist a node at "/phpcr.png"

src/PHPCR/Shell/Console/Application/ShellApplication.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ private function registerCommands()
189189
$this->add(new CommandPhpcr\NodeCreateCommand());
190190
$this->add(new CommandPhpcr\NodeCorrespondingCommand());
191191
$this->add(new CommandPhpcr\NodeDefinitionCommand());
192+
$this->add(new CommandPhpcr\NodeFileImportCommand());
192193
$this->add(new CommandPhpcr\NodePropertySetCommand());
193194
$this->add(new CommandPhpcr\NodeSetPrimaryTypeCommand());
194195
$this->add(new CommandPhpcr\NodeRenameCommand());
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)