import * as wmill from "windmill-client"
import { S3Object } from "windmill-client"
const FORMATS = {
pdf: "application/pdf",
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
odt: "application/vnd.oasis.opendocument.text",
rtf: "application/rtf",
txt: "text/plain",
md: "text/markdown",
html_zip: "application/zip",
epub: "application/epub+zip",
}
/**
* Export Document
* Export a Google Doc (all tabs) as PDF, Word (docx), OpenDocument, RTF, plain text, Markdown, zipped HTML or EPUB, save it to Windmill object storage and return its S3 reference. Without `destination`, a key is generated. Drive caps exports at 10 MB. Requires drive.readonly or drive on the connection (the documents scope can't download files).
*/
export async function main(
auth: RT.Gdocs,
document_id: string,
format: "pdf" | "docx" | "odt" | "rtf" | "txt" | "md" | "html_zip" | "epub" = "pdf",
destination: S3Object | undefined
) {
const url = new URL(
`https://www.googleapis.com/drive/v3/files/${document_id}/export`
)
url.searchParams.append("mimeType", FORMATS[format])
const response = await fetch(url, {
headers: { Authorization: `Bearer ${auth.token}` },
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await wmill.writeS3File(
destination || undefined,
await response.blob(),
undefined,
FORMATS[format]
)
}
Submitted by hugo989 6 hours ago