This commit is contained in:
seb
2026-10-09 21:43:20 +02:00
parent ba376686f3
commit 906ebafbbe
6 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
import { GET_SMALLTHUMB_BY_HASH as MSSQL_GET } from '../../providers/mssql/queries/pic.js';
import { GET_SMALLTHUMB_BY_HASH as SQLITE_GET } from '../../providers/sqlite3/queries/pic.js';
import resolvePicAccess from './resolvePicAccess.js';
import makeRunner from '../runner.js';
export default function getSmallThumbByHash(provider) {
const authorize = resolvePicAccess(provider);
const sql = provider?.dialect === 'sqlite3' ? SQLITE_GET : MSSQL_GET;
/**
* Returns the small thumbnail image (AVIF, 40px max edge) for a given hash.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} fullHash - The full image hash.
* @param {object} [tx] - Optional transaction to run within.
* @returns {Promise<{buffer: Buffer, w: number, h: number, contentType: string}>} The small thumbnail image.
* @throws {Error} If the image is not found.
*
* @example
* const thumb = await store.pic.getSmallThumbByHash(42, 'abc123');
* // => { buffer, w, h, contentType: 'image/avif' }
*/
return async (googleAuthId, fullHash, tx) => {
await authorize(googleAuthId, fullHash, tx);
const runner = makeRunner(provider, tx);
const result = await runner.query(sql, { hash: fullHash });
const row = result.rows[0];
if (!row) {
throw new Error('Image not found');
}
const buffer = row.buffer ?? row.smallthumbbinary;
if (!buffer) {
throw new Error('Image not found');
}
return {
buffer,
w: row.w,
h: row.h,
contentType: 'image/avif',
};
};
}