Setup to allow creating snipped from API

This commit is contained in:
Tim
2025-09-22 14:05:45 +02:00
parent 29df284237
commit 933dc424c3
10 changed files with 168 additions and 4 deletions

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Controller\Api;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Cache\CacheInterface;
class ExtensionController extends AbstractApiController
{
#[Route('/content', methods: ['POST'])]
public function setContent(
Request $request,
CacheInterface $cache,
): Response {
// Parse JSON payload
$data = json_decode($request->getContent(), true);
if (!$data || !isset($data['html'])) {
return $this->errorResponse('Invalid JSON payload: Missing html parameter');
}
$html = $data['html'];
// Store HTML in cache with unique key
$userId = $this->getUser()?->getId();
$cacheKey = 'html_' . $userId . '_' . bin2hex(random_bytes(8));
$cache->get($cacheKey, function () use ($html) {
// value to cache
return $html;
});
// Build URL to redirect user to a form page later
$formUrl = $this->generateUrl(
'snip_new',
['cacheKey' => $cacheKey],
UrlGeneratorInterface::ABSOLUTE_URL
);
return $this->successResponse(['url' => $formUrl]);
}
}

View File

@@ -5,6 +5,8 @@ namespace App\Controller;
use App\Controller\Attribute\MapQueryCached;
use App\Dto\SnipFilterRequest;
use App\Entity\Snip;
use App\Entity\SnipContent;
use App\Entity\Tag;
use App\Form\ConfirmationType;
use App\Form\SnipType;
use App\Repository\SnipRepository;
@@ -16,6 +18,7 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Cache\CacheInterface;
#[Route('/snip', name: 'snip')]
class SnipController extends AbstractController
@@ -121,8 +124,13 @@ class SnipController extends AbstractController
]);
}
#[Route('/new', name: '_new')]
public function new(Request $request, SnipContentService $contentService): Response
#[Route('/new/{cacheKey}', name: '_new')]
public function new(
Request $request,
SnipContentService $contentService,
CacheInterface $cache,
?string $cacheKey = null,
): Response
{
$snip = new Snip();
$snip->setCreatedAtNow();
@@ -131,6 +139,12 @@ class SnipController extends AbstractController
$form = $this->createForm(SnipType::class, $snip);
$form->add('Create', SubmitType::class);
if ($cacheKey) {
$content = $cache->get($cacheKey, fn() => null);
$form->get('content')->setData($content);
$form->get('parser')->setData('unsafe');
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->repository->save($snip);

View File

@@ -25,7 +25,7 @@ class Snip
#[ORM\Column]
public bool $public = false;
#[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)]
#[ORM\OneToMany(targetEntity: SnipContent::class, mappedBy: 'snip', orphanRemoval: true)]
public Collection $snipContents;
#[ORM\OneToOne]

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Service\SnipParser\Generic;
use App\Service\SnipParser\AbstractParser;
class UnsafeParser extends AbstractParser
{
public function safeParseView(string $content): string
{
return $content;
}
}