0

Download Attachment

by
Published today

Download a message's file attachment to Windmill object storage and return its S3 reference. Get the attachment id from List Attachments. Requires Mail.Read (covered by the default Mail.ReadWrite).

Script ms_outlook Verified

The script

Submitted by hugo989 Bun
Verified 1 hour ago
1
import * as wmill from "windmill-client"
2
import { S3Object } from "windmill-client"
3

4
/**
5
 * Download Attachment
6
 * Download a message's file attachment to Windmill object storage and return its S3 reference. Get the attachment id from List Attachments. Requires Mail.Read (covered by the default Mail.ReadWrite).
7
 */
8
export async function main(
9
  auth: RT.MsOutlook,
10
  message_id: string,
11
  attachment_id: string,
12
  destination: S3Object,
13
  mailbox: string | undefined
14
) {
15
  const base = `https://graph.microsoft.com/v1.0/${
16
    mailbox != null && mailbox !== ""
17
      ? `users/${encodeURIComponent(mailbox)}`
18
      : "me"
19
  }`
20
  const response = await fetch(
21
    `${base}/messages/${encodeURIComponent(
22
      message_id
23
    )}/attachments/${encodeURIComponent(attachment_id)}/$value`,
24
    { headers: { Authorization: `Bearer ${auth.token}` } }
25
  )
26

27
  if (!response.ok) {
28
    throw new Error(`${response.status} ${await response.text()}`)
29
  }
30

31
  await wmill.writeS3File(
32
    destination,
33
    response.body as ReadableStream<Uint8Array>
34
  )
35
  return destination
36
}
37