Skip to content

Commit e4f1139

Browse files
authored
Add an image too large error (#1019)
1 parent 2e29709 commit e4f1139

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

src/Controller/Entry/EntryCreateController.php

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Controller\AbstractController;
88
use App\DTO\EntryDto;
99
use App\Entity\Magazine;
10+
use App\Exception\ImageDownloadTooLargeException;
1011
use App\Exception\PostingRestrictedException;
1112
use App\Exception\TagBannedException;
1213
use App\PageView\EntryPageView;
@@ -16,19 +17,23 @@
1617
use App\Service\EntryCommentManager;
1718
use App\Service\EntryManager;
1819
use App\Service\IpResolver;
20+
use App\Service\SettingsManager;
1921
use Psr\Log\LoggerInterface;
2022
use Symfony\Component\HttpFoundation\Request;
2123
use Symfony\Component\HttpFoundation\Response;
2224
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
2325
use Symfony\Component\Security\Http\Attribute\IsGranted;
2426
use Symfony\Component\Validator\Validator\ValidatorInterface;
27+
use Symfony\Contracts\Translation\TranslatorInterface;
2528

2629
class EntryCreateController extends AbstractController
2730
{
2831
use EntryTemplateTrait;
2932
use EntryFormTrait;
3033

3134
public function __construct(
35+
private readonly TranslatorInterface $translator,
36+
private readonly SettingsManager $settingsManager,
3237
private readonly LoggerInterface $logger,
3338
private readonly TagLinkRepository $tagLinkRepository,
3439
private readonly TagRepository $tagRepository,
@@ -45,6 +50,7 @@ public function __invoke(?Magazine $magazine, ?string $type, Request $request):
4550
$dto = new EntryDto();
4651
$dto->magazine = $magazine;
4752
$user = $this->getUserOrThrow();
53+
$maxBytes = $this->settingsManager->getMaxImageByteString();
4854

4955
$form = $this->createFormByType((new EntryPageView(1))->resolveType($type), $dto);
5056
try {
@@ -85,6 +91,7 @@ public function __invoke(?Magazine $magazine, ?string $type, Request $request):
8591
'magazine' => $magazine,
8692
'user' => $user,
8793
'form' => $form->createView(),
94+
'maxSize' => $maxBytes,
8895
],
8996
new Response(null, $form->isSubmitted() && !$form->isValid() ? 422 : 200)
9097
);
@@ -99,6 +106,7 @@ public function __invoke(?Magazine $magazine, ?string $type, Request $request):
99106
'magazine' => $magazine,
100107
'user' => $user,
101108
'form' => $form->createView(),
109+
'maxSize' => $maxBytes,
102110
],
103111
new Response(null, 422)
104112
);
@@ -112,6 +120,21 @@ public function __invoke(?Magazine $magazine, ?string $type, Request $request):
112120
'magazine' => $magazine,
113121
'user' => $user,
114122
'form' => $form->createView(),
123+
'maxSize' => $maxBytes,
124+
],
125+
new Response(null, 422)
126+
);
127+
} catch (ImageDownloadTooLargeException $e) {
128+
$this->addFlash('error', $this->translator->trans('flash_image_download_too_large_error', ['%bytes%' => $maxBytes]));
129+
$this->logger->error($e);
130+
131+
return $this->render(
132+
$this->getTemplateName((new EntryPageView(1))->resolveType($type)),
133+
[
134+
'magazine' => $magazine,
135+
'user' => $user,
136+
'form' => $form->createView(),
137+
'maxSize' => $maxBytes,
115138
],
116139
new Response(null, 422)
117140
);
@@ -126,6 +149,7 @@ public function __invoke(?Magazine $magazine, ?string $type, Request $request):
126149
'magazine' => $magazine,
127150
'user' => $user,
128151
'form' => $form->createView(),
152+
'maxSize' => $maxBytes,
129153
],
130154
new Response(null, 422)
131155
);

src/Controller/Entry/EntryEditController.php

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Entity\Magazine;
1111
use App\Form\EntryEditType;
1212
use App\Service\EntryManager;
13+
use App\Service\SettingsManager;
1314
use Psr\Log\LoggerInterface;
1415
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
1516
use Symfony\Component\HttpFoundation\Request;
@@ -24,6 +25,7 @@ class EntryEditController extends AbstractController
2425
public function __construct(
2526
private readonly EntryManager $manager,
2627
private readonly LoggerInterface $logger,
28+
private readonly SettingsManager $settingsManager,
2729
) {
2830
}
2931

@@ -37,6 +39,7 @@ public function __invoke(
3739
Request $request
3840
): Response {
3941
$dto = $this->manager->createDto($entry);
42+
$maxBytes = $this->settingsManager->getMaxImageByteString();
4043

4144
$form = $this->createForm(EntryEditType::class, $dto);
4245
try {
@@ -66,6 +69,7 @@ public function __invoke(
6669
'magazine' => $magazine,
6770
'entry' => $entry,
6871
'form' => $form->createView(),
72+
'maxSize' => $maxBytes,
6973
],
7074
new Response(null, $form->isSubmitted() && !$form->isValid() ? 422 : 200)
7175
);

src/Repository/ImageRepository.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public function __construct(
3737
/**
3838
* Process and store an uploaded image.
3939
*
40-
* @param upload file path of uploaded image
40+
* @param $upload UploadedFile file path of uploaded image
4141
*
42-
* @throws RuntimeException if image type can't be identified
42+
* @throws \RuntimeException if image type can't be identified
4343
*/
4444
public function findOrCreateFromUpload(UploadedFile $upload): ?Image
4545
{
@@ -49,9 +49,10 @@ public function findOrCreateFromUpload(UploadedFile $upload): ?Image
4949
/**
5050
* Process and store an image from source path.
5151
*
52-
* @param source file path of the image
52+
* @param $source string file path of the image
5353
*
54-
* @throws RuntimeException if image type can't be identified
54+
* @throws \RuntimeException if image type can't be identified
55+
* @throws ImageDownloadTooLargeException
5556
*/
5657
public function findOrCreateFromPath(string $source): ?Image
5758
{
@@ -63,6 +64,8 @@ public function findOrCreateFromPath(string $source): ?Image
6364
*
6465
* @param string $source file path of the image
6566
* @param ImageOrigin $origin where the image comes from
67+
*
68+
* @throws ImageDownloadTooLargeException
6669
*/
6770
private function findOrCreateFromSource(string $source, ImageOrigin $origin): ?Image
6871
{
@@ -108,9 +111,10 @@ private function findOrCreateFromSource(string $source, ImageOrigin $origin): ?I
108111
return $image;
109112
} else {
110113
$this->logger->error(
111-
'findOrCreateFromSource: failed to store image file, because it is too big',
112-
['origin' => $origin, 'type' => \gettype($e)],
114+
'findOrCreateFromSource: failed to store image file, because it is too big - {msg}',
115+
['origin' => $origin, 'type' => \gettype($e), 'msg' => $e->getMessage()],
113116
);
117+
throw $e;
114118
}
115119
} catch (\Exception $e) {
116120
$this->logger->error(

templates/layout/_form_media.html.twig

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
{% if form.image is defined %}
88
{{ form_row(form.image, {label: 'image', attr: {class: 'image-input'}}) }}
99
{% endif %}
10+
{% if maxSize is defined %}
11+
<div>{{ 'max_image_size'|trans }}: {{ maxSize }}</div>
12+
{% endif %}
1013
<div hidden>
1114
{{ form_row(form.imageUrl, {label: 'url'}) }}
1215
</div>

translations/messages.en.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ position_top: Top
743743
pending: Pending
744744
flash_thread_new_error: Thread could not be created. Something went wrong.
745745
flash_thread_tag_banned_error: Thread could not be created. The content is not allowed.
746+
flash_image_download_too_large_error: Image could not be created, it is too big (max size %bytes%)
746747
flash_email_was_sent: Email has been successfully sent.
747748
flash_email_failed_to_sent: Email could not be sent.
748749
flash_post_new_success: Post has been successfully created.
@@ -887,3 +888,4 @@ admin_users_inactive: Inactive
887888
admin_users_suspended: Suspended
888889
admin_users_banned: Banned
889890
user_verify: Activate account
891+
max_image_size: Maximum file size

0 commit comments

Comments
 (0)