file meta

This commit is contained in:
seb
2026-09-18 05:19:47 +02:00
parent e10a8d10e7
commit 9e9d3154ca
17 changed files with 896 additions and 623 deletions

View File

@@ -30,7 +30,8 @@ export default function add(pool) {
* @param {Buffer} [input.thumbavif] - Optional AVIF thumbnail buffer.
* @param {number} [input.thumbx] - Optional thumbnail width.
* @param {number} [input.thumby] - Optional thumbnail height.
* @returns {Promise<{id: number, manifestId: number, fileId: number, sort: number, hash: string, mimetype: string, name: string, size: number, created: Date, modified: Date, thumbavif: Buffer|null, thumbx: number|null, thumby: number|null}>} The created attachment record.
* @param {object} [input.meta] - Optional string key/value metadata stored on the file row.
* @returns {Promise<{id: number, manifestId: number, fileId: number, sort: number, hash: string, mimetype: string, name: string, size: number, created: Date, modified: Date, thumbavif: Buffer|null, thumbx: number|null, thumby: number|null, meta: object|null}>} The created attachment record.
* @throws {Error} If the manifest is not found or file input is invalid.
*
* @example
@@ -42,7 +43,7 @@ export default function add(pool) {
* // => { id, manifestId, fileId, sort, hash, mimetype, name, size, created, modified, thumbavif, thumbx, thumby }
*/
return async (googleAuthId, manifestId, input, ...rest) => {
let rawMimetype, rawName, rawContent, rawHash, rawThumbavif, rawThumbx, rawThumby, rawSort, rawSize, rawCreated, rawModified;
let rawMimetype, rawName, rawContent, rawHash, rawThumbavif, rawThumbx, rawThumby, rawSort, rawSize, rawCreated, rawModified, rawMeta;
if (typeof input === 'object' && input !== null && !Buffer.isBuffer(input)) {
rawMimetype = input.mimetype;
rawName = input.name;
@@ -55,6 +56,7 @@ export default function add(pool) {
rawSize = input.size;
rawCreated = input.created ?? input.createdAt;
rawModified = input.modified ?? input.modifiedAt ?? input.lastModified;
rawMeta = input.meta;
} else {
rawMimetype = input;
rawName = rest[0];
@@ -105,6 +107,19 @@ export default function add(pool) {
: null;
const modified = parsedModified && !isNaN(parsedModified) ? parsedModified : created;
// Simple string key/value metadata; only plain string values are kept.
let meta = null;
if (typeof rawMeta === 'object' && rawMeta !== null && !Array.isArray(rawMeta)) {
meta = {};
for (const [key, value] of Object.entries(rawMeta)) {
if (value === undefined || value === null) continue;
meta[key] = String(value);
}
if (Object.keys(meta).length === 0) {
meta = null;
}
}
const hash =
typeof rawHash === 'string' && rawHash.trim()
? rawHash.trim()
@@ -168,6 +183,7 @@ export default function add(pool) {
.input('thumbavif', thumbavif)
.input('thumbx', thumbx)
.input('thumby', thumby)
.input('meta', meta === null ? null : JSON.stringify(meta))
.query(CREATE_FILE);
const fileId = fileRes.recordset[0].id;
@@ -196,6 +212,7 @@ export default function add(pool) {
thumbavif,
thumbx,
thumby,
meta,
};
} catch (err) {
try {