From e0dfcd0f63e79c8c88954456f781b83d5620927d Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Mon, 3 Feb 2025 21:47:41 +0100 Subject: [PATCH] Updated repo webhook --- .../OgRepository/Image/ExportAction.php | 2 - .../OgRepository/Image/ImportAction.php | 2 - .../Webhook/ResponseController.php | 108 ++++++------------ 3 files changed, 32 insertions(+), 80 deletions(-) diff --git a/src/Controller/OgRepository/Image/ExportAction.php b/src/Controller/OgRepository/Image/ExportAction.php index 73e4eaf..ef449e3 100644 --- a/src/Controller/OgRepository/Image/ExportAction.php +++ b/src/Controller/OgRepository/Image/ExportAction.php @@ -59,8 +59,6 @@ class ExportAction extends AbstractOgRepositoryController $this->createService->__invoke($image->getClient(), CommandTypes::EXPORT_IMAGE, TraceStatus::IN_PROGRESS, $content['job_id'], $inputData); - $this->logger->info('Image exported successfully', ['image' => $image->getName()]); - $image->setStatus(ImageStatus::TRANSFERING); $this->entityManager->persist($image); $this->entityManager->flush(); diff --git a/src/Controller/OgRepository/Image/ImportAction.php b/src/Controller/OgRepository/Image/ImportAction.php index c41df80..075ff5d 100644 --- a/src/Controller/OgRepository/Image/ImportAction.php +++ b/src/Controller/OgRepository/Image/ImportAction.php @@ -59,8 +59,6 @@ class ImportAction extends AbstractOgRepositoryController $this->createService->__invoke($image->getClient(), CommandTypes::IMPORT_IMAGE, TraceStatus::IN_PROGRESS, $content['job_id'], $inputData); - $this->logger->info('Image imported successfully', ['image' => $image->getName()]); - $image->setStatus(ImageStatus::TRANSFERING); $this->entityManager->persist($image); $this->entityManager->flush(); diff --git a/src/Controller/OgRepository/Webhook/ResponseController.php b/src/Controller/OgRepository/Webhook/ResponseController.php index a6a59dd..57b76dc 100644 --- a/src/Controller/OgRepository/Webhook/ResponseController.php +++ b/src/Controller/OgRepository/Webhook/ResponseController.php @@ -6,6 +6,7 @@ use App\Controller\OgRepository\AbstractOgRepositoryController; use App\Entity\Image; use App\Entity\ImageRepository; use App\Entity\Trace; +use App\Model\CommandTypes; use App\Model\ImageStatus; use App\Model\TraceStatus; use Doctrine\ORM\EntityManagerInterface; @@ -42,11 +43,11 @@ class ResponseController extends AbstractOgRepositoryController $action = $data['job_id']; if (str_starts_with($action, "CreateAuxiliarFiles_")) { - $this->handleCreateAuxFiles($action, $data); + $this->handleCreateAuxFiles($data); } elseif (str_starts_with($action, "ExportImage_")) { - $this->handleExportImage($action, $data); + $this->processImageAction($data, 'export'); } elseif (str_starts_with($action, "ImportImage_")) { - $this->handleImportImage($action, $data); + $this->processImageAction($data, 'import'); } else { return new JsonResponse(['message' => 'Invalid action'], Response::HTTP_BAD_REQUEST); } @@ -54,7 +55,7 @@ class ResponseController extends AbstractOgRepositoryController return new JsonResponse($data, Response::HTTP_OK); } - private function handleCreateAuxFiles(string $action, array $data): void + private function handleCreateAuxFiles(array $data): void { $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); $imageUuid = $trace->getInput()['imageUuid']; @@ -62,13 +63,7 @@ class ResponseController extends AbstractOgRepositoryController $image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $imageUuid]); if ($image === null) { - $trace->setStatus(TraceStatus::FAILED); - $trace->setFinishedAt(new \DateTime()); - $trace->setOutput('Image not found'); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - - new JsonResponse(['message' => 'Image not found'], Response::HTTP_NOT_FOUND); + $this->updateTraceStatus($trace, TraceStatus::FAILED, 'Image not found'); return; } @@ -76,41 +71,31 @@ class ResponseController extends AbstractOgRepositoryController $image->setStatus(ImageStatus::SUCCESS); $this->entityManager->persist($image); - $trace->setStatus(TraceStatus::SUCCESS); - $trace->setFinishedAt(new \DateTime()); - - $this->entityManager->persist($trace); - $this->entityManager->flush(); + $this->updateTraceStatus($trace, TraceStatus::SUCCESS); } + /** * @throws TransportExceptionInterface * @throws ServerExceptionInterface * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - private function handleExportImage(string $action, array $data): void + private function processImageAction(array $data, string $actionType): void { $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); - $imageUuid = $trace->getInput()['imageUuid']; - $image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $imageUuid]); - $repositoryUuid = $trace->getInput()['repositoryUuid']; + + $image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $imageUuid]); $repository = $this->entityManager->getRepository(ImageRepository::class)->findOneBy(['uuid' => $repositoryUuid]); if ($image === null) { - $trace->setStatus(TraceStatus::FAILED); - $trace->setFinishedAt(new \DateTime()); - $trace->setOutput('Image not found'); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - - new JsonResponse(['message' => 'Image not found'], Response::HTTP_NOT_FOUND); + $this->updateTraceStatus($trace, TraceStatus::FAILED, 'Image not found'); return; } - $this->logger->info('Image exported', ['image' => $image->getName()]); + $this->logger->info("Image $actionType", ['image' => $image->getName()]); $params = [ 'json' => [ @@ -121,63 +106,34 @@ class ResponseController extends AbstractOgRepositoryController $this->logger->info('Creating aux files', ['image' => $image->getName()]); $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/images/torrentsum', $params); - $image->setRepository($repository); - $image->setStatus(ImageStatus::SUCCESS); - $this->entityManager->persist($image); - - $trace->setStatus(TraceStatus::SUCCESS); - $trace->setFinishedAt(new \DateTime()); - - $this->entityManager->persist($trace); - $this->entityManager->flush(); - } - - /** - * @throws TransportExceptionInterface - * @throws ServerExceptionInterface - * @throws RedirectionExceptionInterface - * @throws ClientExceptionInterface - */ - private function handleImportImage(string $action, array $data): void - { - $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); - - $imageUuid = $trace->getInput()['imageUuid']; - $image = $this->entityManager->getRepository(Image::class)->findOneBy(['uuid' => $imageUuid]); - - $repositoryUuid = $trace->getInput()['repositoryUuid']; - $repository = $this->entityManager->getRepository(ImageRepository::class)->findOneBy(['uuid' => $repositoryUuid]); - - if ($image === null) { - $trace->setStatus(TraceStatus::FAILED); - $trace->setFinishedAt(new \DateTime()); - $trace->setOutput('Image not found'); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - - new JsonResponse(['message' => 'Image not found'], Response::HTTP_NOT_FOUND); - return; - } - - $this->logger->info('Image imported', ['image' => $image->getName()]); - - $params = [ - 'json' => [ - 'image' => $image->getName().'.img' - ] + $inputData = [ + 'imageName' => $image->getName(), + 'imageUuid' => $image->getUuid(), ]; - $this->logger->info('Creating aux files', ['image' => $image->getName()]); - $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/images/torrentsum', $params); + $this->createService->__invoke($image->getClient(), CommandTypes::CREATE_IMAGE_AUX_FILE, TraceStatus::IN_PROGRESS, $content['job_id'], $inputData); $image->setRepository($repository); $image->setStatus(ImageStatus::SUCCESS); $this->entityManager->persist($image); - $trace->setStatus(TraceStatus::SUCCESS); + $this->updateTraceStatus($trace, TraceStatus::SUCCESS); + } + + private function updateTraceStatus(Trace $trace, string $status, string $output = null): void + { + $trace->setStatus($status); $trace->setFinishedAt(new \DateTime()); + if ($output !== null) { + $trace->setOutput($output); + } + $this->entityManager->persist($trace); $this->entityManager->flush(); + + if ($status === TraceStatus::FAILED) { + new JsonResponse(['message' => $output], Response::HTTP_NOT_FOUND); + } } -} \ No newline at end of file +}