import * as wmill from "windmill-client"
import { S3Object } from "windmill-client"
/**
* Upload Ticket Attachments
* Upload files from Windmill object storage to Zendesk and return one upload token per file. Attach them to a ticket by passing the tokens as `upload_tokens` to Update Ticket (or `comment.uploads` in Create Ticket's `fields`). Tokens expire after 60 minutes if unused. Each file keeps the name at the end of its storage path.
*/
export async function main(auth: RT.Zendesk, files: S3Object[]) {
const tokens: string[] = []
for (const file of files) {
const bytes = await wmill.loadS3File(file)
if (!bytes) {
throw new Error(`Could not load ${file.s3} from object storage`)
}
const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/uploads`)
url.searchParams.append("filename", file.s3.split("/").pop()!)
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: auth.token
? `Bearer ${auth.token}`
: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
"Content-Type": "application/binary",
Accept: "application/json",
},
body: bytes,
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { upload } = await response.json()
tokens.push(upload.token)
}
return { tokens }
}
Submitted by hugo989 2 hours ago