Fix new snip error with git repo

Allow simple raw rendering (in html)
This commit is contained in:
Tim 2023-04-04 23:44:10 +02:00
parent 842c936d8c
commit 607435bff0
2 changed files with 34 additions and 7 deletions

View File

@ -39,21 +39,44 @@ class SnipController extends AbstractController
]); ]);
} }
#[Route('/raw/{snip}', name: '_raw')]
public function raw(Snip $snip, Request $request): Response
{
$response = new Response(
$this->snipServiceFactory->create($snip)->get(),
Response::HTTP_OK,
['Content-Type' => 'text/html']
);
$response
->setVary(['Accept', 'Accept-Encoding'])
->setEtag(md5($response->getContent()))
->setTtl(3600)
->setClientTtl(300);
if (!$request->isNoCache()) {
$response->isNotModified($request);
}
return $response;
// return $this->render('snip/single.html.twig', [
// 'snip' => $snip,
// 'content' => $this->snipServiceFactory->create($snip)->get(),
// ]);
}
#[Route('/edit/{snip}', name: '_edit')] #[Route('/edit/{snip}', name: '_edit')]
public function edit(Snip $snip, Request $request): Response public function edit(Snip $snip, Request $request): Response
{ {
$snipService = $this->snipServiceFactory->create($snip);
$form = $this->createForm(SnipType::class, $snip); $form = $this->createForm(SnipType::class, $snip);
$form->get('content')->setData($snipService->get());
$form->add('Save', SubmitType::class); $form->add('Save', SubmitType::class);
if ($snip->getId()) {
$form->get('content')->setData($this->snipServiceFactory->create($snip)->get());
}
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$snip->setCreatedAtTodayNoSeconds()
->setCreatedBy($this->getUser());
$this->repository->save($snip); $this->repository->save($snip);
$snipService->update($form->get('content')->getData()); $this->snipServiceFactory->create($snip)->update($form->get('content')->getData());
$this->addFlash('success', sprintf('Snip "%s" saved successfully', $snip)); $this->addFlash('success', sprintf('Snip "%s" saved successfully', $snip));
@ -72,6 +95,8 @@ class SnipController extends AbstractController
public function new(Request $request): Response public function new(Request $request): Response
{ {
$snip = new Snip(); $snip = new Snip();
$snip->setCreatedAtTodayNoSeconds()
->setCreatedBy($this->getUser());
return $this->edit($snip, $request); return $this->edit($snip, $request);
} }

View File

@ -19,6 +19,7 @@ class SnipService
{ {
$git = new Git(); $git = new Git();
$repoPath = sprintf('%s/%s', $snipBasePath, $snip->getId()); $repoPath = sprintf('%s/%s', $snipBasePath, $snip->getId());
dump($repoPath, $snip);
if (!is_dir($repoPath)) { if (!is_dir($repoPath)) {
$this->repo = $git->init($repoPath); $this->repo = $git->init($repoPath);
} else { } else {
@ -38,6 +39,7 @@ class SnipService
public function update(string $snipContents): void public function update(string $snipContents): void
{ {
dump($this->getSnipPath());
file_put_contents($this->getSnipPath(), $snipContents); file_put_contents($this->getSnipPath(), $snipContents);
$this->repo->addFile('snip.txt'); $this->repo->addFile('snip.txt');
if ($this->repo->hasChanges()) { if ($this->repo->hasChanges()) {