-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.go
166 lines (139 loc) · 4.33 KB
/
File.go
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package libdatamanager
import (
"errors"
"io"
"time"
)
// FileAttributes attributes for a file
type FileAttributes struct {
Tags []string `json:"tags,omitempty"`
Groups []string `json:"groups,omitempty"`
Namespace string `json:"ns"`
}
// FileUpdateItem lists changes to a file
type FileUpdateItem struct {
IsPublic string `json:"ispublic,omitempty"`
NewName string `json:"name,omitempty"`
NewNamespace string `json:"namespace,omitempty"`
RemoveTags []string `json:"rem_tags,omitempty"`
RemoveGroups []string `json:"rem_groups,omitempty"`
AddTags []string `json:"add_tags,omitempty"`
AddGroups []string `json:"add_groups,omitempty"`
}
// FileResponseItem file item for file response
type FileResponseItem struct {
ID uint `json:"id"`
Size int64 `json:"size"`
CreationDate time.Time `json:"creation"`
Name string `json:"name"`
IsPublic bool `json:"isPub"`
PublicName string `json:"pubname"`
Attributes FileAttributes `json:"attrib"`
Encryption int8 `json:"e"`
Checksum string `json:"checksum"`
}
// FileChanges file changes for updating a file
type FileChanges struct {
NewName string
NewNamespace string
AddTags, AddGroups []string
RemoveTags, RemoveGroups []string
SetPublic, SetPrivate bool
}
var (
// ErrCancelled if something was cancelled
ErrCancelled = errors.New("cancelled")
)
const (
// DefaultBuffersize The default buffersize for filestreams
DefaultBuffersize = 10 * 1024
)
// WriterProxy proxy writing
type WriterProxy func(io.Writer) io.Writer
// ReaderProxy proxy writing
type ReaderProxy func(io.Reader) io.Reader
// FileSizeCallback gets called if the filesize is known
type FileSizeCallback func(int64)
// DeleteFile deletes the desired file(s)
func (libdm LibDM) DeleteFile(name string, id uint, all bool, attributes FileAttributes) (*IDsResponse, error) {
var response IDsResponse
if _, err := libdm.Request(EPFileDelete, &FileRequest{
Name: name,
FileID: id,
All: all,
Attributes: attributes,
}, &response, true); err != nil {
return nil, err
}
return &response, nil
}
// ListFiles lists the files corresponding to the args
func (libdm LibDM) ListFiles(name string, id uint, allNamespaces bool, attributes FileAttributes, verbose uint8) (*FileListResponse, error) {
var response FileListResponse
if _, err := libdm.Request(EPFileList, &FileListRequest{
FileID: id,
Name: name,
AllNamespaces: allNamespaces,
Attributes: attributes,
OptionalParams: OptionalRequetsParameter{
Verbose: verbose,
},
}, &response, true); err != nil {
return nil, err
}
return &response, nil
}
// PublishFile publishs a file. If "all" is true, the response object is BulkPublishResponse. Else it is PublishResponse
func (libdm LibDM) PublishFile(name string, id uint, publicName string, all bool, attributes FileAttributes) (interface{}, error) {
request := libdm.NewRequest(EPFilePublish, FileRequest{
Name: name,
FileID: id,
PublicName: publicName,
All: all,
Attributes: attributes,
}).WithAuthFromConfig()
var err error
var response *RestRequestResponse
var resp BulkPublishResponse
response, err = request.Do(&resp)
if err != nil || response.Status == ResponseError {
return nil, NewErrorFromResponse(response, err)
}
return resp, nil
}
// UpdateFile updates a file on the server
func (libdm LibDM) UpdateFile(name string, id uint, namespace string, all bool, changes FileChanges) (*IDsResponse, error) {
// Set attributes
attributes := FileAttributes{
Namespace: namespace,
}
var isPublic string
if changes.SetPublic {
isPublic = "true"
}
if changes.SetPrivate {
isPublic = "false"
}
// Set fileUpdates
fileUpdates := FileUpdateItem{
IsPublic: isPublic,
NewName: changes.NewName,
NewNamespace: changes.NewNamespace,
RemoveTags: changes.RemoveTags,
RemoveGroups: changes.RemoveGroups,
AddTags: changes.AddTags,
AddGroups: changes.AddGroups,
}
var response IDsResponse
// Do request
if _, err := libdm.Request(EPFileUpdate, &FileRequest{
Name: name,
FileID: id,
All: all,
Updates: fileUpdates,
Attributes: attributes,
}, &response, true); err != nil {
return nil, err
}
return &response, nil
}