1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-06-27 16:36:00 +00:00
wallabag/src/Controller/Import/InstapaperController.php
2025-04-07 09:17:32 +02:00

81 lines
3.2 KiB
PHP

<?php
namespace Wallabag\Controller\Import;
use Craue\ConfigBundle\Util\Config;
use OldSound\RabbitMqBundle\RabbitMq\Producer as RabbitMqProducer;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Wallabag\Controller\AbstractController;
use Wallabag\Form\Type\UploadImportType;
use Wallabag\Import\InstapaperImport;
use Wallabag\Redis\Producer as RedisProducer;
class InstapaperController extends AbstractController
{
public function __construct(
private readonly RabbitMqProducer $rabbitMqProducer,
private readonly RedisProducer $redisProducer,
) {
}
#[Route(path: '/import/instapaper', name: 'import_instapaper', methods: ['GET', 'POST'])]
#[IsGranted('IMPORT_ENTRIES')]
public function indexAction(Request $request, InstapaperImport $instapaper, Config $craueConfig, TranslatorInterface $translator)
{
$form = $this->createForm(UploadImportType::class);
$form->handleRequest($request);
$instapaper->setUser($this->getUser());
if ($craueConfig->get('import_with_rabbitmq')) {
$instapaper->setProducer($this->rabbitMqProducer);
} elseif ($craueConfig->get('import_with_redis')) {
$instapaper->setProducer($this->redisProducer);
}
if ($form->isSubmitted() && $form->isValid()) {
$file = $form->get('file')->getData();
$markAsRead = $form->get('mark_as_read')->getData();
$name = 'instapaper_' . $this->getUser()->getId() . '.csv';
if (null !== $file && \in_array($file->getClientMimeType(), $this->getParameter('wallabag.allow_mimetypes'), true) && $file->move($this->getParameter('wallabag.resource_dir'), $name)) {
$res = $instapaper
->setFilepath($this->getParameter('wallabag.resource_dir') . '/' . $name)
->setMarkAsRead($markAsRead)
->import();
$message = 'flashes.import.notice.failed';
if (true === $res) {
$summary = $instapaper->getSummary();
$message = $translator->trans('flashes.import.notice.summary', [
'%imported%' => $summary['imported'],
'%skipped%' => $summary['skipped'],
]);
if (0 < $summary['queued']) {
$message = $translator->trans('flashes.import.notice.summary_with_queue', [
'%queued%' => $summary['queued'],
]);
}
unlink($this->getParameter('wallabag.resource_dir') . '/' . $name);
}
$this->addFlash('notice', $message);
return $this->redirect($this->generateUrl('homepage'));
}
$this->addFlash('notice', 'flashes.import.notice.failed_on_file');
}
return $this->render('Import/Instapaper/index.html.twig', [
'form' => $form->createView(),
'import' => $instapaper,
]);
}
}