diff --git a/Command/CreateCommand.php b/Command/CreateCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..b4c250c5266296b15d9ac10f0f417f3ece7ecbc4 --- /dev/null +++ b/Command/CreateCommand.php @@ -0,0 +1,95 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Command; + +use Doctrine\ORM\EntityManager; +use Irstea\FileUploadBundle\Model\FileManagerInterface; +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * Description of GarbageCollectorCommand + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class CreateCommand extends ContainerAwareCommand +{ + protected function configure() + { + $this + ->setName('irstea:file-upload:create') + ->setDescription("Créer un fichier.") + ->addOption('mime-type', 't', InputOption::VALUE_REQUIRED, "Force le type MIME.") + ->addOption('display-name', 'd', InputOption::VALUE_REQUIRED, "Indique un nom de fichier.") + ->addOption('metadata', 'm', InputOption::VALUE_REQUIRED, "Ajoute des métadata (format JSON).") + ->addArgument('path', InputArgument::REQUIRED, "Nom du fichier à ajouter, - pour lire l'entrée standard") + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + /* @var $em EntityManager */ + $em = $this->getContainer()->get('doctrine.orm.entity_manager'); + + $id = $written = null; + + $em->transactional(function() use($input, $output, $em, &$file, &$written) { + + /* @var $manager FileManagerInterface */ + $manager = $this->getContainer()->get('irstea_file_upload.file_manager'); + + $path = $input->getArgument("path"); + if("-" === $path) { + $realPath = 'php://stdin'; + $displayName = "stdin"; + $lastModified = time(); + $size = 0; + } else { + $realPath = $path; + $displayName = pathinfo($path, PATHINFO_FILENAME); + $size = filesize($path); + $lastModified = filemtime($path); + } + + $mimeType = $input->getOption('mime-type'); + if(null !== $forcedDisplayName = $input->getOption('display-name')) { + $displayName = $forcedDisplayName; + } + $metadata = null; + if(null !== $jsonMetadata = $input->getOption('metadata')) { + $metadata = json_decode($jsonMetadata); + } + + $file = $manager->create($displayName, $size, $mimeType ?: 'application/octet-stream', $lastModified, null, 'console'); + + $fh = fopen($realPath, 'rb'); + $written = $file->copyFrom($fh); + fclose($fh); + + $manager->completed($file); + + if($mimeType) { + $file->setMimeType($mimetype); + } + if($metadata) { + $file->setMetadata(array_merge($file->getMetadata(), $metadata)); + } + + $em->flush(); + }); + + if($output->isVerbose()) { + $output->writeln(sprintf("%s crée, %d octets lus.", $file->getId(), $written)); + } else { + $output->writeln($file->getId()); + } + } +} diff --git a/Command/ReadCommand.php b/Command/ReadCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..ce7bec97cc86afd7a54cd22176a896b3abc781d1 --- /dev/null +++ b/Command/ReadCommand.php @@ -0,0 +1,65 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Command; + +use Doctrine\ORM\EntityManager; +use Irstea\FileUploadBundle\Model\FileManagerInterface; +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * Description of GarbageCollectorCommand + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class ReadCommand extends ContainerAwareCommand +{ + protected function configure() + { + $this + ->setName('irstea:file-upload:read') + ->setDescription("Récupère le contenu d'un fichier.") + ->addOption('append', 'a', InputOption::VALUE_NONE, "Ajoute au fichier de destination au lieu de l'écraser.") + ->addOption('overwrite', 'y', InputOption::VALUE_NONE, "Ecrase le fichier de destination s'il existe.") + ->addArgument('id', InputArgument::REQUIRED, "Identifiant du fichier à récupérer.") + ->addArgument('filepath', InputArgument::OPTIONAL, "Chemin du fichier dans lequel écrire.") + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + /* @var $manager FileManagerInterface */ + $manager = $this->getContainer()->get('irstea_file_upload.file_manager'); + + $file = $manager->get($input->getArgument('id')); + + $path = $input->getArgument('filepath'); + + if(null == $path) { + $fh = fopen('php://stdout', 'wb'); + } else { + if($input->getOption('append')) { + $fh = fopen($path, 'ab'); + } elseif($input->getOption('overwrite')) { + $fh = fopen($path, 'wb'); + } else { + $fh = fopen($path, 'xb'); + } + } + + $written = $file->copyTo($fh); + fclose($fh); + + if($output->isVerbose() && null !== $path) { + $output->writeln(sprintf("%d octets écrits.", $written)); + } + } +}