feat: image upload optimization with sharp, thumbnails, and structured error codes (#304)

- Add sharp for server-side image processing (WebP conversion + thumbnails)
- New shared backend utility for image upload, optimization, and cleanup
- Return structured error codes from upload endpoints (IMAGE_TOO_LARGE, INVALID_TYPE, etc.)
- Frontend error code mapping with i18n support (EN + DE)
- MedicationAvatar tries thumbnail first, falls back to full image
- Error display in MedicationsPage, MobileEditModal, and Auth avatar upload

Closes #302
This commit is contained in:
Daniel Volz
2026-02-24 23:52:59 +01:00
committed by GitHub
parent 7a32b2045e
commit 96b2a0c96f
15 changed files with 916 additions and 93 deletions
+25 -5
View File
@@ -50,13 +50,33 @@ export function useMedications(): UseMedicationsReturn {
body: formData,
credentials: "include",
});
if (res.ok) {
loadMeds();
if (!res.ok) {
let code = "UNKNOWN";
try {
const errorBody = (await res.json()) as { code?: string };
if (typeof errorBody?.code === "string" && errorBody.code.trim().length > 0) {
code = errorBody.code;
}
} catch {
// Keep fallback code when backend response has no JSON body.
}
throw new Error(code);
}
} catch {
// ignore
loadMeds();
} catch (error) {
if (error instanceof Error) {
// Network failures (fetch itself throws) produce browser-specific messages.
// Normalise to NETWORK_ERROR code so the UI can map to a translated string.
if (error.message === "Failed to fetch" || error.message.startsWith("NetworkError")) {
throw new Error("NETWORK_ERROR");
}
throw error;
}
throw new Error("UNKNOWN");
} finally {
setUploadingImage(false);
}
setUploadingImage(false);
},
[loadMeds]
);