You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.2 KiB
82 lines
2.2 KiB
import { EmbeddedFile, EMBED_STATUS, EMBED_TYPES, ImageProcessor } from "./main";
|
|
import { GM_fetch, GM_head } from "./requests";
|
|
import type { Buffer } from "buffer";
|
|
import thumbnail from "./assets/hasembed.png";
|
|
import { settings } from "./stores";
|
|
|
|
const sources = [
|
|
{ host: 'Catbox', prefix: 'https://files.catbox.moe/' },
|
|
{ host: 'Litter', prefix: 'https://litter.catbox.moe/' },
|
|
{ host: 'Pomf', prefix: 'https://a.pomf.cat/' },
|
|
];
|
|
|
|
export let csettings: Parameters<typeof settings['set']>[0];
|
|
settings.subscribe(b => {
|
|
csettings = b;
|
|
});
|
|
|
|
const getExt = (fn: string) => {
|
|
const isDum = fn!.match(/^([a-z0-9]{6}\.(?:jpe?g|png|webm|gif))/gi);
|
|
const isB64 = fn!.match(/^((?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=))?\.(gif|jpe?g|png|webm)/);
|
|
const isExt = fn!.match(/\[.*=(.*)\]/);
|
|
let ext;
|
|
if (isDum) {
|
|
ext = isDum[0];
|
|
} else if (isB64) {
|
|
ext = atob(isB64[1]);
|
|
} else if (isExt) {
|
|
ext = isExt[1];
|
|
}
|
|
return ext;
|
|
};
|
|
|
|
const extract = async (b: Buffer, fn?: string) : Promise<EmbeddedFile[]> => {
|
|
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
|
|
}
|
|
}
|
|
|
|
return [{
|
|
embed_type: EMBED_TYPES.EXTERNAL_POMF,
|
|
filename: ext,
|
|
data: csettings.hotlink ? rsource! : async (lsn) => {
|
|
try {
|
|
return (await GM_fetch(rsource, undefined, lsn)).arrayBuffer();
|
|
} catch (e) {
|
|
//404
|
|
}
|
|
},
|
|
// thumbnail
|
|
} as EmbeddedFile];
|
|
};
|
|
|
|
const has_embed = async (b: Buffer, fn?: string) : Promise<EMBED_STATUS> => {
|
|
const ext = getExt(fn!);
|
|
if (!ext)
|
|
return EMBED_STATUS.NONE;
|
|
for (const source of sources) {
|
|
try {
|
|
const e = await GM_head(source.prefix + ext);
|
|
return EMBED_STATUS.SUCCESS;
|
|
} catch {
|
|
// 404
|
|
}
|
|
}
|
|
|
|
return EMBED_STATUS.NONE;
|
|
};
|
|
|
|
export default {
|
|
skip: true,
|
|
extract,
|
|
has_embed,
|
|
match: fn => !!getExt(fn)
|
|
} as ImageProcessor;
|