Saving original filename to database for optional post-tagging

This commit is contained in:
2026-05-13 06:23:58 +02:00
parent 155fa58d8a
commit 0b5b68dc85
3 changed files with 18 additions and 4 deletions

View File

@@ -162,7 +162,7 @@ export default router => {
group.get(/\/meta\/extract\/item\/(?<id>\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 {

View File

@@ -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')
}
`;