2016-07-12 11:51:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\ImportBundle\Controller;
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
2016-07-12 11:51:05 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2022-08-28 00:01:46 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2018-10-04 12:07:20 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2022-08-28 00:01:46 +00:00
|
|
|
use Symfony\Component\Translation\TranslatorInterface;
|
2016-07-12 11:51:05 +00:00
|
|
|
use Wallabag\ImportBundle\Form\Type\UploadImportType;
|
|
|
|
|
2016-09-21 15:47:47 +00:00
|
|
|
abstract class BrowserController extends Controller
|
2016-07-12 11:51:05 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Route("/browser", name="import_browser")
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function indexAction(Request $request)
|
|
|
|
{
|
|
|
|
$form = $this->createForm(UploadImportType::class);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
$wallabag = $this->getImportService();
|
2016-09-21 15:47:47 +00:00
|
|
|
$wallabag->setUser($this->getUser());
|
2016-07-12 11:51:05 +00:00
|
|
|
|
2016-12-14 10:54:30 +00:00
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2016-07-12 11:51:05 +00:00
|
|
|
$file = $form->get('file')->getData();
|
|
|
|
$markAsRead = $form->get('mark_as_read')->getData();
|
2017-07-01 07:52:38 +00:00
|
|
|
$name = $this->getUser()->getId() . '.json';
|
2016-07-12 11:51:05 +00:00
|
|
|
|
2018-09-05 12:25:32 +00:00
|
|
|
if (null !== $file && \in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes'), true) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
|
2016-07-12 11:51:05 +00:00
|
|
|
$res = $wallabag
|
2017-07-01 07:52:38 +00:00
|
|
|
->setFilepath($this->getParameter('wallabag_import.resource_dir') . '/' . $name)
|
2016-07-12 11:51:05 +00:00
|
|
|
->setMarkAsRead($markAsRead)
|
|
|
|
->import();
|
|
|
|
|
|
|
|
$message = 'flashes.import.notice.failed';
|
|
|
|
|
|
|
|
if (true === $res) {
|
|
|
|
$summary = $wallabag->getSummary();
|
2022-08-28 00:01:46 +00:00
|
|
|
$message = $this->get(TranslatorInterface::class)->trans('flashes.import.notice.summary', [
|
2016-07-12 11:51:05 +00:00
|
|
|
'%imported%' => $summary['imported'],
|
|
|
|
'%skipped%' => $summary['skipped'],
|
|
|
|
]);
|
|
|
|
|
2016-09-21 15:47:47 +00:00
|
|
|
if (0 < $summary['queued']) {
|
2022-08-28 00:01:46 +00:00
|
|
|
$message = $this->get(TranslatorInterface::class)->trans('flashes.import.notice.summary_with_queue', [
|
2016-09-21 15:47:47 +00:00
|
|
|
'%queued%' => $summary['queued'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
unlink($this->getParameter('wallabag_import.resource_dir') . '/' . $name);
|
2016-07-12 11:51:05 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$this->get(SessionInterface::class)->getFlashBag()->add(
|
2016-07-12 11:51:05 +00:00
|
|
|
'notice',
|
|
|
|
$message
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->redirect($this->generateUrl('homepage'));
|
2017-07-01 07:52:38 +00:00
|
|
|
}
|
2022-08-28 00:01:46 +00:00
|
|
|
$this->get(SessionInterface::class)->getFlashBag()->add(
|
2016-07-12 11:51:05 +00:00
|
|
|
'notice',
|
|
|
|
'flashes.import.notice.failed_on_file'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render($this->getImportTemplate(), [
|
|
|
|
'form' => $form->createView(),
|
|
|
|
'import' => $wallabag,
|
|
|
|
]);
|
|
|
|
}
|
2017-07-01 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the service to handle the import.
|
|
|
|
*
|
|
|
|
* @return \Wallabag\ImportBundle\Import\ImportInterface
|
|
|
|
*/
|
|
|
|
abstract protected function getImportService();
|
|
|
|
|
2017-07-24 09:52:43 +00:00
|
|
|
/**
|
|
|
|
* Return the template used for the form.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract protected function getImportTemplate();
|
2016-07-12 11:51:05 +00:00
|
|
|
}
|