This commit is contained in:
seb
2026-10-06 11:19:33 +02:00
parent 1d1017e44b
commit b99e767c57
8 changed files with 301 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
import { getStore } from './store.js';
import { getStore, getProvider } from './store.js';
export async function list(googleAuthId) {
const store = await getStore();
@@ -100,4 +100,79 @@ export async function setMaxKnownTombstoneId(id, maxknowntombstoneid) {
return store.posterminal.setMaxKnownTombstoneId(id, maxknowntombstoneid);
}
/**
* Retrieves an image buffer by hash for POS terminals.
* @param {string} hash - SHA-256 hash or thumbhash
* @param {object} [options]
* @param {number} [options.width=200]
* @param {number} [options.height=200]
* @returns {Promise<{buffer: Buffer, w: number, h: number, fullHash: string} | null>}
*/
export async function getPosImageByHash(hash, { width = 200, height = 200 } = {}) {
if (!hash || typeof hash !== 'string') return null;
const cleanHash = hash.trim();
if (!cleanHash) return null;
const dotIndex = cleanHash.lastIndexOf('.');
const baseHash = dotIndex > 0 ? cleanHash.slice(0, dotIndex) : cleanHash;
const provider = await getProvider();
const isSqlite = provider?.dialect === 'sqlite3';
if (isSqlite) {
const result = await provider.query(
'SELECT w, h, size, hash, thumbw, thumbh, thumbhash FROM pic WHERE hash = @hash OR thumbhash = @hash OR hash = @baseHash OR thumbhash = @baseHash LIMIT 1',
{ hash: cleanHash, baseHash }
);
const row = result.rows[0];
if (!row) return null;
const fullHash = row.hash;
const useThumb = width <= 200 && height <= 200;
let buffer = null;
if (useThumb) {
buffer = await provider.readPicThumb?.(fullHash);
}
if (!buffer) {
buffer = await provider.readPicFull?.(fullHash);
}
if (!buffer && !useThumb) {
buffer = await provider.readPicThumb?.(fullHash);
}
if (!buffer) return null;
return {
buffer: Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer),
w: row.w,
h: row.h,
fullHash,
};
}
const result = await provider.query(
'SELECT TOP 1 w, h, size, hash, thumbw, thumbh, thumbhash, fullbinary, thumbbinary FROM pic WHERE hash = @hash OR thumbhash = @hash OR hash = @baseHash OR thumbhash = @baseHash',
{ hash: cleanHash, baseHash }
);
const row = result.rows[0];
if (!row) return null;
const fullHash = row.hash;
const useThumb = width <= 200 && height <= 200;
const rawBuffer = useThumb
? (row.thumbbinary || row.fullbinary)
: (row.fullbinary || row.thumbbinary);
if (!rawBuffer) return null;
return {
buffer: Buffer.isBuffer(rawBuffer) ? rawBuffer : Buffer.from(rawBuffer),
w: row.w,
h: row.h,
fullHash,
};
}