Add '/prod/download/' controller to download a set of documents

This commit is contained in:
Nicolas Le Goff
2012-10-22 14:15:48 +02:00
parent 6557402d17
commit 175448f562
3 changed files with 161 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ use Alchemy\Phrasea\Controller\Admin\TaskManager;
use Alchemy\Phrasea\Controller\Admin\Users;
use Alchemy\Phrasea\Controller\Prod\Basket;
use Alchemy\Phrasea\Controller\Prod\Bridge;
use Alchemy\Phrasea\Controller\Prod\Download;
use Alchemy\Phrasea\Controller\Prod\Edit;
use Alchemy\Phrasea\Controller\Prod\Export;
use Alchemy\Phrasea\Controller\Prod\Feed;
@@ -134,6 +135,7 @@ return call_user_func(function($environment = null) {
$app->mount('/prod/query/', new Query());
$app->mount('/prod/order/', new Order());
$app->mount('/prod/baskets', new Basket());
$app->mount('/prod/download', new Download());
$app->mount('/prod/story', new Story());
$app->mount('/prod/WorkZone', new WorkZone());
$app->mount('/prod/lists', new UsrLists());

View File

@@ -0,0 +1,111 @@
<?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.
*/
namespace Alchemy\Phrasea\Controller\Prod;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
class Download implements ControllerProviderInterface
{
/**
* {@inheritDoc}
*/
public function connect(Application $app)
{
$controllers = $app['controllers_factory'];
$controllers->before(function(Request $request) use ($app) {
$app['firewall']->requireAuthentication();
});
/**
* Download a set of documents
*
* name : download
*
* description : Download a set of documents
*
* method : POST
*
* parameters : none
*
* return : Redirect Response
*/
$controllers->post('/', $this->call('download'))
->bind('download');
return $controllers;
}
/**
* Download a set of documents
*
* @param Application $app
* @param Request $request
* @return RedirectResponse
*/
public function download(Application $app, Request $request)
{
$lst = $request->request->get('lst');
$ssttid = $request->request->get('ssttid', '');
$subdefs = $request->request->get('obj', array());
$download = new \set_export($app, $lst, $ssttid);
$list = $download->prepare_export(
$app['phraseanet.user'],
$app['filesystem'],
$subdefs,
$request->request->get('title') === 'title' ? true : false,
$request->request->get('businessfields')
);
$list['export_name'] = sprintf('%s.zip', $download->getExportName());
$token = \random::getUrlToken(
$app,
\random::TYPE_DOWNLOAD,
$app['phraseanet.user']->get_id(),
new \DateTime('+3 hours'), // Token lifetime
serialize($list)
);
if (!$token) {
throw new \RuntimeException('Download token could not be generated');
}
$app['events-manager']->trigger('__DOWNLOAD__', array(
'lst' => $lst,
'downloader' => $app['phraseanet.user']->get_id(),
'subdefs' => $subdefs,
'from_basket' => $ssttid,
'export_file' => $download->getExportName()
));
return $app->redirect('/download/' . $token .'/');
}
/**
* Prefix the method to call with the controller class name
*
* @param string $method The method to call
* @return string
*/
private function call($method)
{
return sprintf('%s::%s', __CLASS__, $method);
}
}

View File

@@ -0,0 +1,48 @@
<?php
require_once __DIR__ . '/../../../../PhraseanetWebTestCaseAuthenticatedAbstract.class.inc';
class ExportTest extends \PhraseanetWebTestCaseAuthenticatedAbstract
{
protected $client;
/**
* @covers Alchemy\Phrasea\Controller\Prod\Download::exportFtp
*/
public function testDownloadRecords()
{
$eventManagerStub = $this->getMockBuilder('\eventsmanager_broker')
->disableOriginalConstructor()
->getMock();
$eventManagerStub->expects($this->once())
->method('trigger')
->with($this->equalTo('__DOWNLOAD__'), $this->isType('array'))
->will($this->returnValue(null));
self::$DI['app']['events-manager'] = $eventManagerStub;
self::$DI['client']->request('POST', '/prod/download/', array(
'lst' => self::$DI['record_1']->get_serialize_key(),
'ssttid' => '',
'obj' => array('preview', 'document'),
'title' => 'export_title_test',
'businessfields' => '1'
));
$response = self::$DI['client']->getResponse();
$this->assertTrue($response->isRedirect());
$this->assertRegExp('#download/[a-zA-Z0-9]*/$#', $response->headers->get('location'));
}
/**
* @covers Alchemy\Phrasea\Controller\Prod\Download::connect
* @covers Alchemy\Phrasea\Controller\Prod\Download::call
*/
public function testRequireAuthentication()
{
$this->logout(self::$DI['app']);
self::$DI['client']->request('POST', '/prod/download/');
$this->assertTrue(self::$DI['client']->getResponse()->isRedirect());
}
}