From 9385d28e40db84bd389406b82eec197f77bf8ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 19 Oct 2019 18:50:17 +0200 Subject: [PATCH] Uploading now works everywhere. Show an flash message if the download fails. --- .../AdminPages/BaseAdminController.php | 23 ++++++++++++++-- src/Controller/PartController.php | 27 +++++++++++++++++-- .../Attachments/AttachmentSubmitHandler.php | 2 +- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/Controller/AdminPages/BaseAdminController.php b/src/Controller/AdminPages/BaseAdminController.php index 8d247b62..8bf37f46 100644 --- a/src/Controller/AdminPages/BaseAdminController.php +++ b/src/Controller/AdminPages/BaseAdminController.php @@ -34,6 +34,7 @@ namespace App\Controller\AdminPages; use App\Entity\Base\NamedDBElement; use App\Entity\Base\StructuralDBElement; use App\Entity\UserSystem\User; +use App\Exceptions\AttachmentDownloadException; use App\Form\AdminPages\ImportType; use App\Form\AdminPages\MassCreationForm; use App\Services\AttachmentHelper; @@ -109,7 +110,14 @@ abstract class BaseAdminController extends AbstractController 'secure_attachment' => $attachment['secureFile']->getData(), 'download_url' => $attachment['downloadURL']->getData() ]; - $this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options); + try { + $this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options); + } catch (AttachmentDownloadException $ex) { + $this->addFlash( + 'error', + $this->translator->trans('attachment.download_failed') . ' ' . $ex->getMessage() + ); + } } $em->persist($entity); @@ -154,7 +162,18 @@ abstract class BaseAdminController extends AbstractController $attachments = $form['attachments']; foreach ($attachments as $attachment) { /** @var $attachment FormInterface */ - $this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData()); + $options = [ + 'secure_attachment' => $attachment['secureFile']->getData(), + 'download_url' => $attachment['downloadURL']->getData() + ]; + try { + $this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options); + } catch (AttachmentDownloadException $ex) { + $this->addFlash( + 'error', + $this->translator->trans('attachment.download_failed') . ' ' . $ex->getMessage() + ); + } } $em->persist($new_entity); diff --git a/src/Controller/PartController.php b/src/Controller/PartController.php index 817651e3..80f2b729 100644 --- a/src/Controller/PartController.php +++ b/src/Controller/PartController.php @@ -32,6 +32,7 @@ namespace App\Controller; use App\Entity\Parts\Category; use App\Entity\Parts\Part; +use App\Exceptions\AttachmentDownloadException; use App\Form\Part\PartBaseType; use App\Services\AttachmentHelper; use App\Services\Attachments\AttachmentSubmitHandler; @@ -94,7 +95,18 @@ class PartController extends AbstractController $attachments = $form['attachments']; foreach ($attachments as $attachment) { /** @var $attachment FormInterface */ - $attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData()); + $options = [ + 'secure_attachment' => $attachment['secureFile']->getData(), + 'download_url' => $attachment['downloadURL']->getData() + ]; + try { + $attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options); + } catch (AttachmentDownloadException $ex) { + $this->addFlash( + 'error', + $translator->trans('attachment.download_failed') . ' ' . $ex->getMessage() + ); + } } @@ -169,7 +181,18 @@ class PartController extends AbstractController $attachments = $form['attachments']; foreach ($attachments as $attachment) { /** @var $attachment FormInterface */ - $attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData()); + $options = [ + 'secure_attachment' => $attachment['secureFile']->getData(), + 'download_url' => $attachment['downloadURL']->getData() + ]; + try { + $attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options); + } catch (AttachmentDownloadException $ex) { + $this->addFlash( + 'error', + $translator->trans('attachment.download_failed') . ' ' . $ex->getMessage() + ); + } } $em->persist($new_part); diff --git a/src/Services/Attachments/AttachmentSubmitHandler.php b/src/Services/Attachments/AttachmentSubmitHandler.php index b9ec2d03..734818ae 100644 --- a/src/Services/Attachments/AttachmentSubmitHandler.php +++ b/src/Services/Attachments/AttachmentSubmitHandler.php @@ -217,7 +217,7 @@ class AttachmentSubmitHandler ]); if (200 !== $response->getStatusCode()) { - throw new AttachmentDownloadException('Statuscode:' . $response->getStatusCode()); + throw new AttachmentDownloadException('Status code: ' . $response->getStatusCode()); } //Open a temporary file in the attachment folder