-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathspecialtyHandler.ts
53 lines (48 loc) · 1.54 KB
/
specialtyHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Logger } from "winston";
import { AddFeatureError } from "../../error";
import { FireEngineCheckStatusSuccess } from "../fire-engine/checkStatus";
import path from "path";
import os from "os";
import { writeFile } from "fs/promises";
import { Meta } from "../..";
async function feResToPdfPrefetch(feRes: FireEngineCheckStatusSuccess | undefined): Promise<Meta["pdfPrefetch"]> {
if (!feRes?.file) {
return null;
}
const filePath = path.join(os.tmpdir(), `tempFile-${crypto.randomUUID()}.pdf`);
await writeFile(filePath, Buffer.from(feRes.file.content, "base64"))
return {
status: feRes.pageStatusCode,
url: feRes.url,
filePath,
};
}
export async function specialtyScrapeCheck(
logger: Logger,
headers: Record<string, string> | undefined,
feRes?: FireEngineCheckStatusSuccess,
) {
const contentType = (Object.entries(headers ?? {}).find(
(x) => x[0].toLowerCase() === "content-type",
) ?? [])[1];
if (contentType === undefined) {
logger.warn("Failed to check contentType -- was not present in headers", {
headers,
});
} else if (
contentType === "application/pdf" ||
contentType.startsWith("application/pdf;")
) {
// .pdf
throw new AddFeatureError(["pdf"], await feResToPdfPrefetch(feRes));
} else if (
contentType ===
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
contentType.startsWith(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document;",
)
) {
// .docx
throw new AddFeatureError(["docx"]);
}
}