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 * @param Request $request
21 * @return Response|RedirectResponse
23 public function indexAction(Request
$request)
25 $form = $this->createForm(UploadImportType
::class);
26 $form->handleRequest($request);
28 $wallabag = $this->getImportService();
29 $wallabag->setUser($this->getUser());
31 if ($form->isSubmitted() && $form->isValid()) {
32 $file = $form->get('file')->getData();
33 $markAsRead = $form->get('mark_as_read')->getData();
34 $name = $this->getUser()->getId() . '.json';
36 if (null !== $file && in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes'), true) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
38 ->setFilepath($this->getParameter('wallabag_import.resource_dir') . '/' . $name)
39 ->setMarkAsRead($markAsRead)
42 $message = 'flashes.import.notice.failed';
45 $summary = $wallabag->getSummary();
46 $message = $this->get('translator')->trans('flashes.import.notice.summary', [
47 '%imported%' => $summary['imported'],
48 '%skipped%' => $summary['skipped'],
51 if (0 < $summary['queued']) {
52 $message = $this->get('translator')->trans('flashes.import.notice.summary_with_queue', [
53 '%queued%' => $summary['queued'],
57 unlink($this->getParameter('wallabag_import.resource_dir') . '/' . $name);
60 $this->get('session')->getFlashBag()->add(
65 return $this->redirect($this->generateUrl('homepage'));
68 $this->get('session')->getFlashBag()->add(
70 'flashes.import.notice.failed_on_file'
74 return $this->render($this->getImportTemplate(), [
75 'form' => $form->createView(),
76 'import' => $wallabag,
81 * Return the service to handle the import.
83 * @return \Wallabag\ImportBundle\Import\ImportInterface
85 abstract protected function getImportService();
88 * Return the template used for the form.
92 abstract protected function getImportTemplate();