Fix #1127 : Update permalinks API to expose a file extension

This commit is contained in:
Romain Neutron
2013-03-26 12:37:19 +01:00
parent 04fece1713
commit a1ace8a7ba
6 changed files with 226 additions and 145 deletions

View File

@@ -155,76 +155,100 @@ return call_user_func(
return $deliver_content($app['request'], $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+'); })->assert('sbas_id', '\d+')->assert('record_id', '\d+');
$deliverPermaview = function ($sbas_id, $record_id, $subdef, $key, $app) {
$databox = \databox::get_instance((int) $sbas_id);
$record = \media_Permalink_Adapter::challenge_token($databox, $key, $record_id, $subdef);
if (!($record instanceof \record_adapter))
throw new \Exception('bad luck');
/* @var $twig \Twig_Environment */
$twig = $app['Core']->getTwig();
$params = array(
'subdef_name' => $subdef
, 'module_name' => 'overview'
, 'module' => 'overview'
, 'view' => 'overview'
, 'record' => $record
);
return $twig->render('overview.twig', $params);
};
$app->get('/permalink/v1/{sbas_id}/{record_id}/{subdef}/', function (Request $request, $sbas_id, $record_id, $subdef) use ($app, $deliverPermaview) {
$key = $request->query->get('token');
return $deliverPermaview($sbas_id, $record_id, $subdef, $key, $app);
})->assert('sbas_id', '\d+')->assert('record_id', '\d+');
$app->get('/permalink/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/view/' $app->get('/permalink/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/view/'
, function($label, $sbas_id, $record_id, $key, $subdef) use($app) { , function($label, $sbas_id, $record_id, $key, $subdef) use ($app, $deliverPermaview) {
return $deliverPermaview($sbas_id, $record_id, $subdef, $key, $app);
$databox = \databox::get_instance((int) $sbas_id);
$record = \media_Permalink_Adapter::challenge_token($databox, $key, $record_id, $subdef);
if (!($record instanceof \record_adapter))
throw new \Exception('bad luck');
/* @var $twig \Twig_Environment */
$twig = $app['Core']->getTwig();
$params = array(
'subdef_name' => $subdef
, 'module_name' => 'overview'
, 'module' => 'overview'
, 'view' => 'overview'
, 'record' => $record
);
return $twig->render('overview.twig', $params);
})->assert('sbas_id', '\d+')->assert('record_id', '\d+'); })->assert('sbas_id', '\d+')->assert('record_id', '\d+');
$app->get('/permalink/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/' $deliverPermalink = function ($label, $sbas_id, $record_id, $key, $subdef, $app, $session, $deliver_content) {
, function($label, $sbas_id, $record_id, $key, $subdef) use ($app, $session, $deliver_content) {
$databox = \databox::get_instance((int) $sbas_id);
$record = \media_Permalink_Adapter::challenge_token($databox, $key, $record_id, $subdef);
if (!($record instanceof \record_adapter)) $databox = \databox::get_instance((int) $sbas_id);
throw new \Exception('bad luck'); $record = \media_Permalink_Adapter::challenge_token($databox, $key, $record_id, $subdef);
$watermark = $stamp = false; if (!($record instanceof \record_adapter))
throw new \Exception('bad luck');
if ($session->is_authenticated()) { $watermark = $stamp = false;
$user = \User_Adapter::getInstance($session->get_usr_id(), \appbox::get_instance($app['Core']));
$watermark = !$user->ACL()->has_right_on_base($record->get_base_id(), 'nowatermark'); if ($session->is_authenticated()) {
$user = \User_Adapter::getInstance($session->get_usr_id(), \appbox::get_instance($app['Core']));
if ($watermark) { $watermark = !$user->ACL()->has_right_on_base($record->get_base_id(), 'nowatermark');
$em = $app['Core']->getEntityManager(); if ($watermark) {
$repository = $em->getRepository('\Entities\BasketElement'); $em = $app['Core']->getEntityManager();
if (count($repository->findReceivedValidationElementsByRecord($record, $user)) > 0) { $repository = $em->getRepository('\Entities\BasketElement');
$watermark = false;
} elseif (count($repository->findReceivedElementsByRecord($record, $user)) > 0) {
$watermark = false;
}
}
return $deliver_content($app['request'], $session, $record, $subdef, $watermark, $stamp, $app); if (count($repository->findReceivedValidationElementsByRecord($record, $user)) > 0) {
} else { $watermark = false;
$collection = \collection::get_from_base_id($record->get_base_id()); } elseif (count($repository->findReceivedElementsByRecord($record, $user)) > 0) {
switch ($collection->get_pub_wm()) { $watermark = false;
default:
case 'none':
$watermark = false;
break;
case 'stamp':
$stamp = true;
break;
case 'wm':
$watermark = false;
break;
}
} }
}
return $deliver_content($app['request'], $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()) {
default:
case 'none':
$watermark = false;
break;
case 'stamp':
$stamp = true;
break;
case 'wm':
$watermark = false;
break;
}
}
return $deliver_content($app['request'], $session, $record, $subdef, $watermark, $stamp, $app);
};
$app->get('/permalink/v1/{sbas_id}/{record_id}/{subdef}/{label}',
function (Request $request, $sbas_id, $record_id, $subdef, $label) use ($app, $deliverPermalink, $session, $deliver_content) {
$key = $request->query->get('token');
return $deliverPermalink($label, $sbas_id, $record_id, $key, $subdef, $app, $session, $deliver_content);
})
->assert('sbas_id', '\d+')
->assert('record_id', '\d+');
$app->get('/permalink/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/'
, function($label, $sbas_id, $record_id, $key, $subdef) use ($app, $deliverPermalink, $session, $deliver_content) {
return $deliverPermalink($label, $sbas_id, $record_id, $key, $subdef, $app, $session, $deliver_content);
} }
) )
->assert('sbas_id', '\d+')->assert('record_id', '\d+'); ->assert('sbas_id', '\d+')->assert('record_id', '\d+');

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Exception\RuntimeException;
/** /**
* *
* @package subdefs * @package subdefs
@@ -144,13 +146,14 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea
{ {
$registry = registry::get_instance(); $registry = registry::get_instance();
return sprintf('%spermalink/v1/%s/%d/%d/%s/%s/' return sprintf('%spermalink/v1/%d/%d/%s/%s.%s?token=%s',
, $registry->get('GV_ServerName') $registry->get('GV_ServerName'),
, $this->get_label() $this->media_subdef->get_sbas_id(),
, $this->media_subdef->get_sbas_id() $this->media_subdef->get_record_id(),
, $this->media_subdef->get_record_id() $this->media_subdef->get_name(),
, $this->get_token() $this->get_label(),
, $this->media_subdef->get_name() pathinfo($this->media_subdef->get_record()->get_original_name(), PATHINFO_EXTENSION),
$this->get_token()
); );
} }
@@ -161,13 +164,12 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea
*/ */
public function get_page(registryInterface $registry) public function get_page(registryInterface $registry)
{ {
return sprintf('%spermalink/v1/%s/%d/%d/%s/%s/view/' return sprintf('%spermalink/v1/%d/%d/%s/?token=%s',
, $registry->get('GV_ServerName') $registry->get('GV_ServerName'),
, $this->get_label() $this->media_subdef->get_sbas_id(),
, $this->media_subdef->get_sbas_id() $this->media_subdef->get_record_id(),
, $this->media_subdef->get_record_id() $this->media_subdef->get_name(),
, $this->get_token() $this->get_token()
, $this->media_subdef->get_name()
); );
} }
@@ -226,7 +228,7 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea
{ {
$unicode_processor = new unicode(); $unicode_processor = new unicode();
$label = trim($label); $label = trim($label) ? trim($label) : 'untitled';
while (strpos($label, ' ') !== false) while (strpos($label, ' ') !== false)
$label = str_replace(' ', ' ', $label); $label = str_replace(' ', ' ', $label);
@@ -333,13 +335,21 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea
, ':activated' => '1' , ':activated' => '1'
); );
$error = null;
$stmt = $databox->get_connection()->prepare($sql); $stmt = $databox->get_connection()->prepare($sql);
$stmt->execute($params); try {
$stmt->execute($params);
} catch (\PDOException $e) {
$error = $e;
}
$stmt->closeCursor(); $stmt->closeCursor();
unset($stmt);
if ($error) {
throw new RuntimeException('Permalink already exists', $e->getCode(), $e);
}
$permalink = self::getPermalink($databox, $media_subdef); $permalink = self::getPermalink($databox, $media_subdef);
$permalink->set_label(strip_tags($media_subdef->get_record()->get_title())); $permalink->set_label(strip_tags($media_subdef->get_record()->get_title(false, null, true)));
return $permalink; return $permalink;
} }

View File

@@ -0,0 +1,62 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2012 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class patch_379 implements patchInterface
{
/**
*
* @var string
*/
private $release = '3.7.9';
/**
*
* @var Array
*/
private $concern = array(base::DATA_BOX);
/**
*
* @return string
*/
public function get_release()
{
return $this->release;
}
public function require_all_upgrades()
{
return false;
}
/**
*
* @return Array
*/
public function concern()
{
return $this->concern;
}
/**
* @param base $databox
*/
public function apply(base &$appbox)
{
$sql = 'UPDATE permalinks SET label = "untitled" WHERE label = ""';
$stmt = $appbox->get_connection()->prepare($sql);
$stmt->execute();
$stmt->closeCursor();
return true;
}
}

View File

@@ -96,6 +96,7 @@ class ApplicationOverviewTest extends PhraseanetWebTestCaseAuthenticatedAbstract
{ {
$appbox = appbox::get_instance(\bootstrap::getCore()); $appbox = appbox::get_instance(\bootstrap::getCore());
$this->assertTrue($appbox->get_session()->is_authenticated()); $this->assertTrue($appbox->get_session()->is_authenticated());
$this->get_a_permalinkBCcompatibility();
$this->get_a_permalink(); $this->get_a_permalink();
} }
@@ -104,6 +105,7 @@ class ApplicationOverviewTest extends PhraseanetWebTestCaseAuthenticatedAbstract
$appbox = appbox::get_instance(\bootstrap::getCore()); $appbox = appbox::get_instance(\bootstrap::getCore());
$appbox->get_session()->logout(); $appbox->get_session()->logout();
$this->assertFalse($appbox->get_session()->is_authenticated()); $this->assertFalse($appbox->get_session()->is_authenticated());
$this->get_a_permalinkBCcompatibility();
$this->get_a_permalink(); $this->get_a_permalink();
} }
@@ -129,8 +131,7 @@ class ApplicationOverviewTest extends PhraseanetWebTestCaseAuthenticatedAbstract
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
protected function get_a_permalinkBCcompatibility()
protected function get_a_permalink()
{ {
$token = static::$records['record_1']->get_preview()->get_permalink()->get_token(); $token = static::$records['record_1']->get_preview()->get_permalink()->get_token();
$url = '/permalink/v1/whateverIwannt/' . static::$records['record_1']->get_sbas_id() . '/' . static::$records['record_1']->get_record_id() . '/' . $token . '/preview/'; $url = '/permalink/v1/whateverIwannt/' . static::$records['record_1']->get_sbas_id() . '/' . static::$records['record_1']->get_record_id() . '/' . $token . '/preview/';
@@ -145,4 +146,21 @@ class ApplicationOverviewTest extends PhraseanetWebTestCaseAuthenticatedAbstract
$response = $this->client->getResponse(); $response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
protected function get_a_permalink()
{
$token = static::$records['record_1']->get_preview()->get_permalink()->get_token();
$url = '/permalink/v1/' . static::$records['record_1']->get_sbas_id() . '/' . static::$records['record_1']->get_record_id() . '/preview/whateverIwannt.jpg?token=' . $token . '';
$crawler = $this->client->request('GET', $url);
$response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$url = '/permalink/v1/' . static::$records['record_1']->get_sbas_id() . '/' . static::$records['record_1']->get_record_id() . '/preview/?token=' . $token . '';
$crawler = $this->client->request('GET', $url);
$response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html; charset=UTF-8', $response->headers->get('Content-Type'));
}
} }

View File

@@ -174,10 +174,10 @@ class ControllerRssFeedTest extends \PhraseanetWebTestCaseAbstract
public function createApplication() public function createApplication()
{ {
$app = require __DIR__ . '/../../../../../lib/Alchemy/Phrasea/Application/Root.php'; $app = require __DIR__ . '/../../../../../lib/Alchemy/Phrasea/Application/Root.php';
$app['debug'] = true; $app['debug'] = true;
unset($app['exception_handler']); unset($app['exception_handler']);
return $app; return $app;
} }
@@ -347,7 +347,7 @@ class ControllerRssFeedTest extends \PhraseanetWebTestCaseAbstract
{ {
$this->client->request("GET", "/feeds/feed/0/rss/"); $this->client->request("GET", "/feeds/feed/0/rss/");
} }
/** /**
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/ */
@@ -550,7 +550,7 @@ class ControllerRssFeedTest extends \PhraseanetWebTestCaseAbstract
$is_thumbnail = false; $is_thumbnail = false;
$record = $entry_item->get_record(); $record = $entry_item->get_record();
if (substr($current_attributes["url"], 0 - strlen("/preview/")) == "/preview/") { if (false !== strpos($current_attributes["url"], 'preview')) {
$ressource = $record->get_subdef('preview'); $ressource = $record->get_subdef('preview');
} else { } else {
$ressource = $record->get_thumbnail(); $ressource = $record->get_thumbnail();
@@ -679,7 +679,7 @@ class ControllerRssFeedTest extends \PhraseanetWebTestCaseAbstract
foreach ($fields as $key_field => $field) { foreach ($fields as $key_field => $field) {
$role = true; $role = true;
if(isset($field["media_field"]['attributes']['role'])){ if(isset($field["media_field"]['attributes']['role'])){
$role = false; $role = false;
foreach($node->attributes as $attr){ foreach($node->attributes as $attr){
@@ -689,7 +689,7 @@ class ControllerRssFeedTest extends \PhraseanetWebTestCaseAbstract
} }
} }
} }
if ($field["media_field"]["name"] == $node->nodeName && $role != false) { if ($field["media_field"]["name"] == $node->nodeName && $role != false) {
if ($p4field = $entry_item->get_record()->get_caption()->get_dc_field($field["dc_field"])) { if ($p4field = $entry_item->get_record()->get_caption()->get_dc_field($field["dc_field"])) {

View File

@@ -16,6 +16,12 @@ class media_Permalink_AdapterTest extends PhraseanetPHPUnitAbstract
static::$object = media_Permalink_Adapter::getPermalink($databox, static::$records['record_1']->get_subdef('document')); static::$object = media_Permalink_Adapter::getPermalink($databox, static::$records['record_1']->get_subdef('document'));
} }
public function testGet_label()
{
$this->assertInternalType('string', static::$object->get_label());
$this->assertEquals('test001', static::$object->get_label());
}
public function testGetPermalink() public function testGetPermalink()
{ {
$this->assertTrue((static::$object instanceof media_Permalink_Adapter)); $this->assertTrue((static::$object instanceof media_Permalink_Adapter));
@@ -36,7 +42,7 @@ class media_Permalink_AdapterTest extends PhraseanetPHPUnitAbstract
static::$object->set_label('coucou les chicos'); static::$object->set_label('coucou les chicos');
$this->assertEquals('coucou-les-chicos', static::$object->get_label()); $this->assertEquals('coucou-les-chicos', static::$object->get_label());
static::$object->set_label(''); static::$object->set_label('');
$this->assertEquals('', static::$object->get_label()); $this->assertEquals('untitled', static::$object->get_label());
static::$object->set_label('JE ANp ra&é"\/,;:!§/.?%µ*ù$]@^\[{#~234567890°+\'(-è_çà'); static::$object->set_label('JE ANp ra&é"\/,;:!§/.?%µ*ù$]@^\[{#~234567890°+\'(-è_çà');
$this->assertEquals('JE-ANp-raeu234567890-e_ca', static::$object->get_label()); $this->assertEquals('JE-ANp-raeu234567890-e_ca', static::$object->get_label());
} }
@@ -44,98 +50,59 @@ class media_Permalink_AdapterTest extends PhraseanetPHPUnitAbstract
public function testGet_url() public function testGet_url()
{ {
$registry = registry::get_instance(); $registry = registry::get_instance();
$url = $registry->get('GV_ServerName') . 'permalink/v1/' . static::$object->get_label() . '/' . static::$records['record_1']->get_sbas_id() . '/' . static::$records['record_1']->get_record_id() . '/' . $url = $registry->get('GV_ServerName') . 'permalink/v1/'
static::$object->get_token() . '/document/'; . static::$records['record_1']->get_sbas_id() . '/'
. static::$records['record_1']->get_record_id()
. '/document/' . static::$object->get_label()
. '.' . pathinfo(static::$records['record_1']->get_original_name(), PATHINFO_EXTENSION)
. '?token=' . static::$object->get_token();
$this->assertEquals($url, static::$object->get_url($registry)); $this->assertEquals($url, static::$object->get_url($registry));
} }
/**
* @todo Implement testGet_page().
*/
public function testGet_page() public function testGet_page()
{ {
// Remove the following lines when you implement this test. $registry = registry::get_instance();
$this->markTestIncomplete( $url = $registry->get('GV_ServerName') . 'permalink/v1/'
'This test has not been implemented yet.' . static::$records['record_1']->get_sbas_id() . '/'
); . static::$records['record_1']->get_record_id()
. '/document/'
. '?token=' . static::$object->get_token();
$this->assertEquals($url, static::$object->get_page($registry));
} }
/**
* @todo Implement testGet_id().
*/
public function testGet_id() public function testGet_id()
{ {
// Remove the following lines when you implement this test. $this->assertInternalType('integer', static::$object->get_id());
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
} }
/**
* @todo Implement testGet_token().
*/
public function testGet_token() public function testGet_token()
{ {
// Remove the following lines when you implement this test. $this->assertInternalType('string', static::$object->get_token());
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
} }
/**
* @todo Implement testGet_is_activated().
*/
public function testGet_is_activated() public function testGet_is_activated()
{ {
// Remove the following lines when you implement this test. $this->assertInternalType('boolean', static::$object->get_is_activated());
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$this->assertTrue(is_bool(static::$object->get_is_activated));
} }
/**
* @todo Implement testGet_created_on().
*/
public function testGet_created_on() public function testGet_created_on()
{ {
// Remove the following lines when you implement this test. $this->assertInstanceOf('DateTime', static::$object->get_created_on());
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
} }
/**
* @todo Implement testGet_last_modified().
*/
public function testGet_last_modified() public function testGet_last_modified()
{ {
// Remove the following lines when you implement this test. $this->assertInstanceOf('DateTime', static::$object->get_last_modified());
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
} }
/** /**
* @todo Implement testGet_label(). * @expectedException Alchemy\Phrasea\Exception\RuntimeException
*/ */
public function testGet_label() public function testCreateAPermalinkAlreadyCreated()
{ {
// Remove the following lines when you implement this test. $databox = static::$records['record_1']->get_databox();
$this->markTestIncomplete( media_Permalink_Adapter::create($databox, static::$records['record_1']->get_subdef('document'));
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testCreate().
*/
public function testCreate()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
} }
} }