private function insertLazaretFiles(EntityManager $em, \Pimple $DI) { $session = new LazaretSession(); $session->setUser($DI['user']); $em->persist($session); $em->flush(); $file = File::buildFromPathfile($this->container['root.path'] . '/tests/files/cestlafete.jpg', $DI['coll'], $this->container); $callback = function ($element) use($DI) { $DI['lazaret_1'] = $element; }; $this->container['border-manager']->process($session, $file, $callback, Manager::FORCE_LAZARET); }
/** * {@inheritdoc} */ public function apply(base $appbox, Application $app) { $conn = $appbox->get_connection(); try { //get all old lazaret file & transform them to LazaretFile object $sql = 'SELECT * FROM lazaret'; $stmt = $conn->prepare($sql); $stmt->execute(); $rs = $stmt->fetchAll(); $stmt->closeCursor(); } catch (DBALException $e) { // table not found if ($e->getCode() == '42S02') { } return; } //order matters for foreign keys constraints //truncate all altered tables $this->truncateTable($app['EM'], 'Alchemy\\Phrasea\\Model\\Entities\\LazaretAttribute'); $this->truncateTable($app['EM'], 'Alchemy\\Phrasea\\Model\\Entities\\LazaretCheck'); $this->truncateTable($app['EM'], 'Alchemy\\Phrasea\\Model\\Entities\\LazaretFile'); $this->truncateTable($app['EM'], 'Alchemy\\Phrasea\\Model\\Entities\\LazaretSession'); $i = 0; foreach ($rs as $row) { $filePath = $app['tmp.lazaret.path'] . '/' . $row['filepath']; if (null === ($user = $this->loadUser($app['EM'], $row['usr_id']))) { continue; } if (file_exists($filePath)) { $spec = new ImageSpec(); $spec->setResizeMode(ImageSpec::RESIZE_MODE_INBOUND_FIXEDRATIO); $spec->setDimensions(375, 275); $thumbPath = $app['tmp.lazaret.path'] . '/' . sprintf("thumb_%s", $row['filepath']); try { $app['media-alchemyst']->turnInto($filePath, $thumbPath, $spec); } catch (MediaAlchemystException $e) { } $media = $app['mediavorus']->guess($filePath); $collection = \collection::get_from_base_id($app, $row['base_id']); $borderFile = new \Alchemy\Phrasea\Border\File($app, $media, $collection); $lazaretSession = new LazaretSession(); $lazaretSession->setUser($user); $lazaretFile = new LazaretFile(); $lazaretFile->setBaseId($row['base_id']); if (null === $row['uuid']) { $uuid = $borderFile->getUUID(true); $lazaretFile->setUuid($uuid); } else { $lazaretFile->setUuid($row['uuid']); } if (null === $row['sha256']) { $sha256 = $media->getHash('sha256'); $lazaretFile->setSha256($sha256); } else { $lazaretFile->setSha256($row['sha256']); } $lazaretFile->setOriginalName($row['filename']); $lazaretFile->setFilename($row['filepath']); $lazaretFile->setThumbFilename(pathinfo($thumbPath), PATHINFO_BASENAME); $lazaretFile->setCreated(new \DateTime($row['created_on'])); $lazaretFile->setSession($lazaretSession); $app['EM']->persist($lazaretFile); if (0 === ++$i % 100) { $app['EM']->flush(); $app['EM']->clear(); } } } $app['EM']->flush(); $app['EM']->clear(); $stmt->closeCursor(); return true; }
/** * {@inheritDoc} */ public function setUser(\Alchemy\Phrasea\Model\Entities\User $user) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setUser', array($user)); return parent::setUser($user); }
/** * 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(); }
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(); }