file meta
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { GET_BY_ID } from '../../sql/manifestattachment.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveManifestAccess from './resolveManifestAccess.js';
|
||||
import { parseMeta } from './listByManifestId.js';
|
||||
import request from '../request.js';
|
||||
|
||||
export default function getById(pool) {
|
||||
@@ -13,7 +14,7 @@ export default function getById(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} manifestId - The manifest ID.
|
||||
* @param {number} id - The attachment ID or file ID.
|
||||
* @returns {Promise<{id: number, manifestId: number, fileId: number, sort: number, hash: string, mimetype: string, name: string, size: number, created: Date, modified: Date, content: Buffer, thumbavif: Buffer|null, thumbx: number|null, thumby: number|null}>} The attachment record with binary content.
|
||||
* @returns {Promise<{id: number, manifestId: number, fileId: number, sort: number, hash: string, mimetype: string, name: string, size: number, created: Date, modified: Date, content: Buffer, thumbavif: Buffer|null, thumbx: number|null, thumby: number|null, meta: object|null}>} The attachment record with binary content.
|
||||
* @throws {Error} If the manifest or attachment is not found, or user is not authorized.
|
||||
*
|
||||
* @example
|
||||
@@ -53,6 +54,7 @@ export default function getById(pool) {
|
||||
thumbavif: row.thumbavif ?? null,
|
||||
thumbx: row.thumbx ?? null,
|
||||
thumby: row.thumby ?? null,
|
||||
meta: parseMeta(row.meta),
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,6 +3,18 @@ import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyA
|
||||
import resolveManifestAccess from './resolveManifestAccess.js';
|
||||
import request from '../request.js';
|
||||
|
||||
/** Parse the file.meta JSON text column into an object (string key/value). */
|
||||
export function parseMeta(raw) {
|
||||
if (raw == null) return null;
|
||||
if (typeof raw === 'object') return raw;
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
return typeof parsed === 'object' && parsed !== null ? parsed : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
@@ -18,6 +30,7 @@ function shape(row) {
|
||||
thumbavif: row.thumbavif ?? null,
|
||||
thumbx: row.thumbx ?? null,
|
||||
thumby: row.thumby ?? null,
|
||||
meta: parseMeta(row.meta),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -30,7 +43,7 @@ export default function listByManifestId(pool) {
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} manifestId - The manifest ID.
|
||||
* @returns {Promise<Array<{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}>>} A list of attachment metadata objects.
|
||||
* @returns {Promise<Array<{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}>>} A list of attachment metadata objects.
|
||||
* @throws {Error} If the manifest is not found or the user is not authorized.
|
||||
*
|
||||
* @example
|
||||
|
||||
Reference in New Issue
Block a user