mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
96 lines
2.4 KiB
TypeScript
96 lines
2.4 KiB
TypeScript
import type { ContentFormatSchema } from "@versia/sdk/schemas";
|
|
import { htmlToText as htmlToTextLib } from "html-to-text";
|
|
import { lookup } from "mime-types";
|
|
import type { z } from "zod";
|
|
import { config } from "~/config.ts";
|
|
|
|
export const getBestContentType = (
|
|
content?: z.infer<typeof ContentFormatSchema> | null,
|
|
): {
|
|
content: string;
|
|
format: string;
|
|
} => {
|
|
if (!content) {
|
|
return { content: "", format: "text/plain" };
|
|
}
|
|
|
|
const bestFormatsRanked = [
|
|
"text/x.misskeymarkdown",
|
|
"text/html",
|
|
"text/markdown",
|
|
"text/plain",
|
|
];
|
|
|
|
for (const format of bestFormatsRanked) {
|
|
if (content[format]) {
|
|
return { content: content[format].content, format };
|
|
}
|
|
}
|
|
|
|
return { content: "", format: "text/plain" };
|
|
};
|
|
|
|
export const urlToContentFormat = (
|
|
url: URL,
|
|
contentType?: string,
|
|
): z.infer<typeof ContentFormatSchema> | null => {
|
|
if (url.href.startsWith("https://api.dicebear.com/")) {
|
|
return {
|
|
"image/svg+xml": {
|
|
content: url.toString(),
|
|
remote: true,
|
|
},
|
|
};
|
|
}
|
|
const mimeType =
|
|
contentType ||
|
|
lookup(url.toString().replace(url.search, "")) ||
|
|
"application/octet-stream";
|
|
|
|
return {
|
|
[mimeType]: {
|
|
content: url.toString(),
|
|
remote: true,
|
|
},
|
|
};
|
|
};
|
|
|
|
export const mimeLookup = (url: URL): Promise<string> => {
|
|
const urlWithoutSearch = url.toString().replace(url.search, "");
|
|
|
|
// Strip query params from URL to get the proper file extension
|
|
const naiveLookup = lookup(urlWithoutSearch);
|
|
|
|
if (naiveLookup) {
|
|
return Promise.resolve(naiveLookup);
|
|
}
|
|
|
|
const fetchLookup = fetch(url, {
|
|
method: "HEAD",
|
|
// @ts-expect-error Proxy is a Bun-specific feature
|
|
proxy: config.http.proxy_address,
|
|
})
|
|
.then(
|
|
(response) =>
|
|
response.headers.get("content-type") ||
|
|
"application/octet-stream",
|
|
)
|
|
.catch(() => "application/octet-stream");
|
|
|
|
return fetchLookup;
|
|
};
|
|
|
|
export const htmlToText = (html: string): string => {
|
|
return htmlToTextLib(html, {
|
|
selectors: [
|
|
{
|
|
selector: "a",
|
|
options: {
|
|
hideLinkHrefIfSameAsText: true,
|
|
ignoreHref: true,
|
|
},
|
|
},
|
|
],
|
|
});
|
|
};
|