|
| 1 | +import { type KeepAlive, withKeepAlive } from "../utils/keepalive"; |
| 2 | + |
1 | 3 | export default defineEventHandler(async (event) => { |
2 | | - event.node.req.setTimeout(1 * 60 * 60 * 1000); |
3 | | - console.log("Getting outlines..."); |
4 | | - const database = await processFileUpload(event, { |
5 | | - maxSize: 256 * 1024 * 1024, |
6 | | - processor: async (fileStream) => { |
7 | | - console.log("Processing file stream..."); |
8 | | - return await getJWPUBDatabase(fileStream); |
9 | | - }, |
10 | | - }); |
| 4 | + return withKeepAlive( |
| 5 | + event, |
| 6 | + async (keepalive: KeepAlive) => { |
| 7 | + keepalive.progress("Starting file upload processing..."); |
11 | 8 |
|
12 | | - const outlines = queryDatabase<{ Title: string }>( |
13 | | - database, |
14 | | - "SELECT Title FROM Document", |
15 | | - ); |
16 | | - const htmlOutlines = await parseJWPUB(database); |
17 | | - const parsedOutlines = outlines |
18 | | - .map((outline) => { |
19 | | - const [number, ...title] = outline.Title.split(". "); |
20 | | - return { |
21 | | - number: parseInt(number?.trim() ?? "0"), |
22 | | - title: title.join(". "), |
23 | | - }; |
24 | | - }) |
25 | | - .filter((outline) => outline.number > 0); |
| 9 | + const database = await processFileUpload(event, { |
| 10 | + maxSize: 256 * 1024 * 1024, |
| 11 | + processor: async (fileStream) => { |
| 12 | + keepalive.progress("Extracting database from JWPUB file..."); |
| 13 | + return await getJWPUBDatabase(fileStream); |
| 14 | + }, |
| 15 | + }); |
| 16 | + |
| 17 | + keepalive.progress("Database extracted, querying documents..."); |
| 18 | + const outlines = queryDatabase<{ Title: string }>( |
| 19 | + database, |
| 20 | + "SELECT Title FROM Document", |
| 21 | + ); |
26 | 22 |
|
27 | | - const match = |
28 | | - htmlOutlines.length === parsedOutlines.length && |
29 | | - htmlOutlines.every( |
30 | | - (htmlOutline, index) => |
31 | | - htmlOutline.number === parsedOutlines[index]?.number && |
32 | | - htmlOutline.title === parsedOutlines[index]?.title, |
33 | | - ); |
| 23 | + keepalive.progress("Parsing JWPUB content..."); |
| 24 | + const htmlOutlines = await parseJWPUB(database); |
34 | 25 |
|
35 | | - return match ? htmlOutlines : parsedOutlines; |
| 26 | + keepalive.progress("Processing outlines..."); |
| 27 | + const parsedOutlines = outlines |
| 28 | + .map((outline) => { |
| 29 | + const [number, ...title] = outline.Title.split(". "); |
| 30 | + return { |
| 31 | + number: parseInt(number?.trim() ?? "0"), |
| 32 | + title: title.join(". "), |
| 33 | + }; |
| 34 | + }) |
| 35 | + .filter((outline) => outline.number > 0); |
| 36 | + |
| 37 | + const match = |
| 38 | + htmlOutlines.length === parsedOutlines.length && |
| 39 | + htmlOutlines.every( |
| 40 | + (htmlOutline, index) => |
| 41 | + htmlOutline.number === parsedOutlines[index]?.number && |
| 42 | + htmlOutline.title === parsedOutlines[index]?.title, |
| 43 | + ); |
| 44 | + |
| 45 | + keepalive.progress("Outlines processed successfully"); |
| 46 | + return match ? htmlOutlines : parsedOutlines; |
| 47 | + }, |
| 48 | + "Processing JWPUB file upload...", |
| 49 | + ); |
36 | 50 | }); |
0 commit comments