1 | |
2 | type Confluence = { |
3 | email: string |
4 | apiToken: string |
5 | domain: string |
6 | } |
7 | type Base64 = string |
8 | |
9 | * Create attachment |
10 | * Adds an attachment to a piece of content. |
11 | */ |
12 | export async function main( |
13 | auth: Confluence, |
14 | id: string, |
15 | status: 'current' | 'draft' | undefined, |
16 | body: { |
17 | file: { |
18 | base64: Base64 |
19 | type: |
20 | | 'image/png' |
21 | | 'image/jpeg' |
22 | | 'image/gif' |
23 | | 'application/pdf' |
24 | | 'appication/json' |
25 | | 'text/csv' |
26 | | 'text/plain' |
27 | | 'audio/mpeg' |
28 | | 'audio/wav' |
29 | | 'video/mp4' |
30 | name: string |
31 | } |
32 | comment?: { |
33 | base64: Base64 |
34 | type: |
35 | | 'image/png' |
36 | | 'image/jpeg' |
37 | | 'image/gif' |
38 | | 'application/pdf' |
39 | | 'appication/json' |
40 | | 'text/csv' |
41 | | 'text/plain' |
42 | | 'audio/mpeg' |
43 | | 'audio/wav' |
44 | | 'video/mp4' |
45 | name: string |
46 | } |
47 | minorEdit: { |
48 | base64: Base64 |
49 | type: |
50 | | 'image/png' |
51 | | 'image/jpeg' |
52 | | 'image/gif' |
53 | | 'application/pdf' |
54 | | 'appication/json' |
55 | | 'text/csv' |
56 | | 'text/plain' |
57 | | 'audio/mpeg' |
58 | | 'audio/wav' |
59 | | 'video/mp4' |
60 | name: string |
61 | } |
62 | } |
63 | ) { |
64 | const url = new URL(`https://${auth.domain}/wiki/rest/api/content/${id}/child/attachment`) |
65 | for (const [k, v] of [['status', status]]) { |
66 | if (v !== undefined && v !== '' && k !== undefined) { |
67 | url.searchParams.append(k, v) |
68 | } |
69 | } |
70 | const formData = new FormData() |
71 | for (const [k, v] of Object.entries(body)) { |
72 | if (v !== undefined) { |
73 | if (['file', 'comment', 'minorEdit'].includes(k)) { |
74 | const { base64, type, name } = v as { |
75 | base64: Base64 |
76 | type: string |
77 | name: string |
78 | } |
79 | formData.append( |
80 | k, |
81 | new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], { |
82 | type |
83 | }), |
84 | name |
85 | ) |
86 | } else { |
87 | formData.append(k, String(v)) |
88 | } |
89 | } |
90 | } |
91 | const response = await fetch(url, { |
92 | method: 'POST', |
93 | headers: { |
94 | Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`) |
95 | }, |
96 | body: formData |
97 | }) |
98 | if (!response.ok) { |
99 | const text = await response.text() |
100 | throw new Error(`${response.status} ${text}`) |
101 | } |
102 | return await response.json() |
103 | } |
104 |
|