1 | import * as wmill from "windmill-client"; |
2 | import createClient, { type Middleware } from "openapi-fetch"; |
3 |
|
4 | type Nextcloud = { |
5 | baseUrl: string, |
6 | password: string, |
7 | username: string |
8 | }; |
9 |
|
10 | export async function main( |
11 | ncResource: Nextcloud, |
12 | userId: string | null = null, |
13 | folderName: string, |
14 | groupName: string, |
15 | quota: number, |
16 | useAppApiAuth: boolean = false, |
17 | ) { |
18 |
|
19 | const client = createClient<paths>({ baseUrl: ncResource.baseUrl }); |
20 | const authMiddleware: Middleware = { |
21 | async onRequest({ request, options }) { |
22 | |
23 | |
24 | request.headers.set("Authorization", `Basic ${btoa(ncResource.username + ':' + ncResource.password)}`); |
25 | if (useAppApiAuth) { |
26 | request.headers.set("AA-VERSION", ncResource.aa_version,); |
27 | request.headers.set("EX-APP-ID", ncResource.app_id,); |
28 | request.headers.set("EX-APP-VERSION", ncResource.app_version,); |
29 | request.headers.set("AUTHORIZATION-APP-API", btoa( |
30 | `${userId || ncResource.username}:${ncResource.password}`, |
31 | )); |
32 | } |
33 | return request; |
34 | }, |
35 | }; |
36 | client.use(authMiddleware); |
37 |
|
38 |
|
39 | try { |
40 | const resp = await client.POST("/index.php/apps/groupfolders/folders", { |
41 | params: { |
42 | header: { |
43 | "OCS-APIRequest": true, |
44 | }, |
45 | query: { |
46 | format: "json", |
47 | }, |
48 | }, |
49 | body: { |
50 | mountpoint: folderName |
51 | }, |
52 | }); |
53 |
|
54 | const tableId = resp.data.ocs.data.id; |
55 |
|
56 | await client.POST("/index.php/apps/groupfolders/folders/{id}/groups", { |
57 | params: { |
58 | header: { |
59 | "OCS-APIRequest": true, |
60 | }, |
61 | query: { |
62 | format: "json", |
63 | }, |
64 | path: { |
65 | id: tableId, |
66 | } |
67 | }, |
68 | body: { |
69 | group: groupName |
70 | }, |
71 | }); |
72 |
|
73 | await client.POST("/index.php/apps/groupfolders/folders/{id}/quota", { |
74 | params: { |
75 | header: { |
76 | "OCS-APIRequest": true, |
77 | }, |
78 | query: { |
79 | format: "json", |
80 | }, |
81 | path: { |
82 | id: tableId, |
83 | } |
84 | }, |
85 | body: { |
86 | quota: quota |
87 | }, |
88 | }); |
89 |
|
90 | } catch (e) { |
91 | console.debug('error', e) |
92 | } |
93 | } |