Refactor image upload handling across items and receipts routes to include validation for uploaded images, enhancing error handling and user feedback. Consolidate parsing functions by importing from a shared module, improving code maintainability. Update server hooks to ensure consistent cookie handling and error logging.
CI / build (push) Has been skipped
CI / deploy (push) Successful in 1m33s

This commit is contained in:
eewing
2026-02-26 09:45:37 -06:00
parent 47625259ba
commit 455b762eef
11 changed files with 143 additions and 65 deletions
@@ -2,19 +2,11 @@
import { error, fail, redirect } from "@sveltejs/kit";
import type { Actions, PageServerLoad } from "./$types";
import type { StackqItem } from "$lib/types/items";
import { parseBool, parseNum } from "$lib/parse-form";
import { validateImageUpload } from "$lib/validate-image-upload";
const COLLECTION = "Stackq_Items";
function parseNum(val: unknown): number | undefined {
if (val === "" || val === null || val === undefined) return undefined;
const n = Number(val);
return Number.isFinite(n) ? n : undefined;
}
function parseBool(val: unknown): boolean {
return val === "on" || val === "true" || val === true;
}
export const load = async ({ params, locals }: Parameters<PageServerLoad>[0]) => {
const id = params.id;
if (!id) throw error(404, "Item not found");
@@ -52,6 +44,10 @@ export const actions = {
const Stock = parseBool(form.get("Stock"));
const Image = form.get("Image") as File | null;
const imageFile = Image && Image.size > 0 ? Image : undefined;
if (imageFile) {
const imgError = validateImageUpload(imageFile);
if (imgError) return fail(400, { error: imgError, form: "update" });
}
if (!Item) return fail(400, { error: "Item name is required", form: "update" });
try {
const body: Record<string, unknown> = {
@@ -2,19 +2,11 @@
import { fail } from "@sveltejs/kit";
import type { Actions, PageServerLoad } from "./$types";
import type { StackqItem } from "$lib/types/items";
import { parseBool, parseNum } from "$lib/parse-form";
import { validateImageUpload } from "$lib/validate-image-upload";
const COLLECTION = "Stackq_Items";
function parseNum(val: unknown): number | undefined {
if (val === "" || val === null || val === undefined) return undefined;
const n = Number(val);
return Number.isFinite(n) ? n : undefined;
}
function parseBool(val: unknown): boolean {
return val === "on" || val === "true" || val === true;
}
export const load = async ({ locals }: Parameters<PageServerLoad>[0]) => {
const list = await locals.pb
.collection(COLLECTION)
@@ -48,6 +40,10 @@ export const actions = {
const Stock = parseBool(form.get("Stock"));
const imageFileCreate = form.get("Image") as File | null;
const hasImage = imageFileCreate && imageFileCreate.size > 0;
if (hasImage) {
const imgError = validateImageUpload(imageFileCreate);
if (imgError) return fail(400, { error: imgError, form: "create" });
}
if (!Item) return fail(400, { error: "Item name is required", form: "create" });
@@ -92,6 +88,10 @@ export const actions = {
const Stock = parseBool(form.get("Stock"));
const Image = form.get("Image") as File | null;
const imageFile = Image && Image.size > 0 ? Image : undefined;
if (imageFile) {
const imgError = validateImageUpload(imageFile);
if (imgError) return fail(400, { error: imgError, form: "update" });
}
if (!Item) return fail(400, { error: "Item name is required", form: "update" });
@@ -2,6 +2,7 @@
import { fail } from "@sveltejs/kit";
import type { Actions, PageServerLoad } from "./$types";
import type { StackqReceipt } from "$lib/types/receipts";
import { validateImageUpload } from "$lib/validate-image-upload";
const COLLECTION = "Stackq_Receipts";
@@ -31,6 +32,10 @@ export const actions = {
const Type = (form.get("Type") as string)?.trim() || "Purchase";
const imageFile = form.get("Image") as File | null;
const hasImage = imageFile && imageFile.size > 0;
if (hasImage) {
const imgError = validateImageUpload(imageFile);
if (imgError) return fail(400, { error: imgError, Type });
}
const body: Record<string, unknown> = {
Status: "New",