3 namespace Wallabag\ImportBundle\Controller
;
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
6 use Symfony\Component\HttpFoundation\RedirectResponse
;
7 use Symfony\Component\HttpFoundation\Request
;
8 use Symfony\Component\HttpFoundation\Response
;
9 use Wallabag\ImportBundle\Form\Type\UploadImportType
;
12 * Define Wallabag import for v1 and v2, since there are very similar.
14 abstract class WallabagController
extends Controller
17 * Handle import request.
19 * @return Response|RedirectResponse
21 public function indexAction(Request
$request)
23 $form = $this->createForm(UploadImportType
::class);
24 $form->handleRequest($request);
26 $wallabag = $this->getImportService();
27 $wallabag->setUser($this->getUser());
29 if ($form->isSubmitted() && $form->isValid()) {
30 $file = $form->get('file')->getData();
31 $markAsRead = $form->get('mark_as_read')->getData();
32 $name = $this->getUser()->getId() . '.json';
34 if (null !== $file && \
in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes'), true) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
36 ->setFilepath($this->getParameter('wallabag_import.resource_dir') . '/' . $name)
37 ->setMarkAsRead($markAsRead)
40 $message = 'flashes.import.notice.failed';
43 $summary = $wallabag->getSummary();
44 $message = $this->get('translator')->trans('flashes.import.notice.summary', [
45 '%imported%' => $summary['imported'],
46 '%skipped%' => $summary['skipped'],
49 if (0 < $summary['queued']) {
50 $message = $this->get('translator')->trans('flashes.import.notice.summary_with_queue', [
51 '%queued%' => $summary['queued'],
55 unlink($this->getParameter('wallabag_import.resource_dir') . '/' . $name);
58 $this->get('session')->getFlashBag()->add(
63 return $this->redirect($this->generateUrl('homepage'));
66 $this->get('session')->getFlashBag()->add(
68 'flashes.import.notice.failed_on_file'
72 return $this->render($this->getImportTemplate(), [
73 'form' => $form->createView(),
74 'import' => $wallabag,
79 * Return the service to handle the import.
81 * @return \Wallabag\ImportBundle\Import\ImportInterface
83 abstract protected function getImportService();
86 * Return the template used for the form.
90 abstract protected function getImportTemplate();