From 0b5b68dc858b0f1adb538ffc6395df501aed4b9a Mon Sep 17 00:00:00 2001 From: Kibi Kelburton Date: Wed, 13 May 2026 06:23:58 +0200 Subject: [PATCH] Saving original filename to database for optional post-tagging --- migrations/f0ckm_schema.sql | 3 ++- src/inc/routes/apiv2/index.mjs | 10 +++++++++- src/upload_handler.mjs | 9 +++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/migrations/f0ckm_schema.sql b/migrations/f0ckm_schema.sql index 24b38cb..5881d0b 100644 --- a/migrations/f0ckm_schema.sql +++ b/migrations/f0ckm_schema.sql @@ -844,7 +844,8 @@ CREATE TABLE public.items ( has_coverart boolean DEFAULT false, is_pinned boolean DEFAULT false, is_oc boolean DEFAULT false, - xd_score integer DEFAULT 0 NOT NULL + xd_score integer DEFAULT 0 NOT NULL, + original_filename text ); diff --git a/src/inc/routes/apiv2/index.mjs b/src/inc/routes/apiv2/index.mjs index f8bd87d..c3471da 100644 --- a/src/inc/routes/apiv2/index.mjs +++ b/src/inc/routes/apiv2/index.mjs @@ -162,7 +162,7 @@ export default router => { group.get(/\/meta\/extract\/item\/(?\d+)$/, lib.loggedin, async (req, res) => { const id = req.params.id; try { - const rows = await db`SELECT dest, mime FROM items WHERE id = ${id}`; + const rows = await db`SELECT dest, mime, original_filename FROM items WHERE id = ${id}`; const item = rows[0]; if (!item) return res.json({ success: false, msg: 'Item not found' }, 404); @@ -187,6 +187,14 @@ export default router => { results.push(val.substring(0, 255)); }; + // Original filename is always surfaced first — most useful for tagging + if (item.original_filename) { + let baseName = item.original_filename; + const lastDot = baseName.lastIndexOf('.'); + if (lastDot > 0) baseName = baseName.substring(0, lastDot); + addResult(baseName.trim()); + } + if (item.mime.startsWith('image/')) { // Use exiftool for images — reads EXIF, IPTC, XMP tags properly try { diff --git a/src/upload_handler.mjs b/src/upload_handler.mjs index af3cc14..f68b2af 100644 --- a/src/upload_handler.mjs +++ b/src/upload_handler.mjs @@ -14,6 +14,9 @@ const sendJson = (res, data, code = 200) => { res.end(JSON.stringify(data)); }; +// One-time migration: add original_filename column if it doesn't exist +db`ALTER TABLE items ADD COLUMN IF NOT EXISTS original_filename text`.catch(() => {}); + export const handleUpload = async (req, res, self) => { // Manual session lookup is required here because this handler is called from a // bypass middleware that runs in parallel with the main session middleware. @@ -294,6 +297,7 @@ export const handleUpload = async (req, res, self) => { const insertChecksum = getBypassDuplicateCheck() ? `${checksum}_bypass_${Date.now()}` : checksum; // Insert + const originalFilename = file.filename || null; await db` insert into items ${db({ src: '', @@ -307,8 +311,9 @@ export const handleUpload = async (req, res, self) => { usernetwork: 'web', stamp: ~~(Date.now() / 1000), active: !manualApproval, - is_oc: is_oc - }, 'src', 'dest', 'mime', 'size', 'checksum', 'phash', 'username', 'userchannel', 'usernetwork', 'stamp', 'active', 'is_oc') + is_oc: is_oc, + original_filename: originalFilename + }, 'src', 'dest', 'mime', 'size', 'checksum', 'phash', 'username', 'userchannel', 'usernetwork', 'stamp', 'active', 'is_oc', 'original_filename') } `;