Пример #1
0
 public function generateSubdefs(\record_adapter $record, array $wanted_subdefs = null)
 {
     if (null === ($subdefs = $record->get_databox()->get_subdef_structure()->getSubdefGroup($record->get_type()))) {
         $this->logger->info(sprintf('Nothing to do for %s', $record->get_type()));
         return;
     }
     foreach ($subdefs as $subdef) {
         $subdefname = $subdef->get_name();
         if ($wanted_subdefs && !in_array($subdefname, $wanted_subdefs)) {
             continue;
         }
         $pathdest = null;
         if ($record->has_subdef($subdefname) && $record->get_subdef($subdefname)->is_physically_present()) {
             $pathdest = $record->get_subdef($subdefname)->get_pathfile();
             $record->get_subdef($subdefname)->remove_file();
             $this->logger->info(sprintf('Removed old file for %s', $subdefname));
             $record->clearSubdefCache($subdefname);
         }
         $pathdest = $this->generateSubdefPathname($record, $subdef, $pathdest);
         $this->logger->addInfo(sprintf('Generating subdef %s to %s', $subdefname, $pathdest));
         $this->generateSubdef($record, $subdef, $pathdest);
         if ($this->filesystem->exists($pathdest)) {
             $media = $this->mediavorus->guess($pathdest);
             \media_subdef::create($this->app, $record, $subdef->get_name(), $media);
         }
         $record->clearSubdefCache($subdefname);
         $this->app['dispatcher']->dispatch(RecordEvents::SUB_DEFINITION_CREATED, new RecordSubDefinitionCreatedEvent($record, $subdefname));
     }
     return $this;
 }
Пример #2
0
 /**
  * @covers media_subdef::readTechnicalDatas
  */
 public function testReadTechnicalDatas()
 {
     $technical_datas = self::$objectPresent->readTechnicalDatas(self::$DI['app']['mediavorus']);
     $this->assertArrayHasKey(media_subdef::TC_DATA_WIDTH, $technical_datas);
     $this->assertArrayHasKey(media_subdef::TC_DATA_HEIGHT, $technical_datas);
     $this->assertArrayHasKey(media_subdef::TC_DATA_CHANNELS, $technical_datas);
     $this->assertArrayHasKey(media_subdef::TC_DATA_COLORDEPTH, $technical_datas);
     $this->assertArrayHasKey(media_subdef::TC_DATA_MIMETYPE, $technical_datas);
     $this->assertArrayHasKey(media_subdef::TC_DATA_FILESIZE, $technical_datas);
     $technical_datas = self::$objectNotPresent->readTechnicalDatas(self::$DI['app']['mediavorus']);
     $this->assertEquals([], $technical_datas);
 }
Пример #3
0
 public function substitute(\record_adapter $record, $name, MediaInterface $media)
 {
     $newfilename = $record->get_record_id() . '_0_' . $name . '.' . $media->getFile()->getExtension();
     $subdef_def = false;
     if ($name == 'document') {
         $baseprefs = $record->get_databox()->get_sxml_structure();
         $pathhd = \p4string::addEndSlash((string) $baseprefs->path);
         $filehd = $record->get_record_id() . "_document." . strtolower($media->getFile()->getExtension());
         $pathhd = \databox::dispatch($this->fs, $pathhd);
         $this->fs->copy($media->getFile()->getRealPath(), $pathhd . $filehd, true);
         $subdefFile = $pathhd . $filehd;
         $meta_writable = true;
     } else {
         $type = $record->isStory() ? 'image' : $record->get_type();
         $subdef_def = $record->get_databox()->get_subdef_structure()->get_subdef($type, $name);
         if ($record->has_subdef($name) && $record->get_subdef($name)->is_physically_present()) {
             $path_file_dest = $record->get_subdef($name)->get_pathfile();
             $record->get_subdef($name)->remove_file();
             $record->clearSubdefCache($name);
         } else {
             $path = \databox::dispatch($this->fs, $subdef_def->get_path());
             $this->fs->mkdir($path, 0750);
             $path_file_dest = $path . $newfilename;
         }
         try {
             $this->alchemyst->turnInto($media->getFile()->getRealPath(), $path_file_dest, $subdef_def->getSpecs());
         } catch (MediaAlchemystException $e) {
             return;
         }
         $subdefFile = $path_file_dest;
         $meta_writable = $subdef_def->meta_writeable();
     }
     $this->fs->chmod($subdefFile, 0760);
     $media = $this->mediavorus->guess($subdefFile);
     \media_subdef::create($this->app, $record, $name, $media);
     $record->delete_data_from_cache(\record_adapter::CACHE_SUBDEFS);
     if ($meta_writable) {
         $record->write_metas();
     }
     if ($name == 'document') {
         $record->rebuild_subdefs();
     }
     $this->dispatcher->dispatch(RecordEvents::MEDIA_SUBSTITUTED, new RecordMediaSubstitutedEvent($record));
 }
Пример #4
0
 protected function assertPermalinkHeaders($url, \media_subdef $subdef, $type_url = "page_url")
 {
     $response = $this->executeRequest($url);
     $this->assertEquals(200, $response->getStatusCode());
     switch ($type_url) {
         case "page_url":
             $this->assertTrue(strpos((string) $response->getHeader('content-type'), "text/html") === 0);
             if ($response->hasHeader('content-length')) {
                 $this->assertNotEquals($subdef->get_size(), (string) $response->getHeader('content-length'));
             }
             break;
         case "url":
             $this->assertTrue(strpos((string) $response->getHeader('content-type'), $subdef->get_mime()) === 0, 'Verify that header ' . (string) $response->getHeader('content-type') . ' contains subdef mime type ' . $subdef->get_mime());
             if ($response->hasHeader('content-length')) {
                 $this->assertEquals($subdef->get_size(), (string) $response->getHeader('content-length'));
             }
             break;
         case "download_url":
             $this->assertTrue(strpos((string) $response->getHeader('content-type'), $subdef->get_mime()) === 0, 'Verify that header ' . (string) $response->getHeader('content-type') . ' contains subdef mime type ' . $subdef->get_mime());
             if ($response->hasHeader('content-length')) {
                 $this->assertEquals($subdef->get_size(), (string) $response->getHeader('content-length'));
             }
             break;
     }
 }
Пример #5
0
 public function get_cache_key($option = null)
 {
     return 'permalink_' . $this->media_subdef->get_subdef_id() . ($option ? '_' . $option : '');
 }
Пример #6
0
 /**
  *
  * @param Application   $app
  * @param \media_subdef $subdef
  *
  * @return boolean|string
  */
 public static function watermark(Application $app, \media_subdef $subdef)
 {
     static $palette;
     if (null === $palette) {
         $palette = new RGB();
     }
     $base_id = $subdef->get_record()->get_base_id();
     if ($subdef->get_name() !== 'preview') {
         return $subdef->get_pathfile();
     }
     if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
         return $subdef->get_pathfile();
     }
     if (!$subdef->is_physically_present()) {
         return false;
     }
     $pathIn = $subdef->get_path() . $subdef->get_file();
     if (!is_file($pathIn)) {
         return false;
     }
     $pathOut = $subdef->get_path() . 'watermark_' . $subdef->get_file();
     // cache
     if (is_file($pathOut)) {
         return $pathOut;
     }
     $in_image = $app['imagine']->open($pathIn);
     $in_size = $in_image->getSize();
     $in_w = $in_size->getWidth();
     $in_h = $in_size->getHeight();
     $wm_file = $app['root.path'] . '/config/wm/' . $base_id;
     if (file_exists($wm_file)) {
         $wm_image = $app['imagine']->open($wm_file);
         $wm_size = $wm_image->getSize();
         $wm_w = $wm_size->getWidth();
         $wm_h = $wm_size->getHeight();
         if ($wm_w / $wm_h > $in_w / $in_h) {
             $wm_size = $wm_size->widen($in_w);
         } else {
             $wm_size = $wm_size->heighten($in_h);
         }
         $wm_image->resize($wm_size);
         $in_image->paste($wm_image, new Point($in_w - $wm_size->getWidth() >> 1, $in_h - $wm_size->getHeight() >> 1))->save($pathOut);
     } else {
         $collname = $subdef->get_record()->get_collection()->get_name();
         $draw = $in_image->draw();
         $black = $palette->color("000000");
         $white = $palette->color("FFFFFF");
         $draw->line(new Point(0, 1), new Point($in_w - 2, $in_h - 1), $black);
         $draw->line(new Point(1, 0), new Point($in_w - 1, $in_h - 2), $white);
         $draw->line(new Point(0, $in_h - 2), new Point($in_w - 2, 0), $black);
         $draw->line(new Point(1, $in_h - 1), new Point($in_w - 1, 1), $white);
         $fsize = max(8, (int) (max($in_w, $in_h) / 30));
         $fonts = [$app['imagine']->font(__DIR__ . '/arial.ttf', $fsize, $black), $app['imagine']->font(__DIR__ . '/arial.ttf', $fsize, $white)];
         $testbox = $fonts[0]->box($collname, 0);
         $tx_w = min($in_w, $testbox->getWidth());
         $tx_h = min($in_h, $testbox->getHeight());
         $x0 = max(1, $in_w - $tx_w >> 1);
         $y0 = max(1, $in_h - $tx_h >> 1);
         for ($i = 0; $i <= 1; $i++) {
             $x = max(1, ($in_w >> 2) - ($tx_w >> 1));
             $draw->text($collname, $fonts[$i], new Point($x - $i, $y0 - $i), 0);
             $x = max(1, $in_w - $x - $tx_w);
             $draw->text($collname, $fonts[$i], new Point($x - $i, $y0 - $i), 0);
             $y = max(1, ($in_h >> 2) - ($tx_h >> 1));
             $draw->text($collname, $fonts[$i], new Point($x0 - $i, $y - $i), 0);
             $y = max(1, $in_h - $y - $tx_h);
             $draw->text($collname, $fonts[$i], new Point($x0 - $i, $y - $i), 0);
         }
     }
     $in_image->save($pathOut);
     if (is_file($pathOut)) {
         return $pathOut;
     }
     return false;
 }
Пример #7
0
 /**
  *
  * @param File        $file
  * @param Application $app
  *
  * @return \record_adapter
  */
 public static function createFromFile(File $file, Application $app)
 {
     $databox = $file->getCollection()->get_databox();
     $sql = 'INSERT INTO record
           (coll_id, record_id, parent_record_id, moddate, credate
             , type, sha256, uuid, originalname, mime)
         VALUES
           (:coll_id, null, :parent_record_id, NOW(), NOW()
           , :type, :sha256, :uuid
           , :originalname, :mime)';
     $stmt = $databox->get_connection()->prepare($sql);
     $stmt->execute([':coll_id' => $file->getCollection()->get_coll_id(), ':parent_record_id' => 0, ':type' => $file->getType() ? $file->getType()->getType() : 'unknown', ':sha256' => $file->getMedia()->getHash('sha256'), ':uuid' => $file->getUUID(true), ':originalname' => $file->getOriginalName(), ':mime' => $file->getFile()->getMimeType()]);
     $record_id = $databox->get_connection()->lastInsertId();
     $record = new self($app, $databox->get_sbas_id(), $record_id);
     try {
         $log_id = $app['phraseanet.logger']($databox)->get_id();
         $sql = 'INSERT INTO log_docs (id, log_id, date, record_id, action, final, comment)
         VALUES (null, :log_id, now(),
           :record_id, "add", :coll_id,"")';
         $stmt = $databox->get_connection()->prepare($sql);
         $stmt->execute([':log_id' => $log_id, ':record_id' => $record_id, ':coll_id' => $file->getCollection()->get_coll_id()]);
         $stmt->closeCursor();
     } catch (\Exception $e) {
         unset($e);
     }
     $pathhd = databox::dispatch($app['filesystem'], trim($databox->get_sxml_structure()->path));
     $newname = $record->get_record_id() . "_document." . pathinfo($file->getOriginalName(), PATHINFO_EXTENSION);
     $app['filesystem']->copy($file->getFile()->getRealPath(), $pathhd . $newname, true);
     $media = $app['mediavorus']->guess($pathhd . $newname);
     media_subdef::create($app, $record, 'document', $media);
     $record->delete_data_from_cache(\record_adapter::CACHE_SUBDEFS);
     $record->insertTechnicalDatas($app['mediavorus']);
     $record->rebuild_subdefs();
     return $record;
 }
Пример #8
0
 /**
  * Php raw implementation of thumbnails.html.twig macro
  * @param \media_subdef $subdef
  * @return array
  */
 private function getDimensions(\media_subdef $subdef)
 {
     $outWidth = $subdef->get_width();
     $outHeight = $subdef->get_height() | $outWidth;
     $thumbnail_height = $subdef->get_height() > 0 ? $subdef->get_height() : 120;
     $thumbnail_width = $subdef->get_width() > 0 ? $subdef->get_width() : 120;
     $subdefRatio = 0;
     $thumbnailRatio = $thumbnail_width / $thumbnail_height;
     if ($outWidth > 0 && $outHeight > 0) {
         $subdefRatio = $outWidth / $outHeight;
     }
     if ($thumbnailRatio > $subdefRatio) {
         if ($outWidth > $thumbnail_width) {
             $outWidth = $thumbnail_width;
         }
         $outHeight = $outWidth / $thumbnail_width * $thumbnail_height;
         $top = ($outHeight - $outHeight) / 2;
     } else {
         if ($outHeight > $thumbnail_height) {
             $outHeight = $thumbnail_height;
         }
         $outWidth = $outHeight * $thumbnail_width / $thumbnail_height;
         $top = ($outHeight - $outHeight) / 2;
     }
     return ['width' => round($outWidth), 'height' => round($outHeight), 'top' => $top];
 }
Пример #9
0
 /**
  * @retrieve detailled informations about one suddef
  *
  * @param  media_subdef $media
  *
  * @return array
  */
 private function list_embedable_media(Application $app, \record_adapter $record, \media_subdef $media)
 {
     if (!$media->is_physically_present()) {
         return null;
     }
     if ($app['authentication']->isAuthenticated()) {
         if ($media->get_name() !== 'document' && false === $app['acl']->get($app['authentication']->getUser())->has_access_to_subdef($record, $media->get_name())) {
             return null;
         }
         if ($media->get_name() === 'document' && !$app['acl']->get($app['authentication']->getUser())->has_right_on_base($record->get_base_id(), 'candwnldhd') && !$app['acl']->get($app['authentication']->getUser())->has_hd_grant($record)) {
             return null;
         }
     }
     if ($media->get_permalink() instanceof \media_Permalink_Adapter) {
         $permalink = $this->list_permalink($media->get_permalink());
     } else {
         $permalink = null;
     }
     return ['name' => $media->get_name(), 'permalink' => $permalink, 'height' => $media->get_height(), 'width' => $media->get_width(), 'filesize' => $media->get_size(), 'devices' => $media->getDevices(), 'player_type' => $media->get_type(), 'mime_type' => $media->get_mime()];
 }
Пример #10
0
 /**
  * @param User          $issuer
  * @param \media_subdef $subdef
  * @param int           $url_ttl
  * @return string
  */
 private function generateSubDefinitionUrl(User $issuer, \media_subdef $subdef, $url_ttl)
 {
     $payload = ['iat' => time(), 'iss' => $issuer->getId(), 'sdef' => [$subdef->get_sbas_id(), $subdef->get_record_id(), $subdef->get_name()]];
     if ($url_ttl >= 0) {
         $payload['exp'] = $payload['iat'] + $url_ttl;
     }
     /** @var SecretProvider $provider */
     $provider = $this->app['provider.secrets'];
     $secret = $provider->getSecretForUser($issuer);
     return $this->app->url('media_accessor', ['token' => JWT::encode($payload, $secret->getToken(), 'HS256', $secret->getId())]);
 }
Пример #11
0
 /**
  * @retrieve detailled informations about one suddef
  *
  * @param  media_subdef $media
  * @return array
  */
 protected function list_embedable_media(media_subdef $media)
 {
     if (!$media->is_physically_present()) {
         return null;
     }
     if ($media->get_permalink() instanceof media_Permalink_Adapter) {
         $permalink = $this->list_permalink($media->get_permalink());
     } else {
         $permalink = null;
     }
     return ['name' => $media->get_name(), 'permalink' => $permalink, 'height' => $media->get_height(), 'width' => $media->get_width(), 'filesize' => $media->get_size(), 'devices' => $media->getDevices(), 'player_type' => $media->get_type(), 'mime_type' => $media->get_mime()];
 }