mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-10 07:25:35 +00:00
feat: more verbose favicon console.log
This commit is contained in:
@@ -8,15 +8,17 @@ export default async function handler(
|
|||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse
|
res: NextApiResponse
|
||||||
) {
|
) {
|
||||||
const urlRequest = (req.query?.url as string) || "";
|
const urlParam = (req.query?.url as string) || "";
|
||||||
if (!urlRequest) {
|
if (!urlParam) {
|
||||||
throw new Error("URL's missing");
|
throw new Error("URL's missing");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const urlRequest = urlParam + "/favicon.ico";
|
||||||
try {
|
try {
|
||||||
const { favicon, type, size } = await downloadImageFromUrl(
|
const { favicon, type, size } = await downloadImageFromUrl(
|
||||||
urlRequest + "/favicon.ico"
|
urlRequest + "/favicon.ico"
|
||||||
);
|
);
|
||||||
|
console.log("[Favicon]", `[first: ${urlRequest}]`, "request");
|
||||||
if (size === 0) {
|
if (size === 0) {
|
||||||
throw new Error("Empty favicon");
|
throw new Error("Empty favicon");
|
||||||
}
|
}
|
||||||
@@ -30,20 +32,22 @@ export default async function handler(
|
|||||||
size,
|
size,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
const errorMessage = error?.message || "Unable to retrieve favicon";
|
||||||
|
console.error("[Favicon]", `[first: ${urlRequest}]`, errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log("[Favicon]", `[second: ${urlRequest}]`, "new request");
|
||||||
const requestDocument = await makeRequest(urlRequest);
|
const requestDocument = await makeRequest(urlRequest);
|
||||||
const text = await requestDocument.text();
|
const text = await requestDocument.text();
|
||||||
|
|
||||||
const faviconPath = findFaviconPath(text);
|
const faviconPath = findFaviconPath(text);
|
||||||
if (!faviconPath) {
|
if (!faviconPath) {
|
||||||
throw new Error("[Favicon] Unable to find favicon path");
|
throw new Error("Unable to find favicon path");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isBase64Image(faviconPath)) {
|
if (isBase64Image(faviconPath)) {
|
||||||
console.log("[Favicon] base64, convert it to buffer");
|
console.log("base64, convert it to buffer");
|
||||||
const buffer = convertBase64ToBuffer(faviconPath);
|
const buffer = convertBase64ToBuffer(faviconPath);
|
||||||
return sendImage({
|
return sendImage({
|
||||||
content: buffer,
|
content: buffer,
|
||||||
@@ -68,8 +72,9 @@ export default async function handler(
|
|||||||
size,
|
size,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
const errorMessage = error?.message || "Unable to retrieve favicon";
|
||||||
res.status(404).send({ error: "Unable to retrieve favicon" });
|
console.log("[Favicon]", `[second: ${urlRequest}]`, errorMessage);
|
||||||
|
res.status(404).send({ error: errorMessage });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +82,7 @@ async function makeRequest(url: string) {
|
|||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
headers.set("User-Agent", USER_AGENT);
|
headers.set("User-Agent", USER_AGENT);
|
||||||
|
|
||||||
const request = await fetch(url, { headers });
|
const request = await fetch(url, { headers, redirect: "follow" });
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,19 +119,27 @@ function sendImage({
|
|||||||
res.send(content);
|
res.send(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const selectors = [
|
||||||
|
"link[rel='icon']",
|
||||||
|
"link[rel='shortcut icon']",
|
||||||
|
"link[rel='apple-touch-icon']",
|
||||||
|
"link[rel='apple-touch-icon-precomposed']",
|
||||||
|
"link[rel='apple-touch-startup-image']",
|
||||||
|
"link[rel='mask-icon']",
|
||||||
|
"link[rel='fluid-icon']",
|
||||||
|
];
|
||||||
|
|
||||||
function findFaviconPath(text) {
|
function findFaviconPath(text) {
|
||||||
const document = parse(text);
|
const document = parse(text);
|
||||||
const links = document.querySelectorAll(
|
const links = document.querySelectorAll(selectors.join(", "));
|
||||||
'link[rel="icon"], link[rel="shortcut icon"]'
|
|
||||||
);
|
|
||||||
const link = links.find(
|
const link = links.find(
|
||||||
(link) => !link.getAttribute("href").startsWith("data:image/")
|
(link) => !link.getAttribute("href").startsWith("data:image/")
|
||||||
);
|
);
|
||||||
if (!link) {
|
if (!link || !link.getAttribute("href")) {
|
||||||
return console.warn("nothing, exit");
|
throw new Error("No link/href attribute found");
|
||||||
}
|
}
|
||||||
|
|
||||||
return link.getAttribute("href") || "";
|
return link.getAttribute("href");
|
||||||
}
|
}
|
||||||
|
|
||||||
function popLastSegment(url = "") {
|
function popLastSegment(url = "") {
|
||||||
@@ -138,13 +151,10 @@ function popLastSegment(url = "") {
|
|||||||
|
|
||||||
function buildFaviconUrl(faviconPath, pathWithoutFile) {
|
function buildFaviconUrl(faviconPath, pathWithoutFile) {
|
||||||
if (faviconPath.startsWith("http")) {
|
if (faviconPath.startsWith("http")) {
|
||||||
console.log("startsWith http, result", faviconPath);
|
|
||||||
return faviconPath;
|
return faviconPath;
|
||||||
} else if (faviconPath.startsWith("/")) {
|
} else if (faviconPath.startsWith("/")) {
|
||||||
console.log("startsWith /, result", pathWithoutFile + faviconPath);
|
|
||||||
return pathWithoutFile + faviconPath;
|
return pathWithoutFile + faviconPath;
|
||||||
} else {
|
} else {
|
||||||
console.log("else, result", pathWithoutFile + "/" + faviconPath);
|
|
||||||
return pathWithoutFile + "/" + faviconPath;
|
return pathWithoutFile + "/" + faviconPath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user