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/OmnivoreController.php

84 lines
3.1 KiB
PHP
Raw Normal View History

2024-10-31 08:10:33 +01:00
<?php
namespace Wallabag\Controller\Import;
2024-10-31 08:10:33 +01:00
use Craue\ConfigBundle\Util\Config;
use OldSound\RabbitMqBundle\RabbitMq\Producer as RabbitMqProducer;
2025-03-11 00:54:02 +01:00
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
2024-10-31 08:10:33 +01:00
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\OmnivoreImport;
use Wallabag\Redis\Producer as RedisProducer;
2024-10-31 08:10:33 +01:00
class OmnivoreController extends AbstractController
{
public function __construct(
2025-04-05 13:59:36 +02:00
private readonly RabbitMqProducer $rabbitMqProducer,
private readonly RedisProducer $redisProducer,
) {
2024-10-31 08:10:33 +01:00
}
/**
2025-03-10 01:15:45 +01:00
* @Route("/import/omnivore", name="import_omnivore", methods={"GET", "POST"})
2025-03-11 00:54:02 +01:00
* @IsGranted("IMPORT_ENTRIES")
2024-10-31 08:10:33 +01:00
*/
public function indexAction(Request $request, OmnivoreImport $omnivore, Config $craueConfig, TranslatorInterface $translator)
{
$form = $this->createForm(UploadImportType::class);
$form->handleRequest($request);
$omnivore->setUser($this->getUser());
if ($craueConfig->get('import_with_rabbitmq')) {
$omnivore->setProducer($this->rabbitMqProducer);
} elseif ($craueConfig->get('import_with_redis')) {
$omnivore->setProducer($this->redisProducer);
}
if ($form->isSubmitted() && $form->isValid()) {
$file = $form->get('file')->getData();
$markAsRead = $form->get('mark_as_read')->getData();
$name = 'omnivore_' . $this->getUser()->getId() . '.json';
if (null !== $file && \in_array($file->getClientMimeType(), $this->getParameter('wallabag.allow_mimetypes'), true) && $file->move($this->getParameter('wallabag.resource_dir'), $name)) {
2024-10-31 08:10:33 +01:00
$res = $omnivore
->setFilepath($this->getParameter('wallabag.resource_dir') . '/' . $name)
2024-10-31 08:10:33 +01:00
->setMarkAsRead($markAsRead)
->import();
$message = 'flashes.import.notice.failed';
if (true === $res) {
$summary = $omnivore->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);
2024-10-31 08:10:33 +01:00
}
$this->addFlash('notice', $message);
return $this->redirect($this->generateUrl('homepage'));
}
$this->addFlash('notice', 'flashes.import.notice.failed_on_file');
}
return $this->render('Import/Omnivore/index.html.twig', [
2024-10-31 08:10:33 +01:00
'form' => $form->createView(),
'import' => $omnivore,
]);
}
}