Merge pull request #1451 from supercoolspy/fix/singlefile-title

Fix/singlefile title
This commit is contained in:
Daniel
2025-10-15 14:29:38 +03:30
committed by GitHub
2 changed files with 26 additions and 10 deletions

View File

@@ -4,7 +4,10 @@ import http from "http";
import { HttpsProxyAgent } from "https-proxy-agent";
import { SocksProxyAgent } from "socks-proxy-agent";
export default async function fetchTitleAndHeaders(url: string) {
export default async function fetchTitleAndHeaders(
url: string,
content?: string
) {
if (!url?.startsWith("http://") && !url?.startsWith("https://"))
return { title: "", headers: null };
@@ -48,13 +51,20 @@ export default async function fetchTitleAndHeaders(url: string) {
const response = await Promise.race([responsePromise, timeoutPromise]);
if ((response as any)?.status) {
const text = await (response as any).text();
let text: string;
if (content) {
text = content;
} else {
text = await (response as any).text();
}
const headers = (response as Response)?.headers || null;
// regular expression to find the <title> tag
let match = text.match(/<title.*>([^<]*)<\/title>/);
const title = match?.[1] || "";
const headers = (response as Response)?.headers || null;
return { title, headers };
} else {

View File

@@ -146,7 +146,19 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) {
if (!collection) {
throw new Error("Collection not found.");
}
const { title = "" } = url ? await fetchTitleAndHeaders(url) : {};
// Generate a preview if it's an image
const { mimetype } = files.file[0];
const isPDF = mimetype?.includes("pdf");
const isImage = mimetype?.includes("image");
const isHTML = mimetype === "text/html";
const { title = "" } = url
? await fetchTitleAndHeaders(
url,
isHTML && !isPreview ? fileBuffer.toString("utf-8") : undefined
)
: {};
const link = await prisma.link.create({
data: {
@@ -170,12 +182,6 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) {
},
});
// Generate a preview if it's an image
const { mimetype } = files.file[0];
const isPDF = mimetype?.includes("pdf");
const isImage = mimetype?.includes("image");
const isHTML = mimetype === "text/html";
if (isImage) {
const collectionId = collection.id;
createFolder({ filePath: `archives/preview/${collectionId}` });