-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrash.go
More file actions
152 lines (125 loc) · 4.34 KB
/
trash.go
File metadata and controls
152 lines (125 loc) · 4.34 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package disk
import (
"context"
"fmt"
"net/url"
)
// RestoreFromTrash restores a resource from trash to its original location or a new path
func (c *Client) RestoreFromTrash(ctx context.Context, path string, overwrite bool, name string) (*Link, error) {
if path == "" {
return nil, fmt.Errorf("path cannot be empty")
}
query := url.Values{}
query.Set("path", path)
if overwrite {
query.Set("overwrite", "true")
}
if name != "" {
query.Set("name", name)
}
c.Logger.Debug("Restoring resource from trash: %s", path)
resp, err := c.doRequest(ctx, PUT, "trash/resources/restore?"+query.Encode(), nil)
if err != nil {
c.Logger.LogError("restore from trash", err)
return nil, fmt.Errorf("failed to restore from trash: %w", err)
}
defer resp.Body.Close()
// Handle different response codes
if _, err := c.handleResponse(resp, []int{200, 201, 202}); err != nil {
return nil, fmt.Errorf("failed to restore from trash: %w", err)
}
var link Link
if resp.StatusCode == 202 {
// Asynchronous operation - return link with operation info
if err := c.safeDecodeJSON(resp, &link); err != nil {
return nil, fmt.Errorf("failed to decode restore response: %w", err)
}
}
c.Logger.Info("Successfully restored resource from trash: %s", path)
return &link, nil
}
// ListTrashResources lists resources in the trash, optionally filtered by path
func (c *Client) ListTrashResources(ctx context.Context, path string, limit int, offset int) (*TrashResourceList, error) {
query := url.Values{}
if path != "" {
query.Set("path", path)
}
if limit > 0 {
query.Set("limit", fmt.Sprintf("%d", limit))
}
if offset > 0 {
query.Set("offset", fmt.Sprintf("%d", offset))
}
c.Logger.Debug("Listing trash resources with path: %s", path)
resp, err := c.doRequest(ctx, GET, "trash/resources?"+query.Encode(), nil)
if err != nil {
c.Logger.LogError("list trash resources", err)
return nil, fmt.Errorf("failed to list trash resources: %w", err)
}
defer resp.Body.Close()
if _, err := c.handleResponse(resp, []int{200}); err != nil {
return nil, fmt.Errorf("failed to list trash resources: %w", err)
}
var trashList TrashResourceList
if err := c.safeDecodeJSON(resp, &trashList); err != nil {
return nil, fmt.Errorf("failed to decode trash list: %w", err)
}
c.Logger.Info("Successfully listed %d trash resources", len(trashList.Items))
return &trashList, nil
}
// EmptyTrash permanently deletes all resources from trash or a specific path in trash
func (c *Client) EmptyTrash(ctx context.Context, path string, force bool) error {
query := url.Values{}
if path != "" {
query.Set("path", path)
}
if force {
query.Set("force_async", "false")
}
c.Logger.Debug("Emptying trash with path: %s", path)
resp, err := c.doRequest(ctx, DELETE, "trash/resources?"+query.Encode(), nil)
if err != nil {
c.Logger.LogError("empty trash", err)
return fmt.Errorf("failed to empty trash: %w", err)
}
defer resp.Body.Close()
// Handle different response codes
if _, err := c.handleResponse(resp, []int{200, 202, 204}); err != nil {
return fmt.Errorf("failed to empty trash: %w", err)
}
if resp.StatusCode == 202 {
c.Logger.Info("Trash emptying started asynchronously")
} else {
c.Logger.Info("Successfully emptied trash")
}
return nil
}
// GetTrashResourceMetadata retrieves metadata for a specific resource in trash
func (c *Client) GetTrashResourceMetadata(ctx context.Context, path string, fields []string) (*TrashResource, error) {
if path == "" {
return nil, fmt.Errorf("path cannot be empty")
}
query := url.Values{}
query.Set("path", path)
if len(fields) > 0 {
for _, field := range fields {
query.Add("fields", field)
}
}
c.Logger.Debug("Getting trash resource metadata: %s", path)
resp, err := c.doRequest(ctx, GET, "trash/resources?"+query.Encode(), nil)
if err != nil {
c.Logger.LogError("get trash resource metadata", err)
return nil, fmt.Errorf("failed to get trash resource metadata: %w", err)
}
defer resp.Body.Close()
if _, err := c.handleResponse(resp, []int{200}); err != nil {
return nil, fmt.Errorf("failed to get trash resource metadata: %w", err)
}
var trashResource TrashResource
if err := c.safeDecodeJSON(resp, &trashResource); err != nil {
return nil, fmt.Errorf("failed to decode trash resource metadata: %w", err)
}
c.Logger.Info("Successfully retrieved trash resource metadata: %s", path)
return &trashResource, nil
}