]>
Commit | Line | Data |
---|---|---|
b1d05721 JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Controller; | |
4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
7 | use Symfony\Component\HttpFoundation\Request; | |
8 | use Wallabag\ImportBundle\Form\Type\UploadImportType; | |
9 | ||
10 | class WallabagV1Controller extends Controller | |
11 | { | |
12 | /** | |
7019c7cf | 13 | * @Route("/wallabag-v1", name="import_wallabag_v1") |
b1d05721 JB |
14 | */ |
15 | public function indexAction(Request $request) | |
16 | { | |
1d76102a | 17 | $form = $this->createForm(UploadImportType::class); |
7019c7cf | 18 | $form->handleRequest($request); |
b1d05721 | 19 | |
7019c7cf JB |
20 | $wallabag = $this->get('wallabag_import.wallabag_v1.import'); |
21 | ||
22 | if ($form->isValid()) { | |
23 | $file = $form->get('file')->getData(); | |
24 | $name = $this->getUser()->getId().'.json'; | |
b1d05721 JB |
25 | |
26 | if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { | |
b1d05721 JB |
27 | $res = $wallabag |
28 | ->setUser($this->getUser()) | |
29 | ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name) | |
30 | ->import(); | |
31 | ||
32 | $message = 'Import failed, please try again.'; | |
33 | if (true === $res) { | |
34 | $summary = $wallabag->getSummary(); | |
35 | $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.'; | |
36 | ||
7019c7cf | 37 | unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); |
b1d05721 JB |
38 | } |
39 | ||
40 | $this->get('session')->getFlashBag()->add( | |
41 | 'notice', | |
42 | $message | |
43 | ); | |
44 | ||
45 | return $this->redirect($this->generateUrl('homepage')); | |
46 | } else { | |
47 | $this->get('session')->getFlashBag()->add( | |
48 | 'notice', | |
49 | 'Error while processing import. Please verify your import file.' | |
50 | ); | |
51 | } | |
52 | } | |
53 | ||
54 | return $this->render('WallabagImportBundle:WallabagV1:index.html.twig', [ | |
7019c7cf JB |
55 | 'form' => $form->createView(), |
56 | 'import' => $wallabag, | |
b1d05721 JB |
57 | ]); |
58 | } | |
59 | } |