stash light
This commit is contained in:
217
src/Controller/Admin/DocumentController.php
Normal file
217
src/Controller/Admin/DocumentController.php
Normal file
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Entity\Document;
|
||||
use App\Entity\DocumentVersion;
|
||||
use App\Service\DocumentService;
|
||||
use App\Service\IngestOrchestrator;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
|
||||
|
||||
#[Route('/admin/documents')]
|
||||
class DocumentController extends AbstractController
|
||||
{
|
||||
#[Route('', name: 'admin_documents')]
|
||||
public function index(EntityManagerInterface $em): Response
|
||||
{
|
||||
$documents = $em->getRepository(Document::class)
|
||||
->findBy([], ['createdAt' => 'DESC']);
|
||||
|
||||
return $this->render('admin/document/index.html.twig', [
|
||||
'documents' => $documents
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(
|
||||
'/{id}',
|
||||
name: 'admin_document_show',
|
||||
requirements: ['id' => '[0-9a-fA-F\-]{36}']
|
||||
)]
|
||||
public function show(string $id, EntityManagerInterface $em): Response
|
||||
{
|
||||
try {
|
||||
$uuid = Uuid::fromString($id);
|
||||
} catch (\Exception $e) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
$document = $em->getRepository(Document::class)->find($uuid);
|
||||
|
||||
if (!$document) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
return $this->render('admin/document/show.html.twig', [
|
||||
'document' => $document
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/new', name: 'admin_document_new')]
|
||||
public function new(Request $request, DocumentService $documentService): Response
|
||||
{
|
||||
if ($request->isMethod('POST')) {
|
||||
|
||||
$title = $request->request->get('title');
|
||||
$file = $request->files->get('file');
|
||||
|
||||
if (!$file || !$title) {
|
||||
$this->addFlash('error', 'Titel und Datei sind erforderlich.');
|
||||
return $this->redirectToRoute('admin_document_new');
|
||||
}
|
||||
|
||||
$uploadDir = $this->getParameter('kernel.project_dir') . '/var/knowledge/uploads';
|
||||
|
||||
if (!is_dir($uploadDir)) {
|
||||
mkdir($uploadDir, 0777, true);
|
||||
}
|
||||
|
||||
$newFilename = uniqid() . '_' . $file->getClientOriginalName();
|
||||
|
||||
try {
|
||||
$file->move($uploadDir, $newFilename);
|
||||
} catch (FileException $e) {
|
||||
throw new \RuntimeException('File upload failed.');
|
||||
}
|
||||
|
||||
$filePath = $uploadDir . '/' . $newFilename;
|
||||
|
||||
$documentService->createDocument(
|
||||
$title,
|
||||
$filePath,
|
||||
$this->getUser()
|
||||
);
|
||||
|
||||
return $this->redirectToRoute('admin_documents');
|
||||
}
|
||||
|
||||
return $this->render('admin/document/new.html.twig');
|
||||
}
|
||||
|
||||
#[Route('/{id}/version/new', name: 'admin_document_version_new', requirements: ['id' => '[0-9a-fA-F\-]{36}'])]
|
||||
public function newVersion(
|
||||
string $id,
|
||||
Request $request,
|
||||
EntityManagerInterface $em,
|
||||
DocumentService $documentService
|
||||
): Response {
|
||||
|
||||
$document = $em->getRepository(Document::class)->find($id);
|
||||
|
||||
if (!$document) {
|
||||
throw $this->createNotFoundException();
|
||||
}
|
||||
|
||||
if ($request->isMethod('POST')) {
|
||||
|
||||
$file = $request->files->get('file');
|
||||
|
||||
if (!$file) {
|
||||
$this->addFlash('error', 'Datei ist erforderlich.');
|
||||
return $this->redirectToRoute('admin_document_version_new', ['id' => $id]);
|
||||
}
|
||||
|
||||
$uploadDir = $this->getParameter('kernel.project_dir') . '/var/knowledge/uploads';
|
||||
|
||||
if (!is_dir($uploadDir)) {
|
||||
mkdir($uploadDir, 0777, true);
|
||||
}
|
||||
|
||||
$newFilename = uniqid() . '_' . $file->getClientOriginalName();
|
||||
|
||||
try {
|
||||
$file->move($uploadDir, $newFilename);
|
||||
} catch (FileException $e) {
|
||||
throw new \RuntimeException('File upload failed.');
|
||||
}
|
||||
|
||||
$filePath = $uploadDir . '/' . $newFilename;
|
||||
|
||||
$documentService->addVersion(
|
||||
$document,
|
||||
$filePath,
|
||||
$this->getUser()
|
||||
);
|
||||
|
||||
return $this->redirectToRoute('admin_document_show', ['id' => $id]);
|
||||
}
|
||||
|
||||
return $this->render('admin/document/new_version.html.twig', [
|
||||
'document' => $document
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(
|
||||
'/version/{versionId}/activate',
|
||||
name: 'admin_document_version_activate',
|
||||
requirements: ['versionId' => '[0-9a-fA-F\-]{36}'],
|
||||
methods: ['POST']
|
||||
)]
|
||||
public function activateVersion(
|
||||
string $versionId,
|
||||
Request $request,
|
||||
EntityManagerInterface $em,
|
||||
DocumentService $documentService
|
||||
): RedirectResponse {
|
||||
|
||||
if (!$this->isCsrfTokenValid('activate_version', $request->request->get('_token'))) {
|
||||
throw $this->createAccessDeniedException();
|
||||
}
|
||||
|
||||
$version = $em->getRepository(DocumentVersion::class)->find($versionId);
|
||||
|
||||
if (!$version) {
|
||||
throw $this->createNotFoundException();
|
||||
}
|
||||
|
||||
$documentService->activateVersion($version);
|
||||
|
||||
return $this->redirectToRoute('admin_document_show', [
|
||||
'id' => $version->getDocument()->getId()
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(
|
||||
'/version/{versionId}/ingest',
|
||||
name: 'admin_document_version_ingest',
|
||||
methods: ['POST'],
|
||||
requirements: ['versionId' => '[0-9a-fA-F\-]{36}']
|
||||
)]
|
||||
public function ingestVersion(
|
||||
string $versionId,
|
||||
Request $request,
|
||||
EntityManagerInterface $em,
|
||||
IngestOrchestrator $orchestrator
|
||||
): RedirectResponse {
|
||||
|
||||
if (!$this->isCsrfTokenValid('ingest_version', $request->request->get('_token'))) {
|
||||
throw $this->createAccessDeniedException();
|
||||
}
|
||||
|
||||
$version = $em->getRepository(DocumentVersion::class)->find($versionId);
|
||||
|
||||
if (!$version) {
|
||||
throw $this->createNotFoundException();
|
||||
}
|
||||
|
||||
$orchestrator->runForVersion(
|
||||
$version,
|
||||
$this->getUser(),
|
||||
true // erstmal DryRun
|
||||
);
|
||||
|
||||
return $this->redirectToRoute('admin_document_show', [
|
||||
'id' => $version->getDocument()->getId()
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user