From cefb55f4d5be34917dff6cb8ed0d675bbfe551d0 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Wed, 23 May 2012 20:01:36 +0200 Subject: [PATCH] Implement Etags for medias --- lib/Alchemy/Phrasea/Application/Overview.php | 20 +++++++++++++++----- lib/classes/media/subdef.class.php | 16 ++++++---------- lib/classes/record/adapter.class.php | 1 + templates/web/prod/upload/upload.html.twig | 13 ++++++++----- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/lib/Alchemy/Phrasea/Application/Overview.php b/lib/Alchemy/Phrasea/Application/Overview.php index 03bbc55d13..7c1479e0d4 100644 --- a/lib/Alchemy/Phrasea/Application/Overview.php +++ b/lib/Alchemy/Phrasea/Application/Overview.php @@ -12,6 +12,7 @@ namespace Alchemy\Phrasea\Application; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Request; /** * @@ -28,7 +29,7 @@ return call_user_func( $appbox = \appbox::get_instance($app['Core']); $session = $appbox->get_session(); - $deliver_content = function(\Session_Handler $session, \record_adapter $record, $subdef, $watermark, $stamp, $app) { + $deliver_content = function(Request $request, \Session_Handler $session, \record_adapter $record, $subdef, $watermark, $stamp, $app) { $file = $record->get_subdef($subdef); @@ -57,7 +58,16 @@ return call_user_func( } - return \set_export::stream_file($pathOut, $file->get_file(), $file->get_mime(), 'attachment'); + $response = \set_export::stream_file($pathOut, $file->get_file(), $file->get_mime(), 'inline'); + + /* @var $response \Symfony\Component\HttpFoundation\Response */ + if ($file->getEtag()) { + $response->setEtag($file->getEtag()); + $response->setLastModified($file->get_modification_date()); + $response->isNotModified($request); + } + + return $response; }; $app->get('/datafiles/{sbas_id}/{record_id}/{subdef}/', function($sbas_id, $record_id, $subdef) use ($app, $session, $deliver_content) { @@ -109,7 +119,7 @@ return call_user_func( } } - return $deliver_content($session, $record, $subdef, $watermark, $stamp, $app); + return $deliver_content($app['request'], $session, $record, $subdef, $watermark, $stamp, $app); })->assert('sbas_id', '\d+')->assert('record_id', '\d+'); @@ -165,7 +175,7 @@ return call_user_func( } } - return $deliver_content($session, $record, $subdef, $watermark, $stamp, $app); + return $deliver_content($app['request'], $session, $record, $subdef, $watermark, $stamp, $app); } else { $collection = \collection::get_from_base_id($record->get_base_id()); switch ($collection->get_pub_wm()) { @@ -182,7 +192,7 @@ return call_user_func( } } - return $deliver_content($session, $record, $subdef, $watermark, $stamp, $app); + return $deliver_content($app['request'], $session, $record, $subdef, $watermark, $stamp, $app); } ) ->assert('sbas_id', '\d+')->assert('record_id', '\d+'); diff --git a/lib/classes/media/subdef.class.php b/lib/classes/media/subdef.class.php index b665a22ae5..6c18ad0fe5 100644 --- a/lib/classes/media/subdef.class.php +++ b/lib/classes/media/subdef.class.php @@ -149,10 +149,7 @@ class media_subdef extends media_abstract implements cache_cacheableInterface $this->record = $record; $this->load($substitute); - $nowtime = new DateTime('-3 days'); - $random = $record->get_modification_date() > $nowtime; - - $this->generate_url($random); + $this->generate_url(); return $this; } @@ -349,7 +346,7 @@ class media_subdef extends media_abstract implements cache_cacheableInterface public function getEtag() { if ( ! $this->etag && $this->is_physically_present()) { - $this->setEtag(md5_file($this->get_pathfile())); + $this->setEtag(md5(time() . $this->get_pathfile())); } return $this->etag; @@ -364,8 +361,6 @@ class media_subdef extends media_abstract implements cache_cacheableInterface $stmt->execute(array(':subdef_id' => $this->subdef_id, ':etag' => $etag)); $stmt->closeCursor(); - $this->delete_data_from_cache(); - return $this; } @@ -728,6 +723,7 @@ class media_subdef extends media_abstract implements cache_cacheableInterface $stmt->closeCursor(); $subdef = new self($record, $name); + $subdef->delete_data_from_cache(); if ($subdef->get_permalink() instanceof media_Permalink_Adapter) { $subdef->get_permalink()->delete_data_from_cache(); @@ -743,7 +739,7 @@ class media_subdef extends media_abstract implements cache_cacheableInterface * @param boolean $random * @return string */ - protected function generate_url($random = false) + protected function generate_url() { if (in_array($this->mime, array('video/mp4'))) { $token = p4file::apache_tokenize($this->get_pathfile()); @@ -756,8 +752,7 @@ class media_subdef extends media_abstract implements cache_cacheableInterface $this->url = "/datafiles/" . $this->record->get_sbas_id() . "/" . $this->record->get_record_id() . "/" - . $this->get_name() . "/" - . ($random ? '?' . mt_rand(10000, 99999) : ''); + . $this->get_name() . "/"; return; } @@ -784,6 +779,7 @@ class media_subdef extends media_abstract implements cache_cacheableInterface public function delete_data_from_cache($option = null) { + $this->setEtag(null); $databox = $this->get_record()->get_databox(); return $databox->delete_data_from_cache($this->get_cache_key($option)); diff --git a/lib/classes/record/adapter.class.php b/lib/classes/record/adapter.class.php index a120006fc5..fd8888ec7a 100644 --- a/lib/classes/record/adapter.class.php +++ b/lib/classes/record/adapter.class.php @@ -1016,6 +1016,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface path = :path, size = :size, substit = 1 + updated_on = now() WHERE name = :name AND record_id = :record_id'; $params = array( diff --git a/templates/web/prod/upload/upload.html.twig b/templates/web/prod/upload/upload.html.twig index bccca45fb7..62b5b4df30 100644 --- a/templates/web/prod/upload/upload.html.twig +++ b/templates/web/prod/upload/upload.html.twig @@ -39,7 +39,7 @@
{% trans 'upload:: Destination (collection) :' %} :
{# collections list #} {% if collections|length > 0 %} - {% for sbasId, availableCollections in collections %} {% for collection in availableCollections['databox_collections'] %} @@ -55,7 +55,7 @@
{% trans 'upload:: Status :' %} :
- {% for status in availableCollections['databox'].get_statusbits() %} + {% for bit, status in availableCollections['databox'].get_statusbits() %}
{{ status['name']|title }}
@@ -67,10 +67,10 @@ {% endif %} {{ status['labeloff']|default('off') }} - +
- + {{ status['labelon']|default('on') }} {% if status['img_on'] is not empty %} @@ -108,7 +108,10 @@ {# download box #} -
+
+
    +
+