u
This commit is contained in:
@@ -103,6 +103,7 @@ export default function addImage(provider) {
|
||||
companyuserId,
|
||||
fullHash,
|
||||
sort: effectiveSort,
|
||||
skelehash: processed.skelehash,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -124,6 +125,7 @@ export default function addImage(provider) {
|
||||
},
|
||||
fullHash,
|
||||
thumbHash: processed.thumb.hash,
|
||||
skelehash: processed.skelehash,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import sharp from 'sharp';
|
||||
import { rgbaToThumbHash } from 'thumbhash';
|
||||
import { encodeRGBA } from 'skelehash-wasm';
|
||||
|
||||
const FULL_MAX = 1000;
|
||||
const THUMB_MAX = 200;
|
||||
@@ -52,11 +53,22 @@ export default async function processAvifImage(inputBuffer) {
|
||||
.toBuffer({ resolveWithObject: true }),
|
||||
]);
|
||||
|
||||
const thumbHashBytes = rgbaToThumbHash(
|
||||
thumbRgba.info.width,
|
||||
thumbRgba.info.height,
|
||||
thumbRgba.data,
|
||||
);
|
||||
const [thumbHashBytes, skele] = await Promise.all([
|
||||
rgbaToThumbHash(
|
||||
thumbRgba.info.width,
|
||||
thumbRgba.info.height,
|
||||
thumbRgba.data,
|
||||
),
|
||||
encodeRGBA(
|
||||
thumbRgba.info.width,
|
||||
thumbRgba.info.height,
|
||||
thumbRgba.data,
|
||||
),
|
||||
]);
|
||||
|
||||
if (!skele?.base62) {
|
||||
throw new Error('encodeRGBA failed to produce a base62 skelehash');
|
||||
}
|
||||
|
||||
return {
|
||||
full: {
|
||||
@@ -77,5 +89,6 @@ export default async function processAvifImage(inputBuffer) {
|
||||
h: smallThumb.info.height,
|
||||
buffer: smallThumb.data,
|
||||
},
|
||||
skelehash: skele.base62,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user