feat: add hash and thumbnail columns to file, sort column and order method to manifestattachment
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
import { CREATE_FILE, CREATE_ATTACHMENT } from '../../sql/manifestattachment.js';
|
||||
import { createHash } from 'node:crypto';
|
||||
import sharp from 'sharp';
|
||||
import {
|
||||
CREATE_FILE,
|
||||
CREATE_ATTACHMENT,
|
||||
GET_NEXT_SORT,
|
||||
} from '../../sql/manifestattachment.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveManifestAccess from './resolveManifestAccess.js';
|
||||
import request from '../request.js';
|
||||
@@ -16,7 +22,12 @@ export default function add(pool) {
|
||||
* @param {string} input.mimetype - The MIME type of the file (e.g. `'application/pdf'`).
|
||||
* @param {string} input.name - The file name.
|
||||
* @param {Buffer} input.content - The file content.
|
||||
* @returns {Promise<{id: number, manifestId: number, fileId: number, mimetype: string, name: string}>} The created attachment record.
|
||||
* @param {number} [input.sort] - Optional display sort order.
|
||||
* @param {string} [input.hash] - The file hash (auto-computed SHA-256 if omitted).
|
||||
* @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, thumbavif: Buffer|null, thumbx: number|null, thumby: number|null}>} The created attachment record.
|
||||
* @throws {Error} If the manifest is not found or file input is invalid.
|
||||
*
|
||||
* @example
|
||||
@@ -25,14 +36,19 @@ export default function add(pool) {
|
||||
* name: 'invoice.pdf',
|
||||
* content: fileBuffer,
|
||||
* });
|
||||
* // => { id, manifestId, fileId, mimetype, name }
|
||||
* // => { id, manifestId, fileId, sort, hash, mimetype, name, thumbavif, thumbx, thumby }
|
||||
*/
|
||||
return async (googleAuthId, manifestId, input, ...rest) => {
|
||||
let rawMimetype, rawName, rawContent;
|
||||
let rawMimetype, rawName, rawContent, rawHash, rawThumbavif, rawThumbx, rawThumby, rawSort;
|
||||
if (typeof input === 'object' && input !== null && !Buffer.isBuffer(input)) {
|
||||
rawMimetype = input.mimetype;
|
||||
rawName = input.name;
|
||||
rawContent = input.content;
|
||||
rawHash = input.hash;
|
||||
rawThumbavif = input.thumbavif;
|
||||
rawThumbx = input.thumbx ?? input.thumbw;
|
||||
rawThumby = input.thumby ?? input.thumbh;
|
||||
rawSort = input.sort;
|
||||
} else {
|
||||
rawMimetype = input;
|
||||
rawName = rest[0];
|
||||
@@ -56,6 +72,49 @@ export default function add(pool) {
|
||||
? rawContent
|
||||
: Buffer.from(rawContent);
|
||||
|
||||
if (rawSort !== undefined && rawSort !== null) {
|
||||
if (!Number.isFinite(rawSort)) {
|
||||
throw new Error('Invalid sort');
|
||||
}
|
||||
}
|
||||
|
||||
const hash =
|
||||
typeof rawHash === 'string' && rawHash.trim()
|
||||
? rawHash.trim()
|
||||
: createHash('sha256').update(content).digest('hex');
|
||||
|
||||
let thumbavif = rawThumbavif
|
||||
? (Buffer.isBuffer(rawThumbavif) ? rawThumbavif : Buffer.from(rawThumbavif))
|
||||
: null;
|
||||
let thumbx = Number.isFinite(rawThumbx) ? Number(rawThumbx) : null;
|
||||
let thumby = Number.isFinite(rawThumby) ? Number(rawThumby) : null;
|
||||
|
||||
if (thumbavif && (!thumbx || !thumby)) {
|
||||
try {
|
||||
const meta = await sharp(thumbavif).metadata();
|
||||
thumbx = thumbx ?? meta.width ?? null;
|
||||
thumby = thumby ?? meta.height ?? null;
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
if (!thumbavif) {
|
||||
try {
|
||||
const thumbResult = await sharp(content)
|
||||
.resize(200, 200, { fit: 'inside', withoutEnlargement: true })
|
||||
.avif({ effort: 4 })
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
thumbavif = thumbResult.data;
|
||||
thumbx = thumbResult.info.width;
|
||||
thumby = thumbResult.info.height;
|
||||
} catch {
|
||||
thumbavif = null;
|
||||
thumbx = null;
|
||||
thumby = null;
|
||||
}
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
await manifestAccess(companyId, manifestId);
|
||||
|
||||
@@ -63,10 +122,22 @@ export default function add(pool) {
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
let sort = rawSort;
|
||||
if (sort === undefined || sort === null) {
|
||||
const sortRes = await request(pool, tx)
|
||||
.input('manifestId', manifestId)
|
||||
.query(GET_NEXT_SORT);
|
||||
sort = sortRes.recordset[0].sort;
|
||||
}
|
||||
|
||||
const fileRes = await request(pool, tx)
|
||||
.input('hash', hash)
|
||||
.input('mimetype', mimetype)
|
||||
.input('name', name)
|
||||
.input('content', content)
|
||||
.input('thumbavif', thumbavif)
|
||||
.input('thumbx', thumbx)
|
||||
.input('thumby', thumby)
|
||||
.query(CREATE_FILE);
|
||||
|
||||
const fileId = fileRes.recordset[0].id;
|
||||
@@ -74,6 +145,7 @@ export default function add(pool) {
|
||||
const attachmentRes = await request(pool, tx)
|
||||
.input('manifestId', manifestId)
|
||||
.input('fileId', fileId)
|
||||
.input('sort', sort)
|
||||
.query(CREATE_ATTACHMENT);
|
||||
|
||||
const attachmentId = attachmentRes.recordset[0].id;
|
||||
@@ -84,8 +156,13 @@ export default function add(pool) {
|
||||
id: attachmentId,
|
||||
manifestId,
|
||||
fileId,
|
||||
sort,
|
||||
hash,
|
||||
mimetype,
|
||||
name,
|
||||
thumbavif,
|
||||
thumbx,
|
||||
thumby,
|
||||
};
|
||||
} catch (err) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user