wallabag/src/Wallabag/ImportBundle/Controller/BrowserController.php

84 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace Wallabag\ImportBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Wallabag\ImportBundle\Form\Type\UploadImportType;
2022-08-28 14:59:43 +00:00
use Wallabag\ImportBundle\Import\ImportInterface;
abstract class BrowserController extends AbstractController
{
/**
* @Route("/browser", name="import_browser")
*
* @return Response
*/
public function indexAction(Request $request, TranslatorInterface $translator)
{
$form = $this->createForm(UploadImportType::class);
$form->handleRequest($request);
$wallabag = $this->getImportService();
$wallabag->setUser($this->getUser());
2016-12-14 10:54:30 +00:00
if ($form->isSubmitted() && $form->isValid()) {
$file = $form->get('file')->getData();
$markAsRead = $form->get('mark_as_read')->getData();
2017-07-01 07:52:38 +00:00
$name = $this->getUser()->getId() . '.json';
if (null !== $file && \in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes'), true) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
$res = $wallabag
2017-07-01 07:52:38 +00:00
->setFilepath($this->getParameter('wallabag_import.resource_dir') . '/' . $name)
->setMarkAsRead($markAsRead)
->import();
$message = 'flashes.import.notice.failed';
if (true === $res) {
$summary = $wallabag->getSummary();
$message = $translator->trans('flashes.import.notice.summary', [
'%imported%' => $summary['imported'],
'%skipped%' => $summary['skipped'],
]);
if (0 < $summary['queued']) {
$message = $translator->trans('flashes.import.notice.summary_with_queue', [
'%queued%' => $summary['queued'],
]);
}
2017-07-01 07:52:38 +00:00
unlink($this->getParameter('wallabag_import.resource_dir') . '/' . $name);
}
$this->addFlash('notice', $message);
return $this->redirect($this->generateUrl('homepage'));
2017-07-01 07:52:38 +00:00
}
$this->addFlash('notice', 'flashes.import.notice.failed_on_file');
}
return $this->render($this->getImportTemplate(), [
'form' => $form->createView(),
'import' => $wallabag,
]);
}
2017-07-01 07:52:38 +00:00
/**
* Return the service to handle the import.
*
2022-08-28 14:59:43 +00:00
* @return ImportInterface
2017-07-01 07:52:38 +00:00
*/
abstract protected function getImportService();
2017-07-24 09:52:43 +00:00
/**
* Return the template used for the form.
*
* @return string
*/
abstract protected function getImportTemplate();
}