mirror of
https://github.com/wallabag/wallabag.git
synced 2025-08-06 17:41:01 +00:00
Rework on export
- all export now return a `HttpFoundation\Response` - return a 404 on unsupported format - add tests
This commit is contained in:
parent
03690d1387
commit
add597bad9
10 changed files with 351 additions and 145 deletions
|
@ -4,62 +4,55 @@ namespace Wallabag\CoreBundle\Controller;
|
|||
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
use Wallabag\CoreBundle\Helper\EntriesExport;
|
||||
|
||||
class ExportController extends Controller
|
||||
{
|
||||
/**
|
||||
* Gets all entries for current user.
|
||||
*
|
||||
* @Route("/export/{category}.{format}", name="ebook", requirements={
|
||||
* "_format": "epub|mobi|pdf|json|xml|txt|csv"
|
||||
* })
|
||||
*/
|
||||
public function getEntriesAction($format, $category)
|
||||
{
|
||||
$repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
|
||||
switch ($category) {
|
||||
case 'all':
|
||||
$method = 'All';
|
||||
break;
|
||||
|
||||
case 'unread':
|
||||
$method = 'Unread';
|
||||
break;
|
||||
|
||||
case 'starred':
|
||||
$method = 'Starred';
|
||||
break;
|
||||
|
||||
case 'archive':
|
||||
$method = 'Archive';
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
$methodBuilder = 'getBuilderFor'.$method.'ByUser';
|
||||
$qb = $repository->$methodBuilder($this->getUser()->getId());
|
||||
$entries = $qb->getQuery()->getResult();
|
||||
|
||||
$export = new EntriesExport($entries);
|
||||
$export->setMethod($method);
|
||||
$export->exportAs($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets one entry content.
|
||||
*
|
||||
* @param Entry $entry
|
||||
*
|
||||
* @Route("/export/id/{id}.{format}", requirements={"id" = "\d+"}, name="ebook_entry")
|
||||
* @Route("/export/{id}.{format}", requirements={"id" = "\d+"}, name="export_entry")
|
||||
*/
|
||||
public function getEntryAction(Entry $entry, $format)
|
||||
public function downloadEntryAction(Entry $entry, $format)
|
||||
{
|
||||
$export = new EntriesExport(array($entry));
|
||||
$export->setMethod('entry');
|
||||
$export->exportAs($format);
|
||||
try {
|
||||
return $this->get('wallabag_core.helper.entries_export')
|
||||
->setEntries($entry)
|
||||
->updateTitle('entry')
|
||||
->exportAs($format);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
throw new NotFoundHttpException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all entries for current user.
|
||||
*
|
||||
* @Route("/export/{category}.{format}", name="export_entries", requirements={
|
||||
* "_format": "epub|mobi|pdf|json|xml|txt|csv",
|
||||
* "category": "all|unread|starred|archive"
|
||||
* })
|
||||
*/
|
||||
public function downloadEntriesAction($format, $category)
|
||||
{
|
||||
$method = ucfirst($category);
|
||||
$methodBuilder = 'getBuilderFor'.$method.'ByUser';
|
||||
$entries = $this->getDoctrine()
|
||||
->getRepository('WallabagCoreBundle:Entry')
|
||||
->$methodBuilder($this->getUser()->getId())
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
try {
|
||||
return $this->get('wallabag_core.helper.entries_export')
|
||||
->setEntries($entries)
|
||||
->updateTitle($method)
|
||||
->exportAs($format);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
throw new NotFoundHttpException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue