Browse Source

Still report embeds when fetching for central cache

pull/46/head
coomdev 2 years ago
parent
commit
f5afd85cfc
  1. 38
      src/main.ts

38
src/main.ts

@ -87,14 +87,13 @@ type EmbeddedFileWithoutPreview = {
export type EmbeddedFile = EmbeddedFileWithPreview | EmbeddedFileWithoutPreview;
const processImage = async (srcs: AsyncGenerator<string, void, void>, fn: string, hex: string, prevurl: string, onfound: () => void) => {
const processImage = async (srcs: AsyncGenerator<string, void, void>, fn: string, hex: string, prevurl: string) => {
const ret = await Promise.all(processors.filter(e => e.match(fn)).map(async proc => {
if (proc.skip) {
// skip file downloading, file is referenced from the filename
// basically does things like filtering out blacklisted tags
const md5 = Buffer.from(hex, 'base64');
if (await proc.has_embed(md5, fn, prevurl) === true) {
onfound();
return [await proc.extract(md5, fn), true] as [EmbeddedFile[], boolean];
}
return;
@ -128,7 +127,6 @@ const processImage = async (srcs: AsyncGenerator<string, void, void>, fn: string
//console.log(`Gave up on ${src} after downloading ${cumul.byteLength} bytes...`);
return;
}
onfound();
return [await proc.extract(cumul), false] as [EmbeddedFile[], boolean];
} catch {
// ignore error and retry with another link
@ -199,29 +197,31 @@ const processPost = async (post: HTMLDivElement) => {
return;
let res2: [EmbeddedFile[], boolean][] | undefined = undefined;
const reportEmbed = () => {
if (csettings.tm) {
// dont report results from archive, only live threads
if (['boards.4chan.org', 'boards.4channel.org'].includes(location.host)) {
if (!cappState.isCatalog) { // only save from within threads
// we must be in a thread, thus the following is valid
const op = +location.pathname.match(/\/thread\/(.*)/)![1];
pendingPosts.push({ id: +(post.id.match(/([0-9]+)/)![1]), op });
signalNewEmbeds(); // let it run async
}
}
}
};
if (shouldUseCache()) {
res2 = await getEmbedsFromCache(qp.getCurrentBoard(), +qp.getCurrentThread()!, post.id);
}
if (!res2) {
res2 = await processImage(origlink, qp.getFilename(post), qp.getMD5(post), thumbLink,
() => {
if (csettings.tm) {
// dont report results from archive, only live threads
if (['boards.4chan.org', 'boards.4channel.org'].includes(location.host)) {
if (!cappState.isCatalog) { // only save from within threads
// we must be in a thread, thus the following is valid
const op = +location.pathname.match(/\/thread\/(.*)/)![1];
pendingPosts.push({ id: +(post.id.match(/([0-9]+)/)![1]), op });
signalNewEmbeds(); // let it run async
}
}
}
post.querySelector('.post')?.classList.add("embedfound");
});
res2 = await processImage(origlink, qp.getFilename(post), qp.getMD5(post), thumbLink);
res2 = res2?.filter(e => e);
}
if (!res2 || res2.length == 0)
return;
reportEmbed();
post.querySelector('.post')?.classList.add("embedfound");
processAttachments(post, res2?.flatMap(e => e![0].map(k => [k, e![1]] as [EmbeddedFile, boolean])));
};
@ -694,7 +694,7 @@ function processAttachments(post: HTMLDivElement, ress: [EmbeddedFile, boolean][
target: viewcounthost,
props: {
board: qp.getCurrentBoard(),
op: cappState.isCatalog ? pid : qp.getCurrentThread(),
op: cappState.isCatalog ? pid : qp.getCurrentThread()!,
pid
}
});

Loading…
Cancel
Save