예제 #1
0
 /**
  * @covers Alchemy\Phrasea\Border\Attribute\Factory::getFileAttribute
  * @expectedException \InvalidArgumentException
  */
 public function testGetFileAttributeFail()
 {
     Factory::getFileAttribute(self::$DI['app'], 'nothing', 'nothong');
 }
예제 #2
0
 /**
  * Add an element to phraseanet
  *
  * @param Application $app     A Silex application
  * @param Request     $request The current request
  * @param int         $file_id A lazaret element id
  *
  * parameters   : 'bas_id'            int     (mandatory) : The id of the destination collection
  *                'keep_attributes'   boolean (optional)  : Keep all attributes attached to the lazaret element
  *                'attributes'        array   (optional)  : Attributes id's to attach to the lazaret element
  *
  * @return Response
  */
 public function addElement(Application $app, Request $request, $file_id)
 {
     $ret = ['success' => false, 'message' => '', 'result' => []];
     //Optional parameter
     $keepAttributes = !!$request->request->get('keep_attributes', false);
     $attributesToKeep = $request->request->get('attributes', []);
     //Mandatory parameter
     if (null === $request->request->get('bas_id')) {
         $ret['message'] = $app->trans('You must give a destination collection');
         return $app->json($ret);
     }
     $lazaretFile = $app['EM']->find('Phraseanet:LazaretFile', $file_id);
     /* @var $lazaretFile LazaretFile */
     if (null === $lazaretFile) {
         $ret['message'] = $app->trans('File is not present in quarantine anymore, please refresh');
         return $app->json($ret);
     }
     $lazaretFileName = $app['root.path'] . '/tmp/lazaret/' . $lazaretFile->getFilename();
     $lazaretThumbFileName = $app['root.path'] . '/tmp/lazaret/' . $lazaretFile->getThumbFilename();
     try {
         $borderFile = Border\File::buildFromPathfile($lazaretFileName, $lazaretFile->getCollection($app), $app, $lazaretFile->getOriginalName());
         $record = null;
         /* @var $record \record_adapter */
         //Post record creation
         $callBack = function ($element, $visa, $code) use(&$record) {
             $record = $element;
         };
         //Force creation record
         $app['border-manager']->process($lazaretFile->getSession(), $borderFile, $callBack, Border\Manager::FORCE_RECORD);
         $app['phraseanet.SE']->addRecord($record);
         if ($keepAttributes) {
             //add attribute
             $metaFields = new Border\MetaFieldsBag();
             $metadataBag = new Border\MetadataBag();
             foreach ($lazaretFile->getAttributes() as $attr) {
                 //Check which ones to keep
                 if (!!count($attributesToKeep)) {
                     if (!in_array($attr->getId(), $attributesToKeep)) {
                         continue;
                     }
                 }
                 try {
                     $attribute = Border\Attribute\Factory::getFileAttribute($app, $attr->getName(), $attr->getValue());
                 } catch (\InvalidArgumentException $e) {
                     continue;
                 }
                 /* @var $attribute AttributeInterface */
                 switch ($attribute->getName()) {
                     case AttributeInterface::NAME_METADATA:
                         $value = $attribute->getValue();
                         $metadataBag->set($value->getTag()->getTagname(), new \PHPExiftool\Driver\Metadata\Metadata($value->getTag(), $value->getValue()));
                         break;
                     case AttributeInterface::NAME_STORY:
                         $attribute->getValue()->appendChild($record);
                         break;
                     case AttributeInterface::NAME_STATUS:
                         $record->set_binary_status($attribute->getValue());
                         break;
                     case AttributeInterface::NAME_METAFIELD:
                         $metaFields->set($attribute->getField()->get_name(), $attribute->getValue());
                         break;
                 }
             }
             $datas = $metadataBag->toMetadataArray($record->get_databox()->get_meta_structure());
             $record->set_metadatas($datas);
             $fields = $metaFields->toMetadataArray($record->get_databox()->get_meta_structure());
             $record->set_metadatas($fields);
         }
         //Delete lazaret file
         $app['EM']->remove($lazaretFile);
         $app['EM']->flush();
         $ret['success'] = true;
     } catch (\Exception $e) {
         $ret['message'] = $app->trans('An error occured');
     }
     try {
         $app['filesystem']->remove([$lazaretFileName, $lazaretThumbFileName]);
     } catch (IOException $e) {
     }
     return $app->json($ret);
 }
예제 #3
0
 /**
  * @covers Alchemy\Phrasea\Border\Manager::process
  */
 public function testLazaretAttributes()
 {
     $file = File::buildFromPathfile(self::$file1, self::$DI['collection'], self::$DI['app']);
     $objectNameTag = new \PHPExiftool\Driver\Tag\IPTC\ObjectName();
     $monoValue = new \PHPExiftool\Driver\Value\Mono('title');
     $monoData = new \PHPExiftool\Driver\Metadata\Metadata($objectNameTag, $monoValue);
     $personInImageTag = new \PHPExiftool\Driver\Tag\XMPIptcExt\PersonInImage();
     $multiValue = new \PHPExiftool\Driver\Value\Multi(['Babar', 'Celeste']);
     $multiData = new \PHPExiftool\Driver\Metadata\Metadata($personInImageTag, $multiValue);
     $file->addAttribute(new Metadata($monoData));
     $file->addAttribute(new Metadata($multiData));
     $application = $this->getApplication();
     $postProcess = function ($element, $visa, $code) use($application) {
         $this->assertInstanceOf('\\Alchemy\\Phrasea\\Model\\Entities\\LazaretFile', $element);
         /* @var $element \Alchemy\Phrasea\Model\Entities\LazaretFile */
         foreach ($element->getAttributes() as $attribute) {
             $this->assertEquals('metadata', $attribute->getName());
             $value = Factory::getFileAttribute($application, $attribute->getName(), $attribute->getValue());
             $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Attribute\\Metadata', $value);
         }
     };
     $this->assertEquals(Manager::LAZARET_CREATED, $this->object->process($this->session, $file, $postProcess, Manager::FORCE_LAZARET));
 }
예제 #4
0
 /**
  * Add an element to phraseanet
  *
  * @param Request     $request The current request
  * @param int         $file_id A lazaret element id
  *
  * parameters   : 'bas_id'            int     (mandatory) : The id of the destination collection
  *                'keep_attributes'   boolean (optional)  : Keep all attributes attached to the lazaret element
  *                'attributes'        array   (optional)  : Attributes id's to attach to the lazaret element
  *
  * @return Response
  */
 public function addElement(Request $request, $file_id)
 {
     $ret = ['success' => false, 'message' => '', 'result' => []];
     //Optional parameter
     $keepAttributes = !!$request->request->get('keep_attributes', false);
     $attributesToKeep = $request->request->get('attributes', []);
     //Mandatory parameter
     if (null === $request->request->get('bas_id')) {
         $ret['message'] = $this->app->trans('You must give a destination collection');
         return $this->app->json($ret);
     }
     /* @var LazaretFile $lazaretFile */
     $lazaretFile = $this->getLazaretFileRepository()->find($file_id);
     if (null === $lazaretFile) {
         $ret['message'] = $this->app->trans('File is not present in quarantine anymore, please refresh');
         return $this->app->json($ret);
     }
     $path = $this->app['tmp.lazaret.path'];
     $lazaretFileName = $path . '/' . $lazaretFile->getFilename();
     $lazaretThumbFileName = $path . '/' . $lazaretFile->getThumbFilename();
     try {
         $borderFile = Border\File::buildFromPathfile($lazaretFileName, $lazaretFile->getCollection($this->app), $this->app, $lazaretFile->getOriginalName());
         //Post record creation
         /** @var \record_adapter $record */
         $record = null;
         $callBack = function ($element) use(&$record) {
             $record = $element;
         };
         //Force creation record
         $this->getBorderManager()->process($lazaretFile->getSession(), $borderFile, $callBack, Border\Manager::FORCE_RECORD);
         if ($keepAttributes) {
             //add attribute
             $metaFields = new Border\MetaFieldsBag();
             $metadataBag = new Border\MetadataBag();
             foreach ($lazaretFile->getAttributes() as $attr) {
                 //Check which ones to keep
                 if (!!count($attributesToKeep)) {
                     if (!in_array($attr->getId(), $attributesToKeep)) {
                         continue;
                     }
                 }
                 try {
                     $attribute = Border\Attribute\Factory::getFileAttribute($this->app, $attr->getName(), $attr->getValue());
                 } catch (\InvalidArgumentException $e) {
                     continue;
                 }
                 switch ($attribute->getName()) {
                     case AttributeInterface::NAME_METADATA:
                         /** @var Metadata $value */
                         $value = $attribute->getValue();
                         $metadataBag->set($value->getTag()->getTagname(), new Metadata($value->getTag(), $value->getValue()));
                         break;
                     case AttributeInterface::NAME_STORY:
                         /** @var \record_adapter $value */
                         $value = $attribute->getValue();
                         $value->appendChild($record);
                         break;
                     case AttributeInterface::NAME_STATUS:
                         $record->set_binary_status($attribute->getValue());
                         break;
                     case AttributeInterface::NAME_METAFIELD:
                         /** @var Border\Attribute\MetaField $attribute */
                         $metaFields->set($attribute->getField()->get_name(), $attribute->getValue());
                         break;
                 }
             }
             $data = $metadataBag->toMetadataArray($record->get_databox()->get_meta_structure());
             $record->set_metadatas($data);
             $fields = $metaFields->toMetadataArray($record->get_databox()->get_meta_structure());
             $record->set_metadatas($fields);
         }
         //Delete lazaret file
         $manager = $this->getEntityManager();
         $manager->remove($lazaretFile);
         $manager->flush();
         $ret['success'] = true;
     } catch (\Exception $e) {
         $ret['message'] = $this->app->trans('An error occured');
     }
     try {
         $this->getFilesystem()->remove([$lazaretFileName, $lazaretThumbFileName]);
     } catch (IOException $e) {
     }
     return $this->app->json($ret);
 }