Mirror and zoom the scan preview, strengthen barcode decoding, save failed captures
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
b5defb4e4f
commit
308c0e1ed3
10 files changed
+510
-152
No files matched your search
@@ -1,3 +1,5 @@
|
||||
import { mkdirSync, readdirSync, unlinkSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { Hono } from 'hono';
|
||||
import type { Context } from 'hono';
|
||||
import type { Book, CardData, SearchResult, Settings } from '../shared/types.ts';
|
||||
@@ -37,6 +39,8 @@ export interface AppDeps {
|
||||
renderCardsPdf: RenderCardsPdf; renderCalibrationPdf: RenderCalibrationPdf;
|
||||
classNames: ClassNames;
|
||||
events?: EventBus;
|
||||
/** Where pictures the scanner failed to read are kept for tuning (the newest 30). */
|
||||
capturesDir?: string;
|
||||
}
|
||||
|
||||
class HttpError extends Error {
|
||||
@@ -116,6 +120,25 @@ export function createApp(deps: AppDeps): Hono {
|
||||
return r;
|
||||
}
|
||||
|
||||
let captureSeq = 0;
|
||||
const MAX_CAPTURE_BYTES = 8 * 1024 * 1024, KEEP_CAPTURES = 30;
|
||||
app.post('/api/debug/capture', async (c) => {
|
||||
const dir = deps.capturesDir;
|
||||
if (!dir) throw new HttpError(404, 'Saving captures is not set up');
|
||||
const declared = Number(c.req.header('content-length') ?? 0);
|
||||
if (declared > MAX_CAPTURE_BYTES) return c.json({ error: 'Picture too large' }, 413);
|
||||
const bytes = new Uint8Array(await c.req.arrayBuffer());
|
||||
if (bytes.length > MAX_CAPTURE_BYTES) return c.json({ error: 'Picture too large' }, 413);
|
||||
const isPng = bytes.length > 8 && [0x89, 0x50, 0x4e, 0x47].every((v, i) => bytes[i] === v);
|
||||
if (!isPng) throw new HttpError(400, 'Expected a PNG picture');
|
||||
mkdirSync(dir, { recursive: true });
|
||||
const saved = `capture-${new Date().toISOString().replace(/[:.]/g, '-')}-${String(captureSeq++ % 10000).padStart(4, '0')}.png`;
|
||||
writeFileSync(join(dir, saved), bytes);
|
||||
const old = readdirSync(dir).filter((f) => /^capture-.*\.png$/.test(f)).sort().reverse().slice(KEEP_CAPTURES);
|
||||
for (const f of old) { try { unlinkSync(join(dir, f)); } catch { /* already gone */ } }
|
||||
return c.json({ saved });
|
||||
});
|
||||
|
||||
app.post('/api/scan', async (c) => {
|
||||
const b = await body(c);
|
||||
const isbn = typeof b.isbn === 'string' ? normalizeIsbn(b.isbn) : null;
|
||||
|
||||
Reference in new issue
Block a user