PEE/src/pomf.ts

93 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-08-06 12:08:17 +00:00
import type { ImageProcessor, WorkerEmbeddedFile } from './processor.worker';
2022-01-31 13:09:46 +00:00
import { Buffer } from "buffer";
2022-01-12 05:31:58 +00:00
import thumbnail from "./assets/hasembed.png";
2022-01-12 08:09:30 +00:00
import { settings } from "./stores";
2022-01-29 20:01:45 +00:00
import { getHeaders, ifetch, Platform } from "./platform";
2022-01-12 05:31:58 +00:00
const sources = [
{ host: 'Catbox', prefix: 'files.catbox.moe/' },
{ host: 'Litter', prefix: 'litter.catbox.moe/' },
{ host: 'Zzzz', prefix: 'z.zz.fo/' }
2022-01-12 05:31:58 +00:00
];
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-08-06 12:08:17 +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;
let source: string | undefined;
try {
2022-04-12 18:24:49 +00:00
if (isB64) {
ext = atob(isB64[1]);
} else if (isExt) {
ext = decodeURIComponent(isExt[1]);
if (ext.startsWith('https://'))
ext = ext.slice('https://'.length);
for (const cs of sources)
if (ext.startsWith(cs.prefix)) {
source = cs.prefix;
ext = ext.slice(cs.prefix.length);
break;
}
}
} catch {
/**/
2022-01-12 05:31:58 +00:00
}
return { ext, source };
2022-01-12 05:31:58 +00:00
};
const extract = async (b: Buffer, fn?: string) => {
2022-07-24 17:22:39 +00:00
if (!csettings)
throw new Error("Settings uninit");
const { ext, source } = getExt(fn!);
2022-01-12 05:31:58 +00:00
let rsource: string;
for (const cs of sources) {
if (source && cs.prefix != source)
continue;
2022-01-12 05:31:58 +00:00
try {
2022-01-29 20:01:45 +00:00
await getHeaders('https://' + cs.prefix + ext);
rsource = 'https://' + cs.prefix + ext;
2022-01-12 05:31:58 +00:00
break;
} catch {
// 404
}
}
2022-01-12 06:58:46 +00:00
return [{
2022-01-12 05:31:58 +00:00
filename: ext,
2022-08-06 12:08:17 +00:00
data: csettings.hotlink ? rsource! : { url: rsource! },
2022-01-31 13:12:39 +00:00
thumbnail: Buffer.from(thumbnail)
2022-08-06 12:08:17 +00:00
} as WorkerEmbeddedFile];
2022-01-12 05:31:58 +00:00
};
const has_embed = async (b: Buffer, fn?: string) => {
const { ext, source } = getExt(fn!);
2022-01-12 05:31:58 +00:00
if (!ext)
return false;
for (const cs of sources) {
if (source && cs.prefix != source)
continue;
2022-01-12 05:31:58 +00:00
try {
2022-01-29 20:01:45 +00:00
const e = await getHeaders('https://' + cs.prefix + ext);
2022-01-12 05:31:58 +00:00
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;