2022-01-12 05:31:58 +00:00
|
|
|
import type { EmbeddedFile, ImageProcessor } from "./main";
|
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 = [
|
2022-01-18 08:34:34 +00:00
|
|
|
{ host: 'Catbox', prefix: 'files.catbox.moe/' },
|
|
|
|
{ host: 'Litter', prefix: 'litter.catbox.moe/' },
|
2022-03-10 22:58:06 +00:00
|
|
|
{ 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-04-12 18:24:49 +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;
|
2022-01-18 08:34:34 +00:00
|
|
|
let source: string | undefined;
|
2022-01-13 20:14:29 +00:00
|
|
|
try {
|
2022-04-12 18:24:49 +00:00
|
|
|
if (isB64) {
|
2022-01-13 20:14:29 +00:00
|
|
|
ext = atob(isB64[1]);
|
|
|
|
} else if (isExt) {
|
2022-01-18 08:34:34 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-01-13 20:14:29 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
/**/
|
2022-01-12 05:31:58 +00:00
|
|
|
}
|
2022-01-18 08:34:34 +00:00
|
|
|
return { ext, source };
|
2022-01-12 05:31:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const extract = async (b: Buffer, fn?: string) => {
|
2022-01-18 08:34:34 +00:00
|
|
|
const { ext, source } = getExt(fn!);
|
2022-01-12 05:31:58 +00:00
|
|
|
|
|
|
|
let rsource: string;
|
2022-01-18 08:34:34 +00:00
|
|
|
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);
|
2022-01-18 08:34:34 +00:00
|
|
|
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-01-12 08:09:30 +00:00
|
|
|
data: csettings.hotlink ? rsource! : async (lsn) => {
|
2022-01-12 05:31:58 +00:00
|
|
|
try {
|
2022-01-31 13:09:46 +00:00
|
|
|
return Buffer.from(await (await ifetch(rsource, undefined, lsn)).arrayBuffer());
|
2022-01-12 05:31:58 +00:00
|
|
|
} catch (e) {
|
|
|
|
//404
|
|
|
|
}
|
|
|
|
},
|
2022-01-31 13:12:39 +00:00
|
|
|
thumbnail: Buffer.from(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) => {
|
2022-01-18 08:34:34 +00:00
|
|
|
const { ext, source } = getExt(fn!);
|
2022-01-12 05:31:58 +00:00
|
|
|
if (!ext)
|
|
|
|
return false;
|
2022-01-18 08:34:34 +00:00
|
|
|
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;
|