/** * @covers Alchemy\Phrasea\Border\File::addAttribute */ public function testAddAttribute() { $tag = new ObjectName(); $value = new Mono('Object name'); $metadata1 = new PHPExiftoolMetadata($tag, $value); $attribute1 = new Metadata($metadata1); $this->object->addAttribute($attribute1); $this->assertSame([$attribute1], $this->object->getAttributes()); $tag = new Keywords(); $value = new Multi(['Later', 'Alligator']); $metadata2 = new PHPExiftoolMetadata($tag, $value); $attribute2 = new Metadata($metadata2); $this->object->addAttribute($attribute2); $this->assertSame([$attribute1, $attribute2], $this->object->getAttributes()); }
/** * Creates a record * * @param \collection $collection The destination collection * @param string $pathfile The file to archive * @param string|null $captionFile The Phrasea XML caption file or null if no caption file * @param integer $grp_rid Add the record to a story * @param integer $force Force lazaret or record ; use \Alchemy\Phrasea\Border\Manager::FORCE_* constants * @return \record_adapter */ public function createRecord(Application $app, \collection $collection, $pathfile, $captionFile, $grp_rid, $force, $stat0, $stat1) { $status = \databox_status::operation_or($stat0, $stat1); $media = $app->getMediaFromUri($pathfile); $databox = $collection->get_databox(); $metadatasStructure = $databox->get_meta_structure(); $metadatas = $this->getIndexByFieldName($metadatasStructure, $media->getMetadatas()); $metaFields = []; if ($captionFile !== null && true === $app['filesystem']->exists($captionFile)) { $metaFields = $this->readXMLForDatabox($app, $metadatasStructure, $captionFile); $captionStatus = $this->parseStatusBit(@simplexml_load_file($captionFile)); if ($captionStatus) { $status = \databox_status::operation_mask($status, $captionStatus); } } $file = new File($app, $media, $collection); $file->addAttribute(new BorderAttribute\Status($app, $status)); $file->addAttribute(new BorderAttribute\Metadata(new Metadata(new PhraseaTag\TfFilepath(), new MonoValue($media->getFile()->getRealPath())))); $file->addAttribute(new BorderAttribute\Metadata(new Metadata(new PhraseaTag\TfDirname(), new MonoValue(dirname($media->getFile()->getRealPath()))))); $file->addAttribute(new BorderAttribute\Metadata(new Metadata(new PhraseaTag\TfAtime(), new MonoValue($media->getFile()->getATime())))); $file->addAttribute(new BorderAttribute\Metadata(new Metadata(new PhraseaTag\TfMtime(), new MonoValue($media->getFile()->getMTime())))); $file->addAttribute(new BorderAttribute\Metadata(new Metadata(new PhraseaTag\TfCtime(), new MonoValue($media->getFile()->getCTime())))); foreach ($metadatas as $meta) { $file->addAttribute(new BorderAttribute\Metadata($meta)); } foreach ($metaFields as $metaField) { $file->addAttribute($metaField); } if ($grp_rid) { $file->addAttribute(new BorderAttribute\Story(new \record_adapter($app, $databox->get_sbas_id(), $grp_rid))); } $record = null; $postProcess = function ($element, $visa, $code) use(&$record) { $record = $element; }; $app['border-manager']->process($this->getLazaretSession($app), $file, $postProcess, $force); return $record; }
/** * Upload processus * * @param Application $app The Silex application * @param Request $request The current request * * parameters : 'bas_id' int (mandatory) : The id of the destination collection * 'status' array (optional) : The status to set to new uploaded files * 'attributes' array (optional) : Attributes id's to attach to the uploaded files * 'forceBehavior' int (optional) : Force upload behavior * - 0 Force record * - 1 Force lazaret * * @return Response */ public function upload(Application $app, Request $request) { $datas = ['success' => false, 'code' => null, 'message' => '', 'element' => '', 'reasons' => [], 'id' => '']; if (null === $request->files->get('files')) { throw new BadRequestHttpException('Missing file parameter'); } if (count($request->files->get('files')) > 1) { throw new BadRequestHttpException('Upload is limited to 1 file per request'); } $base_id = $request->request->get('base_id'); if (!$base_id) { throw new BadRequestHttpException('Missing base_id parameter'); } if (!$app['acl']->get($app['authentication']->getUser())->has_right_on_base($base_id, 'canaddrecord')) { throw new AccessDeniedHttpException('User is not allowed to add record on this collection'); } $file = current($request->files->get('files')); if (!$file->isValid()) { throw new BadRequestHttpException('Uploaded file is invalid'); } try { // Add file extension, so mediavorus can guess file type for octet-stream file $uploadedFilename = $file->getRealPath(); $renamedFilename = $file->getRealPath() . '.' . pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION); $app['filesystem']->rename($uploadedFilename, $renamedFilename); $media = $app['mediavorus']->guess($renamedFilename); $collection = \collection::get_from_base_id($app, $base_id); $lazaretSession = new LazaretSession(); $lazaretSession->setUser($app['authentication']->getUser()); $app['EM']->persist($lazaretSession); $packageFile = new File($app, $media, $collection, $file->getClientOriginalName()); $postStatus = $request->request->get('status'); if (isset($postStatus[$collection->get_base_id()]) && is_array($postStatus[$collection->get_base_id()])) { $postStatus = $postStatus[$collection->get_base_id()]; $status = ''; foreach (range(0, 31) as $i) { $status .= isset($postStatus[$i]) ? $postStatus[$i] ? '1' : '0' : '0'; } $packageFile->addAttribute(new Status($app, strrev($status))); } $forceBehavior = $request->request->get('forceAction'); $reasons = []; $elementCreated = null; $callback = function ($element, $visa, $code) use($app, &$reasons, &$elementCreated) { foreach ($visa->getResponses() as $response) { if (!$response->isOk()) { $reasons[] = $response->getMessage($app['translator']); } } $elementCreated = $element; }; $code = $app['border-manager']->process($lazaretSession, $packageFile, $callback, $forceBehavior); $app['filesystem']->rename($renamedFilename, $uploadedFilename); if (!!$forceBehavior) { $reasons = []; } if ($elementCreated instanceof \record_adapter) { $id = $elementCreated->get_serialize_key(); $element = 'record'; $message = $app->trans('The record was successfully created'); $app['phraseanet.SE']->addRecord($elementCreated); // try to create thumbnail from data URI if ('' !== ($b64Image = $request->request->get('b64_image', ''))) { try { $dataUri = Parser::parse($b64Image); $fileName = $app['temporary-filesystem']->createTemporaryFile('base_64_thumb', null, "png"); file_put_contents($fileName, $dataUri->getData()); $media = $app['mediavorus']->guess($fileName); $app['subdef.substituer']->substitute($elementCreated, 'thumbnail', $media); $app['phraseanet.logger']($elementCreated->get_databox())->log($elementCreated, \Session_Logger::EVENT_SUBSTITUTE, 'thumbnail', ''); unset($media); $app['temporary-filesystem']->clean('base_64_thumb'); } catch (DataUriException $e) { } } } else { $app['dispatcher']->dispatch(PhraseaEvents::LAZARET_CREATE, new LazaretEvent($elementCreated)); $id = $elementCreated->getId(); $element = 'lazaret'; $message = $app->trans('The file was moved to the quarantine'); } $datas = ['success' => true, 'code' => $code, 'message' => $message, 'element' => $element, 'reasons' => $reasons, 'id' => $id]; } catch (\Exception $e) { $datas['message'] = $app->trans('Unable to add file to Phraseanet'); } $response = $app->json($datas); // IE 7 and 8 does not correctly handle json response in file API // lets send them an html content-type header $response->headers->set('Content-type', 'text/html'); return $response; }
public function add_record(Application $app, Request $request) { if (count($request->files->get('file')) == 0) { return $this->getBadRequest($app, $request, 'Missing file parameter'); } if (!$request->files->get('file') instanceof UploadedFile) { return $this->getBadRequest($app, $request, 'You can upload one file at time'); } $file = $request->files->get('file'); /* @var $file UploadedFile */ if (!$file->isValid()) { return $this->getBadRequest($app, $request, 'Datas corrupted, please try again'); } if (!$request->get('base_id')) { return $this->getBadRequest($app, $request, 'Missing base_id parameter'); } $collection = \collection::get_from_base_id($app, $request->get('base_id')); if (!$app['acl']->get($app['authentication']->getUser())->has_right_on_base($request->get('base_id'), 'canaddrecord')) { return Result::createError($request, 403, sprintf('You do not have access to collection %s', $collection->get_label($app['locale'])))->createResponse(); } $media = $app['mediavorus']->guess($file->getPathname()); $Package = new File($app, $media, $collection, $file->getClientOriginalName()); if ($request->get('status')) { $Package->addAttribute(new Status($app, $request->get('status'))); } $session = new LazaretSession(); $session->setUser($app['authentication']->getUser()); $app['EM']->persist($session); $app['EM']->flush(); $reasons = $output = null; $callback = function ($element, $visa, $code) use($app, &$reasons, &$output) { if (!$visa->isValid()) { $reasons = array_map(function ($response) use($app) { return $response->getMessage($app['translator']); }, $visa->getResponses()); } $output = $element; }; switch ($request->get('forceBehavior')) { case '0': $behavior = BorderManager::FORCE_RECORD; break; case '1': $behavior = BorderManager::FORCE_LAZARET; break; case null: $behavior = null; break; default: return $this->getBadRequest($app, $request, sprintf('Invalid forceBehavior value `%s`', $request->get('forceBehavior'))); } $app['border-manager']->process($session, $Package, $callback, $behavior); $ret = ['entity' => null]; if ($output instanceof \record_adapter) { $ret['entity'] = '0'; $ret['url'] = '/records/' . $output->get_sbas_id() . '/' . $output->get_record_id() . '/'; $app['phraseanet.SE']->addRecord($output); } if ($output instanceof LazaretFile) { $ret['entity'] = '1'; $ret['url'] = '/quarantine/item/' . $output->getId() . '/'; } return Result::create($request, $ret)->createResponse(); }
/** * Add technical Metadata attribute to a package file by reference to add it * to Phraseanet * * @param File $file The file */ protected function addMediaAttributes(File $file) { $metadataCollection = $this->app['phraseanet.metadata-reader']->read($file->getMedia()); array_walk($metadataCollection, function (Metadata $metadata) use($file) { $file->addAttribute(new MetadataAttr($metadata)); }); return $this; }
public function addRecordAction(Request $request) { if (count($request->files->get('file')) == 0) { return $this->getBadRequestAction($request, 'Missing file parameter'); } $file = $request->files->get('file'); if (!$file instanceof UploadedFile) { return $this->getBadRequestAction($request, 'You can upload one file at time'); } if (!$file->isValid()) { return $this->getBadRequestAction($request, 'Data corrupted, please try again'); } if (!$request->get('base_id')) { return $this->getBadRequestAction($request, 'Missing base_id parameter'); } $collection = \collection::get_from_base_id($this->app, $request->get('base_id')); if (!$this->getAclForUser()->has_right_on_base($request->get('base_id'), 'canaddrecord')) { return Result::createError($request, 403, sprintf('You do not have access to collection %s', $collection->get_label($this->app['locale'])))->createResponse(); } $media = $this->app->getMediaFromUri($file->getPathname()); $Package = new File($this->app, $media, $collection, $file->getClientOriginalName()); if ($request->get('status')) { $Package->addAttribute(new Status($this->app, $request->get('status'))); } $session = new LazaretSession(); $session->setUser($this->getAuthenticatedUser()); $entityManager = $this->app['orm.em']; $entityManager->persist($session); $entityManager->flush(); $reasons = $output = null; $translator = $this->app['translator']; $callback = function ($element, Visa $visa) use($translator, &$reasons, &$output) { if (!$visa->isValid()) { $reasons = array_map(function (CheckerResponse $response) use($translator) { return $response->getMessage($translator); }, $visa->getResponses()); } $output = $element; }; switch ($request->get('forceBehavior')) { case '0': $behavior = Manager::FORCE_RECORD; break; case '1': $behavior = Manager::FORCE_LAZARET; break; case null: $behavior = null; break; default: return $this->getBadRequestAction($request, sprintf('Invalid forceBehavior value `%s`', $request->get('forceBehavior'))); } $nosubdef = $request->get('nosubdefs') === '' || \p4field::isyes($request->get('nosubdefs')); $this->getBorderManager()->process($session, $Package, $callback, $behavior, $nosubdef); $ret = ['entity' => null]; if ($output instanceof \record_adapter) { $ret['entity'] = '0'; $ret['url'] = '/records/' . $output->getDataboxId() . '/' . $output->getRecordId() . '/'; $this->dispatch(PhraseaEvents::RECORD_UPLOAD, new RecordEdit($output)); } if ($output instanceof LazaretFile) { $ret['entity'] = '1'; $ret['url'] = '/quarantine/item/' . $output->getId() . '/'; } return Result::create($request, $ret)->createResponse(); }
/** * Add technical Metadata attribute to a package file by reference to add it * to Phraseanet * * @param File $file The file * @return \Doctrine\ORM\EntityManager */ protected function addMediaAttributes(File $file) { if (method_exists($file->getMedia(), 'getWidth')) { $file->addAttribute(new MetadataAttr(new Metadata(new TfWidth(), new MonoValue($file->getMedia()->getWidth())))); } if (method_exists($file->getMedia(), 'getHeight')) { $file->addAttribute(new MetadataAttr(new Metadata(new TfHeight(), new MonoValue($file->getMedia()->getHeight())))); } if (method_exists($file->getMedia(), 'getChannels')) { $file->addAttribute(new MetadataAttr(new Metadata(new TfChannels(), new MonoValue($file->getMedia()->getChannels())))); } if (method_exists($file->getMedia(), 'getColorDepth')) { $file->addAttribute(new MetadataAttr(new Metadata(new TfBits(), new MonoValue($file->getMedia()->getColorDepth())))); } if (method_exists($file->getMedia(), 'getDuration')) { $file->addAttribute(new MetadataAttr(new Metadata(new TfDuration(), new MonoValue($file->getMedia()->getDuration())))); } if ($file->getFile()->getMimeType() == 'application/pdf' && null !== $this->pdfToText) { try { $text = $this->pdfToText->getText($file->getFile()->getRealPath()); if (trim($text)) { $file->addAttribute(new MetadataAttr(new Metadata(new PdfText(), new MonoValue($text)))); } } catch (\XPDF\Exception\Exception $e) { } } $file->addAttribute(new MetadataAttr(new Metadata(new TfMimetype(), new MonoValue($file->getFile()->getMimeType())))); $file->addAttribute(new MetadataAttr(new Metadata(new TfSize(), new MonoValue($file->getFile()->getSize())))); $file->addAttribute(new MetadataAttr(new Metadata(new TfBasename(), new MonoValue(pathinfo($file->getOriginalName(), PATHINFO_BASENAME))))); $file->addAttribute(new MetadataAttr(new Metadata(new TfFilename(), new MonoValue(pathinfo($file->getOriginalName(), PATHINFO_FILENAME))))); $file->addAttribute(new MetadataAttr(new Metadata(new TfExtension(), new MonoValue(pathinfo($file->getOriginalName(), PATHINFO_EXTENSION))))); return $this; }