Замена wordpdf-cli на Spring Boot REST API: wordpdf-app, SPA wordpdf-web, docker-compose
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
/** Ошибка API: сервер ответил не 2xx — текст из JSON `{"error": …}`. */
|
||||
export class ApiError extends Error {}
|
||||
|
||||
export interface ConvertResult {
|
||||
blob: Blob
|
||||
filename: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Единственная сетевая точка приложения: POST /api/v1/convert (multipart,
|
||||
* поле `file`) → PDF-blob + имя файла из Content-Disposition.
|
||||
* Таймаут на фронте не ставим: конвертация долгая — это ожидаемо.
|
||||
*/
|
||||
export async function convertDocx(
|
||||
file: File,
|
||||
signal: AbortSignal,
|
||||
): Promise<ConvertResult> {
|
||||
const fd = new FormData()
|
||||
fd.append('file', file, file.name)
|
||||
const resp = await fetch('/api/v1/convert', { method: 'POST', body: fd, signal })
|
||||
if (!resp.ok) {
|
||||
const json = (await resp.json().catch(() => null)) as { error?: string } | null
|
||||
throw new ApiError(json?.error ?? `HTTP ${resp.status}`)
|
||||
}
|
||||
// Framework 7 отдаёт `filename*=UTF-8''…` (charset в верхнем регистре) —
|
||||
// флаг i обязателен; фолбэк — имя исходника с заменой расширения
|
||||
const cd = resp.headers.get('Content-Disposition') ?? ''
|
||||
const m = /filename\*=utf-8''([^;]+)/i.exec(cd)
|
||||
const filename = m ? decodeURIComponent(m[1]) : file.name.replace(/\.docx$/i, '') + '.pdf'
|
||||
const blob = await resp.blob()
|
||||
return { blob, filename }
|
||||
}
|
||||
Reference in New Issue
Block a user