File: link_folder.go

package info (click to toggle)
golang-github-henrybear327-go-proton-api 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: sh: 55; makefile: 26
file content (117 lines) | stat: -rw-r--r-- 2,573 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package proton

import (
	"context"
	"fmt"
	"strconv"

	"github.com/bradenaw/juniper/xslices"
	"github.com/go-resty/resty/v2"
)

func (c *Client) ListChildren(ctx context.Context, shareID, linkID string, showAll bool) ([]Link, error) {
	var res struct {
		Links []Link
	}

	var links []Link

	for page := 0; ; page++ {
		if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
			return r.
				SetQueryParams(map[string]string{
					"Page":     strconv.Itoa(page),
					"PageSize": strconv.Itoa(maxPageSize),
					"ShowAll":  Bool(showAll).FormatURL(),
				}).
				SetResult(&res).
				Get("/drive/shares/" + shareID + "/folders/" + linkID + "/children")
		}); err != nil {
			return nil, err
		}

		if len(res.Links) == 0 {
			break
		}

		links = append(links, res.Links...)
	}

	return links, nil
}

func (c *Client) TrashChildren(ctx context.Context, shareID, linkID string, childIDs ...string) error {
	var res struct {
		Responses []struct {
			LinkID   string
			Response APIError
		}
	}

	for _, childIDs := range xslices.Chunk(childIDs, maxPageSize) {
		req := struct {
			LinkIDs []string
		}{
			LinkIDs: childIDs,
		}

		if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
			return r.SetBody(req).SetResult(&res).Post("/drive/shares/" + shareID + "/folders/" + linkID + "/trash_multiple")
		}); err != nil {
			return err
		}

		for _, res := range res.Responses {
			if res.Response.Code != SuccessCode {
				return fmt.Errorf("failed to trash child: %w", res.Response)
			}
		}
	}

	return nil
}

func (c *Client) EmptyTrash(ctx context.Context, shareID string) error {
	var res struct {
		APIError
	}

	if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
		return r.SetResult(&res).Delete("/drive/shares/" + shareID + "/trash")
	}); err != nil {
		return err
	}

	return nil
}

func (c *Client) DeleteChildren(ctx context.Context, shareID, linkID string, childIDs ...string) error {
	var res struct {
		Responses []struct {
			LinkID   string
			Response APIError
		}
	}

	for _, childIDs := range xslices.Chunk(childIDs, maxPageSize) {
		req := struct {
			LinkIDs []string
		}{
			LinkIDs: childIDs,
		}

		if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
			return r.SetBody(req).SetResult(&res).Post("/drive/shares/" + shareID + "/folders/" + linkID + "/delete_multiple")
		}); err != nil {
			return err
		}

		for _, res := range res.Responses {
			if res.Response.Code != SuccessCode {
				return fmt.Errorf("failed to delete child: %w", res.Response)
			}
		}
	}

	return nil
}