mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-10 07:25:35 +00:00
refactor: apply prettier conf and cleanup
This commit is contained in:
@@ -1,32 +1,32 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { createReadStream } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import {
|
||||
downloadImageFromUrl,
|
||||
getFavicon,
|
||||
makeRequestWithUserAgent,
|
||||
} from "lib/request";
|
||||
import { Favicon } from "types/types";
|
||||
import { buildFaviconUrl, findFaviconPath } from "lib/url";
|
||||
import { convertBase64ToBuffer, isBase64Image, isImage } from "lib/image";
|
||||
} from 'lib/request';
|
||||
import { Favicon } from 'types/types';
|
||||
import { buildFaviconUrl, findFaviconPath } from 'lib/url';
|
||||
import { convertBase64ToBuffer, isBase64Image, isImage } from 'lib/image';
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse,
|
||||
) {
|
||||
const urlParam = (req.query?.urlParam as string) || "";
|
||||
const urlParam = (req.query?.urlParam as string) || '';
|
||||
if (!urlParam) {
|
||||
throw new Error("URL is missing");
|
||||
throw new Error('URL is missing');
|
||||
}
|
||||
|
||||
const faviconRequestUrl = buildFaviconUrl(urlParam, "/favicon.ico");
|
||||
const faviconRequestUrl = buildFaviconUrl(urlParam, '/favicon.ico');
|
||||
try {
|
||||
return sendImage(res, await getFavicon(faviconRequestUrl));
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"[Favicon]",
|
||||
'[Favicon]',
|
||||
`[first: ${faviconRequestUrl}]`,
|
||||
"Unable to retrieve favicon from favicon.ico url",
|
||||
'Unable to retrieve favicon from favicon.ico url',
|
||||
);
|
||||
}
|
||||
|
||||
@@ -36,9 +36,9 @@ export default async function handler(
|
||||
const faviconPath = findFaviconPath(documentAsText);
|
||||
if (!faviconPath) {
|
||||
console.error(
|
||||
"[Favicon]",
|
||||
'[Favicon]',
|
||||
`[first: ${faviconRequestUrl}]`,
|
||||
"No link/href attribute found",
|
||||
'No link/href attribute found',
|
||||
);
|
||||
return sendDefaultImage(res);
|
||||
}
|
||||
@@ -46,50 +46,50 @@ export default async function handler(
|
||||
const finalUrl = buildFaviconUrl(requestDocument.url, faviconPath);
|
||||
try {
|
||||
if (!faviconPath) {
|
||||
throw new Error("Unable to find favicon path");
|
||||
throw new Error('Unable to find favicon path');
|
||||
}
|
||||
|
||||
if (isBase64Image(faviconPath)) {
|
||||
console.log(
|
||||
"[Favicon]",
|
||||
'[Favicon]',
|
||||
`[second: ${faviconRequestUrl}]`,
|
||||
"info: base64, convert it to buffer",
|
||||
'info: base64, convert it to buffer',
|
||||
);
|
||||
const buffer = convertBase64ToBuffer(faviconPath);
|
||||
return sendImage(res, {
|
||||
buffer,
|
||||
type: "image/x-icon",
|
||||
type: 'image/x-icon',
|
||||
size: buffer.length,
|
||||
url: faviconPath,
|
||||
});
|
||||
}
|
||||
|
||||
const finalUrl = faviconPath.startsWith("http")
|
||||
const finalUrl = faviconPath.startsWith('http')
|
||||
? faviconPath
|
||||
: buildFaviconUrl(requestDocument.url, faviconPath);
|
||||
const favicon = await downloadImageFromUrl(finalUrl);
|
||||
if (!isImage(favicon.type)) {
|
||||
throw new Error("Favicon path does not return an image");
|
||||
throw new Error('Favicon path does not return an image');
|
||||
}
|
||||
|
||||
console.log("[Favicon]", `[second: ${finalUrl}]`, "success: image found");
|
||||
console.log('[Favicon]', `[second: ${finalUrl}]`, 'success: image found');
|
||||
return sendImage(res, favicon);
|
||||
} catch (error) {
|
||||
const errorMessage = error?.message || "Unable to retrieve favicon";
|
||||
console.log("[Favicon]", `[second: ${finalUrl}], error:`, errorMessage);
|
||||
const errorMessage = error?.message || 'Unable to retrieve favicon';
|
||||
console.log('[Favicon]', `[second: ${finalUrl}], error:`, errorMessage);
|
||||
return sendDefaultImage(res);
|
||||
}
|
||||
}
|
||||
|
||||
function sendImage(res: NextApiResponse, { buffer, type, size }: Favicon) {
|
||||
res.setHeader("Content-Type", type);
|
||||
res.setHeader("Content-Length", size);
|
||||
res.setHeader('Content-Type', type);
|
||||
res.setHeader('Content-Length', size);
|
||||
res.send(buffer);
|
||||
}
|
||||
|
||||
function sendDefaultImage(res: NextApiResponse) {
|
||||
const readStream = createReadStream(
|
||||
resolve(process.cwd(), "./public/empty-image.png"),
|
||||
resolve(process.cwd(), './public/empty-image.png'),
|
||||
);
|
||||
res.writeHead(206);
|
||||
readStream.pipe(res);
|
||||
|
||||
Reference in New Issue
Block a user