]>
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 | /** | |
13 | * @Route("/import/wallabag-v1", name="import_wallabag_v1") | |
14 | */ | |
15 | public function indexAction(Request $request) | |
16 | { | |
17 | $importForm = $this->createForm(new UploadImportType()); | |
18 | $importForm->handleRequest($request); | |
19 | $user = $this->getUser(); | |
20 | ||
21 | if ($importForm->isValid()) { | |
22 | $file = $importForm->get('file')->getData(); | |
23 | $name = $user->getId().'.json'; | |
24 | ||
25 | if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { | |
26 | $wallabag = $this->get('wallabag_import.wallabag_v1.import'); | |
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 | ||
37 | @unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); | |
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', [ | |
55 | 'form' => $importForm->createView(), | |
56 | ]); | |
57 | } | |
58 | } |