From 1f4408de9ed08f3b0fda45a93f1585c80feeb21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 20 Oct 2015 13:58:13 +0200 Subject: 1st draft for Pocket import via API --- .../ImportBundle/Controller/PocketController.php | 139 +++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/Wallabag/ImportBundle/Controller/PocketController.php (limited to 'src/Wallabag/ImportBundle/Controller') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php new file mode 100644 index 00000000..ffd0c9ab --- /dev/null +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -0,0 +1,139 @@ +render('WallabagImportBundle:Pocket:index.html.twig', array()); + } + + /** + * Create a new Client. + * + * @return Client + */ + private function createClient() + { + return new Client([ + 'defaults' => [ + 'headers' => [ + 'content-type' => 'application/json', + 'X-Accept' => 'application/json', + ], + ], + ]); + } + + /** + * @Route("/auth-pocket", name="authpocket") + */ + public function authAction() + { + $client = $this->createClient(); + $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', + [ + 'body' => json_encode([ + 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), + 'redirect_uri' => $this->generateUrl('import', array(), true), + ]), + ] + ); + + $response = $client->send($request); + $values = $response->json(); + $code = $values['code']; + + // store code in session for callback method + $session = $this->get('session'); + $session->set('pocketCode', $code); + + $url = 'https://getpocket.com/auth/authorize?request_token='.$code.'&redirect_uri='.$this->generateUrl('callbackpocket', array(), true); + + return $this->redirect($url, 301); + } + + /** + * @Route("/callback-pocket", name="callbackpocket") + */ + public function callbackAction() + { + $client = $this->createClient(); + + $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', + [ + 'body' => json_encode([ + 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), + 'code' => $this->get('session')->get('pocketCode'), + ]), + ] + ); + + $response = $client->send($request); + $values = $response->json(); + $accessToken = $values['access_token']; + + $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', + [ + 'body' => json_encode([ + 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), + 'access_token' => $accessToken, + 'detailType' => 'complete', + ]), + ] + ); + + $response = $client->send($request); + $entries = $response->json(); + + $this->parsePocketEntries($entries['list']); + + $this->get('session')->getFlashBag()->add( + 'notice', + count($entries['list']).' entries imported' + ); + + return $this->redirect($this->generateUrl('homepage')); + } + + /** + * @param $entries + */ + private function parsePocketEntries($entries) + { + $em = $this->getDoctrine()->getManager(); + + foreach ($entries as $entry) { + $newEntry = new Entry($this->getUser()); + $newEntry->setUrl($entry['given_url']); + $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled')); + + if (isset($entry['excerpt'])) { + $newEntry->setContent($entry['excerpt']); + } + + if (isset($entry['has_image']) && $entry['has_image'] > 0) { + $newEntry->setPreviewPicture($entry['image']['src']); + } + + if (isset($entry['word_count'])) { + $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count'])); + } + + $em->persist($newEntry); + } + + $em->flush(); + } +} -- cgit v1.2.3 From ff7b031d5792f7b6fd43b508d89397775bd1433c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 23 Oct 2015 14:01:27 +0200 Subject: refactor pocket import --- .../ImportBundle/Controller/PocketController.php | 112 ++------------------- 1 file changed, 7 insertions(+), 105 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index ffd0c9ab..76d8417b 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -4,10 +4,7 @@ namespace Wallabag\ImportBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; -use Symfony\Component\HttpFoundation\Request; -use GuzzleHttp\Client; -use Wallabag\CoreBundle\Entity\Entry; -use Wallabag\CoreBundle\Tools\Utils; +use Wallabag\ImportBundle\Import\PocketImport; class PocketController extends Controller { @@ -19,49 +16,15 @@ class PocketController extends Controller return $this->render('WallabagImportBundle:Pocket:index.html.twig', array()); } - /** - * Create a new Client. - * - * @return Client - */ - private function createClient() - { - return new Client([ - 'defaults' => [ - 'headers' => [ - 'content-type' => 'application/json', - 'X-Accept' => 'application/json', - ], - ], - ]); - } - /** * @Route("/auth-pocket", name="authpocket") */ public function authAction() { - $client = $this->createClient(); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', - [ - 'body' => json_encode([ - 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), - 'redirect_uri' => $this->generateUrl('import', array(), true), - ]), - ] - ); - - $response = $client->send($request); - $values = $response->json(); - $code = $values['code']; - - // store code in session for callback method - $session = $this->get('session'); - $session->set('pocketCode', $code); + $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key')); + $authUrl = $pocket->oAuthRequest($this->generateUrl('import', array(), true), $this->generateUrl('callbackpocket', array(), true)); - $url = 'https://getpocket.com/auth/authorize?request_token='.$code.'&redirect_uri='.$this->generateUrl('callbackpocket', array(), true); - - return $this->redirect($url, 301); + return $this->redirect($authUrl, 301); } /** @@ -69,71 +32,10 @@ class PocketController extends Controller */ public function callbackAction() { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', - [ - 'body' => json_encode([ - 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), - 'code' => $this->get('session')->get('pocketCode'), - ]), - ] - ); - - $response = $client->send($request); - $values = $response->json(); - $accessToken = $values['access_token']; - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', - [ - 'body' => json_encode([ - 'consumer_key' => $this->container->getParameter('pocket_consumer_key'), - 'access_token' => $accessToken, - 'detailType' => 'complete', - ]), - ] - ); - - $response = $client->send($request); - $entries = $response->json(); - - $this->parsePocketEntries($entries['list']); - - $this->get('session')->getFlashBag()->add( - 'notice', - count($entries['list']).' entries imported' - ); + $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key')); + $accessToken = $pocket->oAuthAuthorize(); + $pocket->import($accessToken); return $this->redirect($this->generateUrl('homepage')); } - - /** - * @param $entries - */ - private function parsePocketEntries($entries) - { - $em = $this->getDoctrine()->getManager(); - - foreach ($entries as $entry) { - $newEntry = new Entry($this->getUser()); - $newEntry->setUrl($entry['given_url']); - $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled')); - - if (isset($entry['excerpt'])) { - $newEntry->setContent($entry['excerpt']); - } - - if (isset($entry['has_image']) && $entry['has_image'] > 0) { - $newEntry->setPreviewPicture($entry['image']['src']); - } - - if (isset($entry['word_count'])) { - $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count'])); - } - - $em->persist($newEntry); - } - - $em->flush(); - } } -- cgit v1.2.3 From 557e549db7932e9101f60bfda64238a235a0ce3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 23 Oct 2015 14:09:19 +0200 Subject: service call --- src/Wallabag/ImportBundle/Controller/PocketController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index 76d8417b..9602c282 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -21,7 +21,7 @@ class PocketController extends Controller */ public function authAction() { - $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key')); + $pocket = $this->get('wallabag_import.import.pocket_import'); $authUrl = $pocket->oAuthRequest($this->generateUrl('import', array(), true), $this->generateUrl('callbackpocket', array(), true)); return $this->redirect($authUrl, 301); @@ -32,7 +32,7 @@ class PocketController extends Controller */ public function callbackAction() { - $pocket = new PocketImport($this->get('security.token_storage'), $this->get('session'), $this->getDoctrine()->getManager(), $this->container->getParameter('pocket_consumer_key')); + $pocket = $this->get('wallabag_import.import.pocket_import'); $accessToken = $pocket->oAuthAuthorize(); $pocket->import($accessToken); -- cgit v1.2.3 From d51b38ed309c9aead938e8c8963c05c6d82b4ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 23 Oct 2015 14:45:50 +0200 Subject: create ImportController to list importers --- .../ImportBundle/Controller/ImportController.php | 17 +++++++++++++++++ .../ImportBundle/Controller/PocketController.php | 9 ++++----- 2 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 src/Wallabag/ImportBundle/Controller/ImportController.php (limited to 'src/Wallabag/ImportBundle/Controller') diff --git a/src/Wallabag/ImportBundle/Controller/ImportController.php b/src/Wallabag/ImportBundle/Controller/ImportController.php new file mode 100644 index 00000000..3569793b --- /dev/null +++ b/src/Wallabag/ImportBundle/Controller/ImportController.php @@ -0,0 +1,17 @@ +render('WallabagImportBundle:Import:index.html.twig', array()); + } +} diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index 9602c282..f851c81c 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -4,20 +4,19 @@ namespace Wallabag\ImportBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; -use Wallabag\ImportBundle\Import\PocketImport; class PocketController extends Controller { /** - * @Route("/import", name="import") + * @Route("/import/pocket", name="pocket") */ - public function importAction() + public function indexAction() { return $this->render('WallabagImportBundle:Pocket:index.html.twig', array()); } /** - * @Route("/auth-pocket", name="authpocket") + * @Route("/import/pocket/auth", name="authpocket") */ public function authAction() { @@ -28,7 +27,7 @@ class PocketController extends Controller } /** - * @Route("/callback-pocket", name="callbackpocket") + * @Route("/import/pocket/callback", name="callbackpocket") */ public function callbackAction() { -- cgit v1.2.3 From 0aa344dc247c77376fcbf2112191f9f8b3dfc846 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 24 Dec 2015 15:22:56 +0100 Subject: Update url & service name Prefix ur with service namel: [service]_[route name] Add comment in Interface --- src/Wallabag/ImportBundle/Controller/PocketController.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index f851c81c..2ab062e7 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -8,7 +8,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class PocketController extends Controller { /** - * @Route("/import/pocket", name="pocket") + * @Route("/import/pocket", name="pocket_import") */ public function indexAction() { @@ -16,22 +16,25 @@ class PocketController extends Controller } /** - * @Route("/import/pocket/auth", name="authpocket") + * @Route("/import/pocket/auth", name="pocket_auth") */ public function authAction() { - $pocket = $this->get('wallabag_import.import.pocket_import'); - $authUrl = $pocket->oAuthRequest($this->generateUrl('import', array(), true), $this->generateUrl('callbackpocket', array(), true)); + $pocket = $this->get('wallabag_import.pocket.import'); + $authUrl = $pocket->oAuthRequest( + $this->generateUrl('import', array(), true), + $this->generateUrl('pocket_callback', array(), true) + ); return $this->redirect($authUrl, 301); } /** - * @Route("/import/pocket/callback", name="callbackpocket") + * @Route("/import/pocket/callback", name="pocket_callback") */ public function callbackAction() { - $pocket = $this->get('wallabag_import.import.pocket_import'); + $pocket = $this->get('wallabag_import.pocket.import'); $accessToken = $pocket->oAuthAuthorize(); $pocket->import($accessToken); -- cgit v1.2.3 From 77a7752a592af9ac821621a34d9955533baf40a0 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 30 Dec 2015 10:06:45 +0100 Subject: Update after previous merge PR #1443 was merged into this branch to handle all import type in the same place. --- .../ImportBundle/Controller/ImportController.php | 48 +++++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller') diff --git a/src/Wallabag/ImportBundle/Controller/ImportController.php b/src/Wallabag/ImportBundle/Controller/ImportController.php index 3569793b..6ebd6a0a 100644 --- a/src/Wallabag/ImportBundle/Controller/ImportController.php +++ b/src/Wallabag/ImportBundle/Controller/ImportController.php @@ -4,14 +4,58 @@ namespace Wallabag\ImportBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\NullOutput; +use Symfony\Component\HttpFoundation\Request; +use Wallabag\ImportBundle\Command\ImportCommand; +use Wallabag\ImportBundle\Form\Type\UploadImportType; class ImportController extends Controller { /** * @Route("/import", name="import") */ - public function importAction() + public function importAction(Request $request) { - return $this->render('WallabagImportBundle:Import:index.html.twig', array()); + $importForm = $this->createForm(new UploadImportType()); + $importForm->handleRequest($request); + $user = $this->getUser(); + + if ($importForm->isValid()) { + $file = $importForm->get('file')->getData(); + $name = $user->getId().'.json'; + $dir = __DIR__.'/../../../../web/uploads/import'; + + if (in_array($file->getMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($dir, $name)) { + $command = new ImportCommand(); + $command->setContainer($this->container); + $input = new ArrayInput(array('userId' => $user->getId())); + $return = $command->run($input, new NullOutput()); + + if ($return == 0) { + $this->get('session')->getFlashBag()->add( + 'notice', + 'Import successful' + ); + } else { + $this->get('session')->getFlashBag()->add( + 'notice', + 'Import failed' + ); + } + + return $this->redirect('/'); + } else { + $this->get('session')->getFlashBag()->add( + 'notice', + 'Error while processing import. Please verify your import file.' + ); + } + } + + return $this->render('WallabagImportBundle:Import:index.html.twig', array( + 'form' => array( + 'import' => $importForm->createView(), ), + )); } } -- cgit v1.2.3 From 252ebd60719d32ec954d0519c9edf2b52b03310c Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 30 Dec 2015 12:23:51 +0100 Subject: Rewrote Pocket Import For the moment, we won't do a queue system, just a plain synchronous import. We also use ContentProxy to grab content for each article from Pocket. Error from Pocket are now logged using the logger. The ImportInterface need to be simple and not related to oAuth (not all import will use that method). --- .../ImportBundle/Controller/ImportController.php | 48 +--------------------- .../ImportBundle/Controller/PocketController.php | 45 ++++++++++++++------ 2 files changed, 35 insertions(+), 58 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller') diff --git a/src/Wallabag/ImportBundle/Controller/ImportController.php b/src/Wallabag/ImportBundle/Controller/ImportController.php index 6ebd6a0a..2a0d6ab5 100644 --- a/src/Wallabag/ImportBundle/Controller/ImportController.php +++ b/src/Wallabag/ImportBundle/Controller/ImportController.php @@ -4,58 +4,14 @@ namespace Wallabag\ImportBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Output\NullOutput; -use Symfony\Component\HttpFoundation\Request; -use Wallabag\ImportBundle\Command\ImportCommand; -use Wallabag\ImportBundle\Form\Type\UploadImportType; class ImportController extends Controller { /** * @Route("/import", name="import") */ - public function importAction(Request $request) + public function importAction() { - $importForm = $this->createForm(new UploadImportType()); - $importForm->handleRequest($request); - $user = $this->getUser(); - - if ($importForm->isValid()) { - $file = $importForm->get('file')->getData(); - $name = $user->getId().'.json'; - $dir = __DIR__.'/../../../../web/uploads/import'; - - if (in_array($file->getMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($dir, $name)) { - $command = new ImportCommand(); - $command->setContainer($this->container); - $input = new ArrayInput(array('userId' => $user->getId())); - $return = $command->run($input, new NullOutput()); - - if ($return == 0) { - $this->get('session')->getFlashBag()->add( - 'notice', - 'Import successful' - ); - } else { - $this->get('session')->getFlashBag()->add( - 'notice', - 'Import failed' - ); - } - - return $this->redirect('/'); - } else { - $this->get('session')->getFlashBag()->add( - 'notice', - 'Error while processing import. Please verify your import file.' - ); - } - } - - return $this->render('WallabagImportBundle:Import:index.html.twig', array( - 'form' => array( - 'import' => $importForm->createView(), ), - )); + return $this->render('WallabagImportBundle:Import:index.html.twig', []); } } diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index 2ab062e7..61eeba43 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -8,35 +8,56 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class PocketController extends Controller { /** - * @Route("/import/pocket", name="pocket_import") + * @Route("/import/pocket", name="import_pocket") */ public function indexAction() { - return $this->render('WallabagImportBundle:Pocket:index.html.twig', array()); + return $this->render('WallabagImportBundle:Pocket:index.html.twig', []); } /** - * @Route("/import/pocket/auth", name="pocket_auth") + * @Route("/import/pocket/auth", name="import_pocket_auth") */ public function authAction() { - $pocket = $this->get('wallabag_import.pocket.import'); - $authUrl = $pocket->oAuthRequest( - $this->generateUrl('import', array(), true), - $this->generateUrl('pocket_callback', array(), true) - ); + $requestToken = $this->get('wallabag_import.pocket.import') + ->getRequestToken($this->generateUrl('import', [], true)); + + $this->get('session')->set('import.pocket.code', $requestToken); - return $this->redirect($authUrl, 301); + return $this->redirect( + 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', [], true), + 301 + ); } /** - * @Route("/import/pocket/callback", name="pocket_callback") + * @Route("/import/pocket/callback", name="import_pocket_callback") */ public function callbackAction() { + $message = 'Import failed, please try again.'; $pocket = $this->get('wallabag_import.pocket.import'); - $accessToken = $pocket->oAuthAuthorize(); - $pocket->import($accessToken); + + // something bad happend on pocket side + if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) { + $this->get('session')->getFlashBag()->add( + 'notice', + $message + ); + + return $this->redirect($this->generateUrl('import_pocket')); + } + + if (true === $pocket->import()) { + $summary = $pocket->getSummary(); + $message = $summary['imported'].' entrie(s) imported, '.$summary['skipped'].' already saved.'; + } + + $this->get('session')->getFlashBag()->add( + 'notice', + $message + ); return $this->redirect($this->generateUrl('homepage')); } -- cgit v1.2.3 From b1d05721cf37ab94ec1a6837fe79cf19474dd0ff Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 30 Dec 2015 13:26:30 +0100 Subject: Rewrote Wallabag v1 import --- .../ImportBundle/Controller/PocketController.php | 2 +- .../Controller/WallabagV1Controller.php | 58 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php (limited to 'src/Wallabag/ImportBundle/Controller') diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index 61eeba43..ebcee099 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -51,7 +51,7 @@ class PocketController extends Controller if (true === $pocket->import()) { $summary = $pocket->getSummary(); - $message = $summary['imported'].' entrie(s) imported, '.$summary['skipped'].' already saved.'; + $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.'; } $this->get('session')->getFlashBag()->add( diff --git a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php new file mode 100644 index 00000000..de200184 --- /dev/null +++ b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php @@ -0,0 +1,58 @@ +createForm(new UploadImportType()); + $importForm->handleRequest($request); + $user = $this->getUser(); + + if ($importForm->isValid()) { + $file = $importForm->get('file')->getData(); + $name = $user->getId().'.json'; + + if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { + $wallabag = $this->get('wallabag_import.wallabag_v1.import'); + $res = $wallabag + ->setUser($this->getUser()) + ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name) + ->import(); + + $message = 'Import failed, please try again.'; + if (true === $res) { + $summary = $wallabag->getSummary(); + $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.'; + + @unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); + } + + $this->get('session')->getFlashBag()->add( + 'notice', + $message + ); + + return $this->redirect($this->generateUrl('homepage')); + } else { + $this->get('session')->getFlashBag()->add( + 'notice', + 'Error while processing import. Please verify your import file.' + ); + } + } + + return $this->render('WallabagImportBundle:WallabagV1:index.html.twig', [ + 'form' => $importForm->createView(), + ]); + } +} -- cgit v1.2.3 From 7019c7cf6c6af39c0f458769e20c3f9306477943 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 31 Dec 2015 11:24:46 +0100 Subject: Add tagged services for import - list services in /import - add url to import service - ImportBundle routing are now prefixed by /import - optimize flush in each import (flushing each 20 contents) - improve design of each import - add more tests --- .../ImportBundle/Controller/ImportController.php | 6 ++++-- .../ImportBundle/Controller/PocketController.php | 10 ++++++---- .../Controller/WallabagV1Controller.php | 21 +++++++++++---------- 3 files changed, 21 insertions(+), 16 deletions(-) (limited to 'src/Wallabag/ImportBundle/Controller') diff --git a/src/Wallabag/ImportBundle/Controller/ImportController.php b/src/Wallabag/ImportBundle/Controller/ImportController.php index 2a0d6ab5..c1486e38 100644 --- a/src/Wallabag/ImportBundle/Controller/ImportController.php +++ b/src/Wallabag/ImportBundle/Controller/ImportController.php @@ -8,10 +8,12 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class ImportController extends Controller { /** - * @Route("/import", name="import") + * @Route("/", name="import") */ public function importAction() { - return $this->render('WallabagImportBundle:Import:index.html.twig', []); + return $this->render('WallabagImportBundle:Import:index.html.twig', [ + 'imports' => $this->get('wallabag_import.chain')->getAll(), + ]); } } diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index ebcee099..a0853383 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -8,15 +8,17 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class PocketController extends Controller { /** - * @Route("/import/pocket", name="import_pocket") + * @Route("/pocket", name="import_pocket") */ public function indexAction() { - return $this->render('WallabagImportBundle:Pocket:index.html.twig', []); + return $this->render('WallabagImportBundle:Pocket:index.html.twig', [ + 'import' => $this->get('wallabag_import.pocket.import'), + ]); } /** - * @Route("/import/pocket/auth", name="import_pocket_auth") + * @Route("/pocket/auth", name="import_pocket_auth") */ public function authAction() { @@ -32,7 +34,7 @@ class PocketController extends Controller } /** - * @Route("/import/pocket/callback", name="import_pocket_callback") + * @Route("/pocket/callback", name="import_pocket_callback") */ public function callbackAction() { diff --git a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php index de200184..e50a6c35 100644 --- a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php +++ b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php @@ -10,20 +10,20 @@ use Wallabag\ImportBundle\Form\Type\UploadImportType; class WallabagV1Controller extends Controller { /** - * @Route("/import/wallabag-v1", name="import_wallabag_v1") + * @Route("/wallabag-v1", name="import_wallabag_v1") */ public function indexAction(Request $request) { - $importForm = $this->createForm(new UploadImportType()); - $importForm->handleRequest($request); - $user = $this->getUser(); + $form = $this->createForm(new UploadImportType()); + $form->handleRequest($request); - if ($importForm->isValid()) { - $file = $importForm->get('file')->getData(); - $name = $user->getId().'.json'; + $wallabag = $this->get('wallabag_import.wallabag_v1.import'); + + if ($form->isValid()) { + $file = $form->get('file')->getData(); + $name = $this->getUser()->getId().'.json'; if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { - $wallabag = $this->get('wallabag_import.wallabag_v1.import'); $res = $wallabag ->setUser($this->getUser()) ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name) @@ -34,7 +34,7 @@ class WallabagV1Controller extends Controller $summary = $wallabag->getSummary(); $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.'; - @unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); + unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); } $this->get('session')->getFlashBag()->add( @@ -52,7 +52,8 @@ class WallabagV1Controller extends Controller } return $this->render('WallabagImportBundle:WallabagV1:index.html.twig', [ - 'form' => $importForm->createView(), + 'form' => $form->createView(), + 'import' => $wallabag, ]); } } -- cgit v1.2.3