public function deliverContent(Request $request, \record_adapter $record, $subdef, $watermark, $stamp) { $file = $record->get_subdef($subdef); $pathOut = $file->get_pathfile(); if ($watermark === true && $file->get_type() === \media_subdef::TYPE_IMAGE) { $pathOut = \recordutils_image::watermark($this->app, $file); } elseif ($stamp === true && $file->get_type() === \media_subdef::TYPE_IMAGE) { $pathOut = \recordutils_image::stamp($this->app, $file); } $disposition = $request->query->get('download') ? DeliverDataInterface::DISPOSITION_ATTACHMENT : DeliverDataInterface::DISPOSITION_INLINE; /** @var Response $response */ $response = $this->deliverFile($pathOut, $file->get_file(), $disposition, $file->get_mime()); if (in_array($subdef, array('document', 'preview'))) { $response->setPrivate(); $this->logView($record, $request); } elseif ($subdef !== 'thumbnail') { try { if ($file->getDataboxSubdef()->get_class() != \databox_subdef::CLASS_THUMBNAIL) { $response->setPrivate(); $this->logView($record, $request); } } catch (\Exception $e) { // Ignore exception } } $response->isNotModified($request); return $response; }
public function testStampWithFile() { $this->addStampConf(self::$DI['record_1']->get_collection()); self::$DI['app']->getApplicationBox()->write_collection_pic(self::$DI['app']['media-alchemyst'], self::$DI['app']['filesystem'], self::$DI['record_1']->get_collection(), new SymfoFile(__DIR__ . '/../../files/logocoll.gif'), \collection::PIC_STAMP); $path = recordutils_image::stamp(self::$DI['app'], self::$DI['record_1']->get_subdef('preview')); $this->assertTrue(0 === strpos(basename($path), 'stamp_')); unlink($path); }
public function deliverContent(Request $request, \record_adapter $record, $subdef, $watermark, $stamp, Application $app) { $file = $record->get_subdef($subdef); $pathOut = $file->get_pathfile(); if ($watermark === true && $file->get_type() === \media_subdef::TYPE_IMAGE) { $pathOut = \recordutils_image::watermark($app, $file); } elseif ($stamp === true && $file->get_type() === \media_subdef::TYPE_IMAGE) { $pathOut = \recordutils_image::stamp($app, $file); } $log_id = null; try { $logger = $app['phraseanet.logger']($record->get_databox()); $log_id = $logger->get_id(); $referrer = 'NO REFERRER'; if (isset($_SERVER['HTTP_REFERER'])) { $referrer = $_SERVER['HTTP_REFERER']; } $record->log_view($log_id, $referrer, $app['conf']->get(['main', 'key'])); } catch (\Exception $e) { } $disposition = $request->query->get('download') ? DeliverDataInterface::DISPOSITION_ATTACHMENT : DeliverDataInterface::DISPOSITION_INLINE; $response = $app['phraseanet.file-serve']->deliverFile($pathOut, $file->get_file(), $disposition, $file->get_mime()); $response->setPrivate(); /* @var $response \Symfony\Component\HttpFoundation\Response */ if ($file->getEtag()) { $response->setEtag($file->getEtag()); $response->setLastModified($file->get_modification_date()); } if (false === $record->is_grouping() && $subdef !== 'document') { try { if ($file->getDataboxSubdef()->get_class() == \databox_subdef::CLASS_THUMBNAIL) { // default expiration is 5 days $expiration = 60 * 60 * 24 * 5; $response->setExpires(new \DateTime(sprintf('+%d seconds', $expiration))); $response->setMaxAge($expiration); $response->setSharedMaxAge($expiration); $response->setPublic(); } } catch (\Exception $e) { } } $response->isNotModified($request); return $response; }
/** * * @param User $user * @param Filesystem $filesystem * @param Array $subdefs * @param boolean $rename_title * @param boolean $includeBusinessFields * * @return Array */ public function prepare_export(User $user, Filesystem $filesystem, array $subdefs, $rename_title, $includeBusinessFields) { if (!is_array($subdefs)) { throw new Exception('No subdefs given'); } $includeBusinessFields = !!$includeBusinessFields; $files = []; $n_files = 0; $file_names = []; $size = 0; $unicode = new \unicode(); foreach ($this->elements as $download_element) { $id = count($files); $files[$id] = ['base_id' => $download_element->get_base_id(), 'record_id' => $download_element->get_record_id(), 'original_name' => '', 'export_name' => '', 'subdefs' => []]; $BF = false; if ($includeBusinessFields && $this->app['acl']->get($user)->has_right_on_base($download_element->get_base_id(), 'canmodifrecord')) { $BF = true; } $desc = $this->app['serializer.caption']->serialize($download_element->get_caption(), CaptionSerializer::SERIALIZE_XML, $BF); $files[$id]['original_name'] = $files[$id]['export_name'] = $download_element->get_original_name(true); $files[$id]['original_name'] = trim($files[$id]['original_name']) != '' ? $files[$id]['original_name'] : $id; $infos = pathinfo($files[$id]['original_name']); $extension = isset($infos['extension']) ? $infos['extension'] : ''; if ($rename_title) { $title = strip_tags($download_element->get_title(null, null, true)); $files[$id]['export_name'] = $unicode->remove_nonazAZ09($title, true, true, true); } else { $files[$id]["export_name"] = $infos['filename']; } $sizeMaxAjout = 0; $sizeMaxExt = 0; $sd = $download_element->get_subdefs(); foreach ($download_element->get_downloadable() as $name => $properties) { if ($properties === false || !in_array($name, $subdefs)) { continue; } if (!in_array($name, ['caption', 'caption-yaml']) && !isset($sd[$name])) { continue; } set_time_limit(100); $subdef_export = $subdef_alive = false; $n_files++; $tmp_pathfile = ['path' => null, 'file' => null]; switch ($properties['class']) { case 'caption': case 'caption-yaml': $subdef_export = true; $subdef_alive = true; break; case 'thumbnail': $tmp_pathfile = ['path' => $sd[$name]->get_path(), 'file' => $sd[$name]->get_file()]; $subdef_export = true; $subdef_alive = true; break; case 'document': $subdef_export = true; $path = \recordutils_image::stamp($this->app, $sd[$name]); $tmp_pathfile = ['path' => $sd[$name]->get_path(), 'file' => $sd[$name]->get_file()]; if (file_exists($path)) { $tmp_pathfile = ['path' => dirname($path), 'file' => basename($path)]; $subdef_alive = true; } break; case 'preview': $subdef_export = true; $tmp_pathfile = ['path' => $sd[$name]->get_path(), 'file' => $sd[$name]->get_file()]; if (!$this->app['acl']->get($user)->has_right_on_base($download_element->get_base_id(), "nowatermark") && !$this->app['acl']->get($user)->has_preview_grant($download_element) && $sd[$name]->get_type() == media_subdef::TYPE_IMAGE) { $path = recordutils_image::watermark($this->app, $sd[$name]); if (file_exists($path)) { $tmp_pathfile = ['path' => dirname($path), 'file' => basename($path)]; $subdef_alive = true; } } else { $subdef_alive = true; } break; } if ($subdef_export === true && $subdef_alive === true) { switch ($properties['class']) { case 'caption': if ($name == 'caption-yaml') { $suffix = '_captionyaml'; $extension = 'yml'; $mime = 'text/x-yaml'; } else { $suffix = '_caption'; $extension = 'xml'; $mime = 'text/xml'; } $files[$id]["subdefs"][$name]["ajout"] = $suffix; $files[$id]["subdefs"][$name]["exportExt"] = $extension; $files[$id]["subdefs"][$name]["label"] = $properties['label']; $files[$id]["subdefs"][$name]["path"] = null; $files[$id]["subdefs"][$name]["file"] = null; $files[$id]["subdefs"][$name]["size"] = 0; $files[$id]["subdefs"][$name]["folder"] = $download_element->get_directory(); $files[$id]["subdefs"][$name]["mime"] = $mime; break; case 'document': case 'preview': case 'thumbnail': $infos = pathinfo(p4string::addEndSlash($tmp_pathfile["path"]) . $tmp_pathfile["file"]); $files[$id]["subdefs"][$name]["ajout"] = $name == 'document' ? '' : "_" . $name; $files[$id]["subdefs"][$name]["path"] = $tmp_pathfile["path"]; $files[$id]["subdefs"][$name]["file"] = $tmp_pathfile["file"]; $files[$id]["subdefs"][$name]["label"] = $properties['label']; $files[$id]["subdefs"][$name]["size"] = $sd[$name]->get_size(); $files[$id]["subdefs"][$name]["mime"] = $sd[$name]->get_mime(); $files[$id]["subdefs"][$name]["folder"] = $download_element->get_directory(); $files[$id]["subdefs"][$name]["exportExt"] = isset($infos['extension']) ? $infos['extension'] : ''; $size += $sd[$name]->get_size(); break; } $longueurAjoutCourant = mb_strlen($files[$id]["subdefs"][$name]["ajout"]); $sizeMaxAjout = max($longueurAjoutCourant, $sizeMaxAjout); $longueurExtCourant = mb_strlen($files[$id]["subdefs"][$name]["exportExt"]); $sizeMaxExt = max($longueurExtCourant, $sizeMaxExt); } } $max_length = 31 - $sizeMaxExt - $sizeMaxAjout; $name = $files[$id]["export_name"]; $start_length = mb_strlen($name); if ($start_length > $max_length) { $name = mb_substr($name, 0, $max_length); } $n = 1; while (in_array(mb_strtolower($name), $file_names)) { $n++; $suffix = "-" . $n; // pour diese si besoin $max_length = 31 - $sizeMaxExt - $sizeMaxAjout - mb_strlen($suffix); $name = mb_strtolower($files[$id]["export_name"]); if ($start_length > $max_length) { $name = mb_substr($name, 0, $max_length) . $suffix; } else { $name = $name . $suffix; } } $file_names[] = mb_strtolower($name); $files[$id]["export_name"] = $name; $files[$id]["export_name"] = $unicode->remove_nonazAZ09($files[$id]["export_name"], true, true, true); $files[$id]["original_name"] = $unicode->remove_nonazAZ09($files[$id]["original_name"], true, true, true); $i = 0; $name = utf8_decode($files[$id]["export_name"]); $tmp_name = ""; $good_keys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_', '.', '#']; while (isset($name[$i])) { if (!in_array(mb_strtolower($name[$i]), $good_keys)) { $tmp_name .= '_'; } else { $tmp_name .= $name[$i]; } $tmp_name = str_replace('__', '_', $tmp_name); $i++; } $files[$id]["export_name"] = $tmp_name; if (in_array('caption', $subdefs)) { $caption_dir = $this->app['root.path'] . '/tmp/desc_tmp/' . time() . $this->app['authentication']->getUser()->getId() . '/'; $filesystem->mkdir($caption_dir, 0750); $desc = $this->app['serializer.caption']->serialize($download_element->get_caption(), CaptionSerializer::SERIALIZE_XML, $BF); $file = $files[$id]["export_name"] . $files[$id]["subdefs"]['caption']["ajout"] . '.' . $files[$id]["subdefs"]['caption']["exportExt"]; $path = $caption_dir; file_put_contents($path . $file, $desc); $files[$id]["subdefs"]['caption']["path"] = $path; $files[$id]["subdefs"]['caption']["file"] = $file; $files[$id]["subdefs"]['caption']["size"] = filesize($path . $file); $files[$id]["subdefs"]['caption']['businessfields'] = $BF ? '1' : '0'; } if (in_array('caption-yaml', $subdefs)) { $caption_dir = $this->app['root.path'] . '/tmp/desc_tmp/' . time() . $this->app['authentication']->getUser()->getId() . '/'; $filesystem->mkdir($caption_dir, 0750); $desc = $this->app['serializer.caption']->serialize($download_element->get_caption(), CaptionSerializer::SERIALIZE_YAML, $BF); $file = $files[$id]["export_name"] . $files[$id]["subdefs"]['caption-yaml']["ajout"] . '.' . $files[$id]["subdefs"]['caption-yaml']["exportExt"]; $path = $caption_dir; file_put_contents($path . $file, $desc); $files[$id]["subdefs"]['caption-yaml']["path"] = $path; $files[$id]["subdefs"]['caption-yaml']["file"] = $file; $files[$id]["subdefs"]['caption-yaml']["size"] = filesize($path . $file); $files[$id]["subdefs"]['caption-yaml']['businessfields'] = $BF ? '1' : '0'; } } $this->list = ['files' => $files, 'names' => $file_names, 'size' => $size, 'count' => $n_files]; return $this->list; }