fix(uploads): downscale huge-dimension files and give thumbnails a fallback

A flat 8000x8000 PNG can weigh 13 KB, so the byte-size shortcut skipped
compression and every thumbnail render had to decode 64 MP first — on a small
server that outlived nginx's timeout, leaving the card broken until the
variant finished while the original (/uploads) opened fine. Files over 2000 px
on their long side are now converted even when they are small on disk.

ant-design-vue paints the upload tile as <img src=thumbUrl alt=name> with no
error handling, so any failed resize showed the grey box with just the file
name. The tile now falls back to the stored original.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
mahmood
2026-11-26 14:16:38 +03:30
co-authored by Claude Haiku 5.5
parent 41399a33bb
commit 0b54752b6f
3 changed files with 58 additions and 5 deletions
@@ -10,7 +10,7 @@
The bound value is the stored file's URL, which is what the schema keeps.
-->
<div class="image-upload">
<div class="image-upload" @error.capture="onThumbError">
<a-upload
:file-list="fileList"
list-type="picture-card"
@@ -91,6 +91,24 @@ watch(
const placeholder = computed(() => props.placeholder);
/**
* ant-design-vue paints the tile as `<img :src="thumbUrl || url" alt=name>`
* with no error handling of its own, so a failed resize (a 404/504 from
* /api/img, or a variant still rendering) left the card grey with only the
* file name — which is what an admin saw right after uploading. Swap the
* thumbnail for the stored original, which the eye preview already loads.
* Capture phase: `error` on an <img> does not bubble.
*/
function onThumbError(e: Event) {
const img = e.target as HTMLImageElement | null;
const original = fileList.value[0]?.url;
if (!img || img.tagName !== 'IMG' || !original) return;
// Compare paths, not the whole URL: the browser resolves src absolutely.
if (new URL(img.src, window.location.origin).pathname !== original) {
img.src = original;
}
}
function beforeUpload(file: File) {
const problem = checkImageFile(file);
if (problem) {