-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathconvertToMarkdown.ts
More file actions
53 lines (46 loc) · 1.6 KB
/
convertToMarkdown.ts
File metadata and controls
53 lines (46 loc) · 1.6 KB
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 { task } from "@trigger.dev/sdk/v3";
import { python } from "@trigger.dev/python";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
export const convertToMarkdown = task({
id: "convert-to-markdown",
run: async (payload: { url: string }) => {
const { url } = payload;
// STEP 1: Create temporary file with unique name
const tempDir = os.tmpdir();
const fileName = `doc-${Date.now()}-${
Math.random().toString(36).substring(2, 7)
}`;
const urlPath = new URL(url).pathname;
const extension = path.extname(urlPath) || ".docx";
const tempFilePath = path.join(tempDir, `${fileName}${extension}`);
// STEP 2: Download file from URL
const response = await fetch(url);
const buffer = await response.arrayBuffer();
await fs.promises.writeFile(tempFilePath, Buffer.from(buffer));
// STEP 3: Run Python script to convert document to markdown
const pythonResult = await python.runScript(
"./src/python/markdown-converter.py",
[JSON.stringify({ file_path: tempFilePath })],
);
// STEP 4: Clean up temporary file
fs.unlink(tempFilePath, () => {});
// STEP 5: Process result
if (pythonResult.stdout) {
const result = JSON.parse(pythonResult.stdout);
return {
url,
markdown: result.status === "success" ? result.markdown : null,
error: result.status === "error" ? result.error : null,
success: result.status === "success",
};
}
return {
url,
markdown: null,
error: "No output from Python script",
success: false,
};
},
});