From 206f78979b3df84772d546e99a8ab7cf567c009f Mon Sep 17 00:00:00 2001 From: JasonFraser Date: Tue, 24 Nov 2026 08:15:51 -0400 Subject: [PATCH] Warn on low-resolution image upload before extraction Probes a staged bare image's own naturalWidth (fired, not awaited, so staging stays synchronous) and flags one below LOW_RESOLUTION_WIDTH_PX - deliberately the same threshold raw_ocr_extract._MIN_OCR_WIDTH_PX uses server-side to decide whether to upscale before OCR, kept in sync rather than two independently-chosen numbers. Inline warning under the staged file's name, not a blocking modal - both real choices (pick a different file, or proceed anyway) are already on screen. Co-Authored-By: Claude Sonnet 5 --- frontend/src/components/FileUploadZone.vue | 36 ++++++++++-- frontend/src/composables/useFileUpload.js | 64 +++++++++++++++++++++- 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/FileUploadZone.vue b/frontend/src/components/FileUploadZone.vue index b9dbc60..a3d9387 100644 --- a/frontend/src/components/FileUploadZone.vue +++ b/frontend/src/components/FileUploadZone.vue @@ -21,6 +21,15 @@ defineProps({ isDragOver: { type: Boolean, default: false }, isProcessing: { type: Boolean, required: true }, phaseText: { type: String, required: true }, + // Billing/usage note aside - this is about EXTRACTION QUALITY, a + // real reported case: a low-resolution photo/screenshot upload can + // read so poorly via OCR that the result needs careful manual review + // even after the backend's own upscaling correction (see CLAUDE.md's + // "Two real bugs found investigating a specific real-world statement + // report" entry). {[name:size]: {width, height}}, keyed the same way + // chosenFiles' own dedup already is - see useFileUpload.js's own + // lowResolutionWarnings docstring. + lowResolutionWarnings: { type: Object, default: () => ({}) }, }) const emit = defineEmits(['file-input-change', 'drag-over', 'drag-leave', 'drop', 'remove-file', 'extract']) @@ -53,10 +62,29 @@ const extractionStore = useExtractionStore() pulled back out before Extract is ever clicked. --> diff --git a/frontend/src/composables/useFileUpload.js b/frontend/src/composables/useFileUpload.js index a2d0dc5..3cd0406 100644 --- a/frontend/src/composables/useFileUpload.js +++ b/frontend/src/composables/useFileUpload.js @@ -11,12 +11,56 @@ import { ref } from 'vue' import { useExtractionStore } from '../stores/extractionStore' const ACCEPTED_FILE_RE = /\.(pdf|jpe?g|png)$/i +const IMAGE_FILE_RE = /\.(jpe?g|png)$/i +// Mirrors the backend's own raw_ocr_extract._MIN_OCR_WIDTH_PX exactly - +// that's the width below which the backend already upscales before OCR +// (see CLAUDE.md's "Two real bugs found investigating a specific real- +// world statement report" entry) - this is the matching FRONTEND-side +// warning threshold, surfaced at upload time rather than only +// discovered after a Preview/extraction comes back wrong. A PDF isn't +// checked at all - a scanned PDF page is rendered server-side at a +// fixed resolution regardless of whatever the original scan's own +// resolution was, so there's nothing meaningful to read client-side +// for it the way a bare image's own naturalWidth directly is one. +const LOW_RESOLUTION_WIDTH_PX = 1600 + +/** + * The real natural pixel width of an image file, or null if it + * couldn't be read (never rejects - a failed probe just means no + * warning is shown, not that the file is blocked). + */ +function probeImageWidth(file) { + return new Promise((resolve) => { + const url = URL.createObjectURL(file) + const img = new Image() + img.onload = () => { + resolve({ width: img.naturalWidth, height: img.naturalHeight }) + URL.revokeObjectURL(url) + } + img.onerror = () => { + resolve(null) + URL.revokeObjectURL(url) + } + img.src = url + }) +} export function useFileUpload() { const extractionStore = useExtractionStore() const chosenFiles = ref([]) const isDragOver = ref(false) + /** + * {["name:size"]: {width, height}} for every staged image found to be + * below LOW_RESOLUTION_WIDTH_PX - keyed the same way chosenFiles' + * own dedup already is, so FileUploadZone.vue can look a warning up + * for a given staged file with no extra bookkeeping. Populated + * asynchronously (probing an image's real dimensions takes a load), + * so a file can briefly show with no warning before one appears a + * moment later - an honest reflection of when the check actually + * resolves, not worth blocking the staged-file display on. + */ + const lowResolutionWarnings = ref({}) /** * Validates and ADDS files to whatever's already staged for the next @@ -47,18 +91,36 @@ export function useFileUpload() { if (!staged.has(key)) { chosenFiles.value.push(f) staged.add(key) + // Fire-and-forget - never awaited here, so staging itself stays + // synchronous and instant; the warning (if any) just appears a + // moment later once the probe resolves. Only a bare image is + // checked at all (see LOW_RESOLUTION_WIDTH_PX's own comment for + // why a PDF is skipped). + if (IMAGE_FILE_RE.test(f.name)) { + probeImageWidth(f).then((dims) => { + if (dims && dims.width < LOW_RESOLUTION_WIDTH_PX) { + lowResolutionWarnings.value = { ...lowResolutionWarnings.value, [key]: dims } + } + }) + } } } } /** Removes one staged file (by its position in chosenFiles) before it's ever uploaded. */ function removeChosenFile(index) { + const f = chosenFiles.value[index] chosenFiles.value.splice(index, 1) + if (f) { + const { [f.name + ':' + f.size]: _removed, ...rest } = lowResolutionWarnings.value + lowResolutionWarnings.value = rest + } } /** Empties the staging list - called once a batch has been handed off to extraction, so those files aren't left sitting there to be accidentally re-extracted (and duplicated) by a later click. */ function clearChosenFiles() { chosenFiles.value = [] + lowResolutionWarnings.value = {} } function onFileInputChange(e) { @@ -85,7 +147,7 @@ export function useFileUpload() { } return { - chosenFiles, isDragOver, + chosenFiles, isDragOver, lowResolutionWarnings, setFiles, removeChosenFile, clearChosenFiles, onFileInputChange, onDragOver, onDragLeave, onDrop, } }