2024-08-26 19:06:49 +02:00
|
|
|
import type { ContentFormat } from "@versia/federation/types";
|
2024-04-10 04:05:02 +02:00
|
|
|
import { lookup } from "mime-types";
|
2024-06-26 05:13:40 +02:00
|
|
|
import { config } from "~/packages/config-manager";
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2024-11-02 00:43:33 +01:00
|
|
|
export const getBestContentType = (
|
|
|
|
|
content?: ContentFormat | null,
|
|
|
|
|
): {
|
|
|
|
|
content: string;
|
|
|
|
|
format: string;
|
|
|
|
|
} => {
|
2024-06-13 04:26:43 +02:00
|
|
|
if (!content) {
|
|
|
|
|
return { content: "", format: "text/plain" };
|
|
|
|
|
}
|
2024-04-10 04:05:02 +02:00
|
|
|
|
|
|
|
|
const bestFormatsRanked = [
|
|
|
|
|
"text/x.misskeymarkdown",
|
|
|
|
|
"text/html",
|
|
|
|
|
"text/markdown",
|
|
|
|
|
"text/plain",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (const format of bestFormatsRanked) {
|
2024-06-13 04:26:43 +02:00
|
|
|
if (content[format]) {
|
2024-04-10 04:05:02 +02:00
|
|
|
return { content: content[format].content, format };
|
2024-06-13 04:26:43 +02:00
|
|
|
}
|
2024-04-07 07:30:49 +02:00
|
|
|
}
|
2024-04-10 04:05:02 +02:00
|
|
|
|
|
|
|
|
return { content: "", format: "text/plain" };
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-16 23:57:21 +01:00
|
|
|
export const urlToContentFormat = (
|
|
|
|
|
url: string,
|
|
|
|
|
contentType?: string,
|
|
|
|
|
): ContentFormat | null => {
|
2024-06-13 04:26:43 +02:00
|
|
|
if (!url) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-10 04:05:02 +02:00
|
|
|
if (url.startsWith("https://api.dicebear.com/")) {
|
|
|
|
|
return {
|
|
|
|
|
"image/svg+xml": {
|
|
|
|
|
content: url,
|
2024-08-26 19:06:49 +02:00
|
|
|
remote: true,
|
2024-04-10 04:05:02 +02:00
|
|
|
},
|
|
|
|
|
};
|
2024-04-07 07:30:49 +02:00
|
|
|
}
|
2024-04-10 04:05:02 +02:00
|
|
|
const mimeType =
|
2024-12-16 23:57:21 +01:00
|
|
|
contentType ||
|
2024-04-10 04:05:02 +02:00
|
|
|
lookup(url.replace(new URL(url).search, "")) ||
|
|
|
|
|
"application/octet-stream";
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
[mimeType]: {
|
|
|
|
|
content: url,
|
2024-08-26 19:06:49 +02:00
|
|
|
remote: true,
|
2024-04-10 04:05:02 +02:00
|
|
|
},
|
|
|
|
|
};
|
2023-11-05 00:59:55 +01:00
|
|
|
};
|
2024-05-12 03:27:28 +02:00
|
|
|
|
2024-10-03 11:43:16 +02:00
|
|
|
export const mimeLookup = (url: string): Promise<string> => {
|
2024-05-12 03:27:28 +02:00
|
|
|
const naiveLookup = lookup(url.replace(new URL(url).search, ""));
|
|
|
|
|
|
2024-06-13 04:26:43 +02:00
|
|
|
if (naiveLookup) {
|
2024-10-03 11:43:16 +02:00
|
|
|
return Promise.resolve(naiveLookup);
|
2024-06-13 04:26:43 +02:00
|
|
|
}
|
2024-05-12 03:27:28 +02:00
|
|
|
|
2024-06-26 05:13:40 +02:00
|
|
|
const fetchLookup = fetch(url, {
|
|
|
|
|
method: "HEAD",
|
2024-08-26 18:15:14 +02:00
|
|
|
// @ts-expect-error Proxy is a Bun-specific feature
|
2024-06-26 05:13:40 +02:00
|
|
|
proxy: config.http.proxy.address,
|
2024-12-16 23:57:21 +01:00
|
|
|
})
|
|
|
|
|
.then(
|
|
|
|
|
(response) =>
|
|
|
|
|
response.headers.get("content-type") ||
|
|
|
|
|
"application/octet-stream",
|
|
|
|
|
)
|
|
|
|
|
.catch(() => "application/octet-stream");
|
2024-05-12 03:27:28 +02:00
|
|
|
|
|
|
|
|
return fetchLookup;
|
|
|
|
|
};
|