PEE/src/pomf.ts

85 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-01-12 05:31:58 +00:00
import type { EmbeddedFile, ImageProcessor } from "./main";
import { GM_fetch, GM_head } from "./requests";
import type { Buffer } from "buffer";
import thumbnail from "./assets/hasembed.png";
2022-01-12 08:09:30 +00:00
import { settings } from "./stores";
2022-01-12 05:31:58 +00:00
const sources = [
{ host: 'Catbox', prefix: 'https://files.catbox.moe/' },
{ host: 'Litter', prefix: 'https://litter.catbox.moe/' },
{ host: 'Pomf', prefix: 'https://a.pomf.cat/' },
];
2022-01-12 08:09:30 +00:00
export let csettings: Parameters<typeof settings['set']>[0];
settings.subscribe(b => {
csettings = b;
});
2022-01-12 05:31:58 +00:00
const getExt = (fn: string) => {
2022-01-13 07:44:47 +00:00
const isDum = fn!.match(/^[a-z0-9]{6}\./i);
2022-01-12 06:58:46 +00:00
const isB64 = fn!.match(/^((?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=))?\.(gif|jpe?g|png|webm)/);
2022-01-12 05:31:58 +00:00
const isExt = fn!.match(/\[.*=(.*)\]/);
let ext;
try {
if (isDum) {
ext = fn.split('.').slice(0, -1).join('.');
} else if (isB64) {
ext = atob(isB64[1]);
} else if (isExt) {
ext = isExt[1];
}
} catch {
/**/
2022-01-12 05:31:58 +00:00
}
return ext;
};
const extract = async (b: Buffer, fn?: string) => {
const ext = getExt(fn!);
let rsource: string;
for (const source of sources) {
try {
await GM_head(source.prefix + ext);
rsource = source.prefix + ext;
break;
} catch {
// 404
}
}
2022-01-12 06:58:46 +00:00
return [{
2022-01-12 05:31:58 +00:00
filename: ext,
2022-01-12 08:09:30 +00:00
data: csettings.hotlink ? rsource! : async (lsn) => {
2022-01-12 05:31:58 +00:00
try {
return (await GM_fetch(rsource, undefined, lsn)).arrayBuffer();
} catch (e) {
//404
}
},
thumbnail
2022-01-12 06:58:46 +00:00
} as EmbeddedFile];
2022-01-12 05:31:58 +00:00
};
const has_embed = async (b: Buffer, fn?: string) => {
const ext = getExt(fn!);
if (!ext)
return false;
for (const source of sources) {
try {
const e = await GM_head(source.prefix + ext);
return true;
} catch {
// 404
}
}
2022-01-12 06:58:46 +00:00
2022-01-12 05:31:58 +00:00
return false;
};
export default {
skip: true,
extract,
has_embed,
2022-01-12 06:58:46 +00:00
match: fn => !!getExt(fn)
2022-01-12 05:31:58 +00:00
} as ImageProcessor;