0
Create a group folder in Nextcloud
One script reply has been approved by the moderators Verified

Create a group folder in a Nextcloud. Needs the Group folders app installed. This is only for creating, not changing, it will create a new folder with every use

Created by nextcloud 10 days ago Viewed 378 times
0
Submitted by nextcloud Bun
Verified 10 days ago
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
      // fetch token, if it doesn’t exist
23
      // add Authorization header to every request
24
      request.headers.set("Authorization", `Basic ${btoa((userId || ncResource.username) + ':' + ncResource.password)}`);
25
      if (useAppApiAuth) {
26
        request.headers.set("AA-VERSION", "2.3.0",);
27
        request.headers.set("EX-APP-ID", "flow",);
28
        request.headers.set("EX-APP-VERSION", "1.0.1",);
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
}