mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +00:00
feat(transcode): add Gotenberg support for Office, HTML, CSV, and Markdown PDF proxies (#283)
* feat(transcode): add Gotenberg support for Office, HTML, CSV, and Markdown PDF proxies * fix(transcode): use Gotenberg chromium markdown endpoint with index.html wrapper for markdown conversion * test(transcode): add complete unit test coverage for HTML, CSV landscape/portrait, TXT, and Gotenberg fallback branches * fix(gotenberg): force .md extension for Gotenberg markdown conversion endpoint compatibility * refactor(gotenberg): standardize basic auth environment variables on GOTENBERG_BASIC_AUTH_USERNAME and GOTENBERG_BASIC_AUTH_PASSWORD * test(e2e): add real Gotenberg testcontainers workflow E2E test for docx, pptx, and xlsx * docs: add Gotenberg document PDF conversion documentation and update features in README * docs(gotenberg): add Docker quickstart commands and remove obsolete GOTENBERG_USERNAME note
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import * as os from 'os'
|
||||
import { GotenbergService } from './gotenberg'
|
||||
|
||||
describe('GotenbergService', () => {
|
||||
const originalEnv = process.env
|
||||
let tmpDir: string
|
||||
let testFilePath: string
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = { ...originalEnv }
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gotenberg-test-'))
|
||||
testFilePath = path.join(tmpDir, 'test.docx')
|
||||
fs.writeFileSync(testFilePath, 'fake docx content')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
process.env = originalEnv
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true })
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('isAvailable', () => {
|
||||
it('should return false if GOTENBERG_URL is not set', async () => {
|
||||
delete process.env.GOTENBERG_URL
|
||||
const service = new GotenbergService()
|
||||
expect(await service.isAvailable()).toBe(false)
|
||||
})
|
||||
|
||||
it('should return false if GOTENBERG_URL is empty', async () => {
|
||||
process.env.GOTENBERG_URL = ' '
|
||||
const service = new GotenbergService()
|
||||
expect(await service.isAvailable()).toBe(false)
|
||||
})
|
||||
|
||||
it('should return true when health check returns 200 OK', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
} as Response)
|
||||
|
||||
const service = new GotenbergService()
|
||||
const available = await service.isAvailable()
|
||||
|
||||
expect(available).toBe(true)
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
'http://gotenberg:3000/health',
|
||||
expect.objectContaining({
|
||||
method: 'GET',
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should include Basic Auth header if username and password are provided', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
process.env.GOTENBERG_BASIC_AUTH_USERNAME = 'user'
|
||||
process.env.GOTENBERG_BASIC_AUTH_PASSWORD = 'pass'
|
||||
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
} as Response)
|
||||
|
||||
const service = new GotenbergService()
|
||||
await service.isAvailable()
|
||||
|
||||
const expectedBasic = Buffer.from('user:pass').toString('base64')
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
'http://gotenberg:3000/health',
|
||||
expect.objectContaining({
|
||||
headers: {
|
||||
Authorization: `Basic ${expectedBasic}`,
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should return false when fetch fails', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('Network error'))
|
||||
|
||||
const service = new GotenbergService()
|
||||
expect(await service.isAvailable()).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('convertDocumentToPdf', () => {
|
||||
it('should throw error if GOTENBERG_URL is not set', async () => {
|
||||
delete process.env.GOTENBERG_URL
|
||||
const service = new GotenbergService()
|
||||
await expect(service.convertDocumentToPdf(testFilePath, 'test.docx')).rejects.toThrow(
|
||||
'Gotenberg URL is not configured',
|
||||
)
|
||||
})
|
||||
|
||||
it('should call libreoffice convert endpoint and return buffer', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000/'
|
||||
const mockPdfBuffer = Buffer.from('%PDF-1.7 mock output')
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
arrayBuffer: async () => mockPdfBuffer.buffer,
|
||||
} as unknown as Response)
|
||||
|
||||
const service = new GotenbergService()
|
||||
const result = await service.convertDocumentToPdf(testFilePath, 'test.docx', {
|
||||
landscape: true,
|
||||
})
|
||||
|
||||
expect(result).toBeDefined()
|
||||
expect(result.toString()).toContain('%PDF-1.7')
|
||||
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
'http://gotenberg:3000/forms/libreoffice/convert',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: expect.any(FormData),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw error if gotenberg returns non-200', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
statusText: 'Internal Server Error',
|
||||
text: async () => 'Conversion failed',
|
||||
} as unknown as Response)
|
||||
|
||||
const service = new GotenbergService()
|
||||
await expect(service.convertDocumentToPdf(testFilePath, 'test.docx')).rejects.toThrow(
|
||||
'Gotenberg conversion failed with status 500: Conversion failed',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('convertHtmlToPdf', () => {
|
||||
it('should call chromium html convert endpoint and return buffer', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
const mockPdfBuffer = Buffer.from('%PDF-1.4 html output')
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
arrayBuffer: async () => mockPdfBuffer.buffer,
|
||||
} as unknown as Response)
|
||||
|
||||
const htmlPath = path.join(tmpDir, 'test.html')
|
||||
fs.writeFileSync(htmlPath, '<h1>Hello</h1>')
|
||||
|
||||
const service = new GotenbergService()
|
||||
const result = await service.convertHtmlToPdf(htmlPath, 'test.html')
|
||||
|
||||
expect(result).toBeDefined()
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
'http://gotenberg:3000/forms/chromium/convert/html',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('convertMarkdownToPdf', () => {
|
||||
it('should call chromium markdown convert endpoint with index.html wrapper and return buffer', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
const mockPdfBuffer = Buffer.from('%PDF-1.4 markdown output')
|
||||
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
arrayBuffer: async () => mockPdfBuffer.buffer,
|
||||
} as unknown as Response)
|
||||
|
||||
const mdPath = path.join(tmpDir, 'test.md')
|
||||
fs.writeFileSync(mdPath, '# Hello Gotenberg')
|
||||
|
||||
const service = new GotenbergService()
|
||||
const result = await service.convertMarkdownToPdf(mdPath, 'test.md')
|
||||
|
||||
expect(result).toBeDefined()
|
||||
expect(fetchSpy).toHaveBeenCalledWith(
|
||||
'http://gotenberg:3000/forms/chromium/convert/markdown',
|
||||
expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: expect.any(FormData),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('should rename non-.md filenames like README.markdown to file.md for Gotenberg endpoint compatibility', async () => {
|
||||
process.env.GOTENBERG_URL = 'http://gotenberg:3000'
|
||||
const mockPdfBuffer = Buffer.from('%PDF-1.4 markdown output')
|
||||
let capturedFormData: FormData | null = null
|
||||
|
||||
vi.spyOn(globalThis, 'fetch').mockImplementation(async (_url, options) => {
|
||||
capturedFormData = options?.body as FormData
|
||||
return {
|
||||
ok: true,
|
||||
status: 200,
|
||||
arrayBuffer: async () => mockPdfBuffer.buffer,
|
||||
} as unknown as Response
|
||||
})
|
||||
|
||||
const mdPath = path.join(tmpDir, 'README.markdown')
|
||||
fs.writeFileSync(mdPath, '# Hello Gotenberg')
|
||||
|
||||
const service = new GotenbergService()
|
||||
await service.convertMarkdownToPdf(mdPath, 'README.markdown')
|
||||
|
||||
expect(capturedFormData).not.toBeNull()
|
||||
const indexBlob = (capturedFormData as unknown as FormData).get('files') as File
|
||||
const indexHtmlText = await indexBlob.text()
|
||||
expect(indexHtmlText).toContain('{{ toHTML "file.md" }}')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,178 @@
|
||||
import * as fs from 'fs'
|
||||
|
||||
export interface GotenbergConvertOptions {
|
||||
landscape?: boolean
|
||||
}
|
||||
|
||||
export class GotenbergService {
|
||||
private getUrl(): string | undefined {
|
||||
const url = process.env.GOTENBERG_URL?.trim()
|
||||
return url ? url.replace(/\/+$/, '') : undefined
|
||||
}
|
||||
|
||||
private getAuthHeader(): string | undefined {
|
||||
const username = process.env.GOTENBERG_BASIC_AUTH_USERNAME
|
||||
const password = process.env.GOTENBERG_BASIC_AUTH_PASSWORD
|
||||
|
||||
if (username && password) {
|
||||
const credentials = Buffer.from(`${username}:${password}`).toString('base64')
|
||||
return `Basic ${credentials}`
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
async isAvailable(): Promise<boolean> {
|
||||
const baseUrl = this.getUrl()
|
||||
if (!baseUrl) {
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
const headers: Record<string, string> = {}
|
||||
const authHeader = this.getAuthHeader()
|
||||
if (authHeader) {
|
||||
headers['Authorization'] = authHeader
|
||||
}
|
||||
|
||||
const response = await fetch(`${baseUrl}/health`, {
|
||||
method: 'GET',
|
||||
headers,
|
||||
signal: AbortSignal.timeout(3000),
|
||||
})
|
||||
|
||||
return response.ok
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async convertDocumentToPdf(
|
||||
filePath: string,
|
||||
filename: string,
|
||||
options: GotenbergConvertOptions = {},
|
||||
): Promise<Buffer> {
|
||||
const baseUrl = this.getUrl()
|
||||
if (!baseUrl) {
|
||||
throw new Error('Gotenberg URL is not configured.')
|
||||
}
|
||||
|
||||
const fileBytes = fs.readFileSync(filePath)
|
||||
const formData = new FormData()
|
||||
formData.append('files', new Blob([fileBytes]), filename)
|
||||
|
||||
if (options.landscape) {
|
||||
formData.append('landscape', 'true')
|
||||
}
|
||||
|
||||
const headers: Record<string, string> = {}
|
||||
const authHeader = this.getAuthHeader()
|
||||
if (authHeader) {
|
||||
headers['Authorization'] = authHeader
|
||||
}
|
||||
|
||||
const response = await fetch(`${baseUrl}/forms/libreoffice/convert`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: formData,
|
||||
signal: AbortSignal.timeout(60000),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text().catch(() => '')
|
||||
throw new Error(
|
||||
`Gotenberg conversion failed with status ${response.status}: ${errorText || response.statusText}`,
|
||||
)
|
||||
}
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer()
|
||||
return Buffer.from(arrayBuffer)
|
||||
}
|
||||
|
||||
async convertHtmlToPdf(filePath: string, filename: string): Promise<Buffer> {
|
||||
const baseUrl = this.getUrl()
|
||||
if (!baseUrl) {
|
||||
throw new Error('Gotenberg URL is not configured.')
|
||||
}
|
||||
|
||||
const fileBytes = fs.readFileSync(filePath)
|
||||
const formData = new FormData()
|
||||
const targetFilename =
|
||||
filename.toLowerCase().endsWith('.html') || filename.toLowerCase().endsWith('.htm')
|
||||
? filename
|
||||
: 'index.html'
|
||||
formData.append('files', new Blob([fileBytes]), targetFilename)
|
||||
|
||||
const headers: Record<string, string> = {}
|
||||
const authHeader = this.getAuthHeader()
|
||||
if (authHeader) {
|
||||
headers['Authorization'] = authHeader
|
||||
}
|
||||
|
||||
const response = await fetch(`${baseUrl}/forms/chromium/convert/html`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: formData,
|
||||
signal: AbortSignal.timeout(60000),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text().catch(() => '')
|
||||
throw new Error(
|
||||
`Gotenberg HTML conversion failed with status ${response.status}: ${errorText || response.statusText}`,
|
||||
)
|
||||
}
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer()
|
||||
return Buffer.from(arrayBuffer)
|
||||
}
|
||||
|
||||
async convertMarkdownToPdf(filePath: string, filename: string): Promise<Buffer> {
|
||||
const baseUrl = this.getUrl()
|
||||
if (!baseUrl) {
|
||||
throw new Error('Gotenberg URL is not configured.')
|
||||
}
|
||||
|
||||
const mdFilename = filename.toLowerCase().endsWith('.md') ? filename : 'file.md'
|
||||
|
||||
const htmlTemplate = `<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>PDF</title>
|
||||
</head>
|
||||
<body>
|
||||
{{ toHTML "${mdFilename}" }}
|
||||
</body>
|
||||
</html>`
|
||||
|
||||
const fileBytes = fs.readFileSync(filePath)
|
||||
const formData = new FormData()
|
||||
formData.append('files', new Blob([htmlTemplate], { type: 'text/html' }), 'index.html')
|
||||
formData.append('files', new Blob([fileBytes]), mdFilename)
|
||||
|
||||
const headers: Record<string, string> = {}
|
||||
const authHeader = this.getAuthHeader()
|
||||
if (authHeader) {
|
||||
headers['Authorization'] = authHeader
|
||||
}
|
||||
|
||||
const response = await fetch(`${baseUrl}/forms/chromium/convert/markdown`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: formData,
|
||||
signal: AbortSignal.timeout(60000),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text().catch(() => '')
|
||||
throw new Error(
|
||||
`Gotenberg Markdown conversion failed with status ${response.status}: ${errorText || response.statusText}`,
|
||||
)
|
||||
}
|
||||
|
||||
const arrayBuffer = await response.arrayBuffer()
|
||||
return Buffer.from(arrayBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
export const gotenbergService = new GotenbergService()
|
||||
Reference in New Issue
Block a user