Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle .pdf links that are html #1002

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion apps/api/src/scraper/scrapeURL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ export type Meta = {
featureFlags: Set<FeatureFlag>;
};

async function isPdfContent(url: string): Promise<boolean> {
try {
const response = await fetch(url, { method: "HEAD" });
const contentType = response.headers.get("Content-Type");
return contentType?.includes("application/pdf") ?? false;
} catch (error) {
logger.warn("Failed to verify Content-Type for URL: " + url, { error });
return false;
}
}

async function verifyFeatureFlags(meta: Meta): Promise<void> {
if (meta.featureFlags.has("pdf")) {
const isPdf = await isPdfContent(meta.url);
if (!isPdf) {
meta.featureFlags.delete("pdf");
}
}
}

function buildFeatureFlags(
url: string,
options: ScrapeOptions,
Expand Down Expand Up @@ -100,7 +120,7 @@ function buildFeatureFlags(
if (urlO.pathname.endsWith(".docx")) {
flags.add("docx");
}

return flags;
}

Expand Down Expand Up @@ -358,6 +378,9 @@ export async function scrapeURL(
internalOptions: InternalOptions = {},
): Promise<ScrapeUrlResponse> {
const meta = buildMetaObject(id, url, options, internalOptions);

await verifyFeatureFlags(meta);

try {
while (true) {
try {
Expand Down