|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('node:fs/promises'); |
| 4 | +const path = require('node:path'); |
| 5 | +const readline = require('node:readline/promises'); |
| 6 | +const { stdin, stderr, stdout } = require('node:process'); |
| 7 | + |
| 8 | +const DEFAULT_BASE_URL = 'https://saas.pdflux.com'; |
| 9 | +const DEFAULT_POLL_INTERVAL_MS = 2000; |
| 10 | +const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000; |
| 11 | + |
| 12 | +function sleep(ms) { |
| 13 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 14 | +} |
| 15 | + |
| 16 | +function normalizeBaseUrl(url) { |
| 17 | + return (url || DEFAULT_BASE_URL).trim().replace(/\/+$/, ''); |
| 18 | +} |
| 19 | + |
| 20 | +async function readAccessToken() { |
| 21 | + const fromEnv = (process.env.PAODINGAI_API_KEY || '').trim(); |
| 22 | + if (fromEnv) { |
| 23 | + return fromEnv; |
| 24 | + } |
| 25 | + |
| 26 | + const rl = readline.createInterface({ |
| 27 | + input: stdin, |
| 28 | + output: stderr, |
| 29 | + }); |
| 30 | + |
| 31 | + try { |
| 32 | + const input = await rl.question('Enter PAODINGAI_API_KEY: '); |
| 33 | + const token = (input || '').trim(); |
| 34 | + if (!token) { |
| 35 | + throw new Error('PAODINGAI_API_KEY is required but not provided.'); |
| 36 | + } |
| 37 | + return token; |
| 38 | + } finally { |
| 39 | + rl.close(); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +async function parseResponse(response) { |
| 44 | + const contentType = response.headers.get('content-type') || ''; |
| 45 | + const bodyText = await response.text(); |
| 46 | + if (contentType.includes('application/json')) { |
| 47 | + try { |
| 48 | + return JSON.parse(bodyText); |
| 49 | + } catch { |
| 50 | + return { status: false, msg: bodyText || 'Invalid JSON response' }; |
| 51 | + } |
| 52 | + } |
| 53 | + return bodyText; |
| 54 | +} |
| 55 | + |
| 56 | +function extractApiError(payload, fallback) { |
| 57 | + if (!payload) { |
| 58 | + return fallback; |
| 59 | + } |
| 60 | + if (typeof payload === 'string') { |
| 61 | + return payload || fallback; |
| 62 | + } |
| 63 | + if (typeof payload === 'object') { |
| 64 | + return payload.msg || payload.message || JSON.stringify(payload); |
| 65 | + } |
| 66 | + return fallback; |
| 67 | +} |
| 68 | + |
| 69 | +async function uploadFile({ baseUrl, token, filePath }) { |
| 70 | + const formData = new FormData(); |
| 71 | + const filename = path.basename(filePath); |
| 72 | + const bytes = await fs.readFile(filePath); |
| 73 | + const fileBlob = new Blob([bytes], { type: 'application/octet-stream' }); |
| 74 | + formData.append('file', fileBlob, filename); |
| 75 | + |
| 76 | + const response = await fetch(`${baseUrl}/api/v1/saas/upload`, { |
| 77 | + method: 'POST', |
| 78 | + headers: { |
| 79 | + Authorization: `Bearer ${token}`, |
| 80 | + }, |
| 81 | + body: formData, |
| 82 | + }); |
| 83 | + |
| 84 | + const payload = await parseResponse(response); |
| 85 | + if (!response.ok) { |
| 86 | + throw new Error( |
| 87 | + `Upload failed (${response.status}): ${extractApiError(payload, 'Request failed')}`, |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + if (typeof payload !== 'object' || payload.status === false) { |
| 92 | + throw new Error( |
| 93 | + `Upload failed: ${extractApiError(payload, 'Invalid upload response')}`, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + const uuid = payload?.data?.uuid; |
| 98 | + if (!uuid) { |
| 99 | + throw new Error( |
| 100 | + `Upload succeeded but uuid is missing: ${JSON.stringify(payload)}`, |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + return uuid; |
| 105 | +} |
| 106 | + |
| 107 | +async function pollParsed({ baseUrl, token, uuid, pollIntervalMs, timeoutMs }) { |
| 108 | + const startTime = Date.now(); |
| 109 | + |
| 110 | + while (Date.now() - startTime < timeoutMs) { |
| 111 | + const response = await fetch(`${baseUrl}/api/v1/saas/document/${uuid}`, { |
| 112 | + method: 'GET', |
| 113 | + headers: { |
| 114 | + Authorization: `Bearer ${token}`, |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + const payload = await parseResponse(response); |
| 119 | + if (!response.ok) { |
| 120 | + throw new Error( |
| 121 | + `Polling failed (${response.status}): ${extractApiError(payload, 'Request failed')}`, |
| 122 | + ); |
| 123 | + } |
| 124 | + if (typeof payload !== 'object' || payload.status === false) { |
| 125 | + throw new Error( |
| 126 | + `Polling failed: ${extractApiError(payload, 'Invalid status response')}`, |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + const parsed = payload?.data?.parsed; |
| 131 | + if (parsed === 2) { |
| 132 | + return; |
| 133 | + } |
| 134 | + if (typeof parsed === 'number' && parsed < 0) { |
| 135 | + throw new Error( |
| 136 | + `Parsing failed with status ${parsed}: ${extractApiError(payload, 'Parse failed')}`, |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + await sleep(pollIntervalMs); |
| 141 | + } |
| 142 | + |
| 143 | + throw new Error( |
| 144 | + `Polling timed out after ${Math.floor(timeoutMs / 1000)} seconds.`, |
| 145 | + ); |
| 146 | +} |
| 147 | + |
| 148 | +async function downloadMarkdown({ baseUrl, token, uuid }) { |
| 149 | + const response = await fetch( |
| 150 | + `${baseUrl}/api/v1/saas/document/${uuid}/markdown`, |
| 151 | + { |
| 152 | + method: 'GET', |
| 153 | + headers: { |
| 154 | + Authorization: `Bearer ${token}`, |
| 155 | + }, |
| 156 | + }, |
| 157 | + ); |
| 158 | + |
| 159 | + const contentType = response.headers.get('content-type') || ''; |
| 160 | + const bodyText = await response.text(); |
| 161 | + |
| 162 | + if (!response.ok) { |
| 163 | + let errorMessage = bodyText; |
| 164 | + if (contentType.includes('application/json')) { |
| 165 | + try { |
| 166 | + const payload = JSON.parse(bodyText); |
| 167 | + errorMessage = extractApiError(payload, bodyText); |
| 168 | + } catch { |
| 169 | + // Keep bodyText |
| 170 | + } |
| 171 | + } |
| 172 | + throw new Error( |
| 173 | + `Markdown download failed (${response.status}): ${errorMessage || 'Request failed'}`, |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + if (contentType.includes('application/json')) { |
| 178 | + try { |
| 179 | + const payload = JSON.parse(bodyText); |
| 180 | + if (payload?.status === false) { |
| 181 | + throw new Error( |
| 182 | + `Markdown download failed: ${extractApiError(payload, 'API returned error')}`, |
| 183 | + ); |
| 184 | + } |
| 185 | + } catch (error) { |
| 186 | + if (error instanceof Error) { |
| 187 | + throw error; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return bodyText; |
| 193 | +} |
| 194 | + |
| 195 | +async function ensureInputFile(filePathArg) { |
| 196 | + if (!filePathArg) { |
| 197 | + throw new Error('Usage: node upload_to_markdown.js <local-file-path>'); |
| 198 | + } |
| 199 | + |
| 200 | + const resolvedPath = path.resolve(filePathArg); |
| 201 | + let stat; |
| 202 | + try { |
| 203 | + stat = await fs.stat(resolvedPath); |
| 204 | + } catch { |
| 205 | + throw new Error(`Input file does not exist: ${resolvedPath}`); |
| 206 | + } |
| 207 | + |
| 208 | + if (!stat.isFile()) { |
| 209 | + throw new Error(`Input path is not a file: ${resolvedPath}`); |
| 210 | + } |
| 211 | + |
| 212 | + return resolvedPath; |
| 213 | +} |
| 214 | + |
| 215 | +async function main() { |
| 216 | + const filePath = await ensureInputFile(process.argv[2]); |
| 217 | + const token = await readAccessToken(); |
| 218 | + const baseUrl = normalizeBaseUrl(process.env.PAODINGAI_API_BASE_URL); |
| 219 | + |
| 220 | + const pollIntervalMs = DEFAULT_POLL_INTERVAL_MS; |
| 221 | + const timeoutMs = DEFAULT_TIMEOUT_MS; |
| 222 | + |
| 223 | + stderr.write(`[pdflux-saas-markdown] Uploading ${path.basename(filePath)}\n`); |
| 224 | + const uuid = await uploadFile({ baseUrl, token, filePath }); |
| 225 | + |
| 226 | + stderr.write(`[pdflux-saas-markdown] Uploaded uuid=${uuid}\n`); |
| 227 | + stderr.write('[pdflux-saas-markdown] Polling parse status\n'); |
| 228 | + await pollParsed({ baseUrl, token, uuid, pollIntervalMs, timeoutMs }); |
| 229 | + |
| 230 | + stderr.write( |
| 231 | + '[pdflux-saas-markdown] Parse completed, downloading markdown\n', |
| 232 | + ); |
| 233 | + const markdown = await downloadMarkdown({ baseUrl, token, uuid }); |
| 234 | + |
| 235 | + stdout.write(markdown); |
| 236 | + if (!markdown.endsWith('\n')) { |
| 237 | + stdout.write('\n'); |
| 238 | + } |
| 239 | +} |
| 240 | + |
| 241 | +main().catch(error => { |
| 242 | + stderr.write( |
| 243 | + `[pdflux-saas-markdown] ${error instanceof Error ? error.message : String(error)}\n`, |
| 244 | + ); |
| 245 | + process.exitCode = 1; |
| 246 | +}); |
0 commit comments