This commit is contained in:
seb
2026-10-18 14:51:33 +02:00
parent d587f31953
commit f6b0ff7483
5 changed files with 92 additions and 17 deletions

View File

@@ -1,3 +1,5 @@
import sharp from 'sharp';
import { encodeRGBA } from 'skelehash-wasm';
import { DELETE_BY_ARTICLE_AND_HASH as MSSQL_QUERY } from '../../providers/mssql/queries/pic.js';
import { DELETE_BY_ARTICLE_AND_HASH as SQLITE_QUERY } from '../../providers/sqlite3/queries/pic.js';
import resolveArticleBySku from './resolveArticleBySku.js';
@@ -24,6 +26,55 @@ export default function deleteImage(provider) {
return async (googleAuthId, sku, fullHash, tx) => {
const runDelete = async (runner) => {
const { articleId, companyuserId } = await resolve(googleAuthId, sku, runner);
let thumbBuffer;
if (provider?.dialect === 'sqlite3') {
const exists = await runner.query(
'SELECT ap.id FROM articlepic ap INNER JOIN pic p ON p.id = ap.pic_id WHERE ap.article_id = @articleId AND p.hash = @fullHash',
{ articleId, fullHash },
);
if (!exists.rows[0]) {
throw new Error('Image not found');
}
thumbBuffer = await (runner.readPicThumb ? runner.readPicThumb(fullHash) : provider.readPicThumb?.(fullHash));
} else {
const picResult = await runner.query(
'SELECT p.thumbbinary FROM pic p INNER JOIN articlepic ap ON ap.pic_id = p.id WHERE ap.article_id = @articleId AND p.hash = @fullHash',
{ articleId, fullHash },
);
if (!picResult.rows[0]) {
throw new Error('Image not found');
}
thumbBuffer = picResult.rows[0]?.thumbbinary;
}
let skelehash = null;
if (thumbBuffer) {
const { data, info } = await sharp(thumbBuffer).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const res = await encodeRGBA(info.width, info.height, data);
skelehash = res?.base62;
}
if (!skelehash) {
const historyRows = await runner.query(
'SELECT meta FROM articlehistory WHERE article_id = @articleId AND meta LIKE @pattern',
{ articleId, pattern: `%"${fullHash}"%` },
);
for (const row of historyRows.rows || []) {
try {
const m = typeof row.meta === 'string' ? JSON.parse(row.meta) : row.meta;
if (m?.fullHash === fullHash && m?.skelehash) {
skelehash = m.skelehash;
break;
}
} catch {}
}
}
if (!skelehash) {
throw new Error(`Failed to resolve skelehash for image ${fullHash}`);
}
const result = await runner.query(sql, { articleId, fullHash });
if (!result.rowsAffected) {
@@ -42,6 +93,7 @@ export default function deleteImage(provider) {
articleId,
companyuserId,
fullHash,
skelehash,
});
};