[dyad] Added image file size analysis - wrote 3 file(s)
This commit is contained in:
@@ -18,6 +18,7 @@ export interface HeadlineNode {
|
||||
export interface ImageAltData {
|
||||
src: string;
|
||||
alt: string;
|
||||
size: number | null;
|
||||
}
|
||||
|
||||
export async function extractMetaData(url: string, keyword?: string) {
|
||||
@@ -127,7 +128,7 @@ export async function extractMetaData(url: string, keyword?: string) {
|
||||
keywordCount = matches ? matches.length : 0;
|
||||
}
|
||||
|
||||
const imageAltData: ImageAltData[] = [];
|
||||
const imageSrcs: { src: string; alt: string }[] = [];
|
||||
$("img").each((i, el) => {
|
||||
const src = $(el).attr("src");
|
||||
const alt = $(el).attr("alt") || "";
|
||||
@@ -135,7 +136,7 @@ export async function extractMetaData(url: string, keyword?: string) {
|
||||
if (src) {
|
||||
try {
|
||||
const absoluteSrc = new URL(src, formattedUrl).href;
|
||||
imageAltData.push({
|
||||
imageSrcs.push({
|
||||
src: absoluteSrc,
|
||||
alt: alt.trim(),
|
||||
});
|
||||
@@ -145,6 +146,25 @@ export async function extractMetaData(url: string, keyword?: string) {
|
||||
}
|
||||
});
|
||||
|
||||
const imageSizePromises = imageSrcs.map(async (img) => {
|
||||
try {
|
||||
// Use a HEAD request for efficiency
|
||||
const res = await fetch(img.src, { method: "HEAD" });
|
||||
if (res.ok) {
|
||||
const contentLength = res.headers.get("content-length");
|
||||
return {
|
||||
...img,
|
||||
size: contentLength ? parseInt(contentLength, 10) : null,
|
||||
};
|
||||
}
|
||||
return { ...img, size: null };
|
||||
} catch (error) {
|
||||
return { ...img, size: null };
|
||||
}
|
||||
});
|
||||
|
||||
const imageAltData: ImageAltData[] = await Promise.all(imageSizePromises);
|
||||
|
||||
return {
|
||||
data: {
|
||||
title,
|
||||
|
||||
Reference in New Issue
Block a user