forked from ggordan/go-onedrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
240 lines (205 loc) · 7.38 KB
/
item.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package onedrive
import (
"context"
"fmt"
"net/http"
"time"
)
// ItemService manages the communication with Item related API endpoints
type ItemService struct {
*OneDrive
}
// The Thumbnail resource type represents a thumbnail for an image, video,
// document, or any file or folder on OneDrive that has a graphical representation.
// See: http://onedrive.github.io/resources/thumbnail.htm
type Thumbnail struct {
Width int `json:"width"`
Height int `json:"height"`
URL string `json:"url"`
// Relationships
Content []byte `json:"content"`
}
// The ThumbnailSet type is a keyed collection of Thumbnail objects. It is used
// to represent a set of thumbnails associated with a single file on OneDrive.
// See: http://onedrive.github.io/resources/thumbnailSet.htm
type ThumbnailSet struct {
ID string `json:"id"`
Small *Thumbnail `json:"small"`
Medium *Thumbnail `json:"medium"`
Large *Thumbnail `json:"large"`
}
// Items represents a collection of Items
type Items struct {
Collection []*Item `json:"value"`
}
// The ItemReference type groups data needed to reference a OneDrive item across
// the service into a single structure.
// See: http://onedrive.github.io/resources/itemReference.htm
type ItemReference struct {
DriveID string `json:"driveId"`
ID string `json:"id"`
Path string `json:"path"`
}
// The Item resource type represents metadata for an item in OneDrive. All
// top-level filesystem objects in OneDrive are Item resources. If an item is
// a Folder or File facet, the Item resource will contain a value for either
// the folder or file property, respectively.
// See: http://onedrive.github.io/resources/item.htm
type Item struct {
ID string `json:"id"`
Name string `json:"name"`
ETag string `json:"eTag"`
CTag string `json:"cTag"`
CreatedBy *IdentitySet `json:"createdBy"`
LastModifiedBy *IdentitySet `json:"lastModifiedBy"`
CreatedDateTime time.Time `json:"createdDateTime"`
LastModifiedDateTime time.Time `json:"lastModifiedDateTime"`
Size int64 `json:"size"`
ParentReference *ItemReference `json:"parentReference"`
WebURL string `json:"webUrl"`
File *FileFacet `json:"file"`
Folder *FolderFacet `json:"folder"`
Image *ImageFacet `json:"image"`
Photo *PhotoFacet `json:"photo"`
Audio *AudioFacet `json:"audio"`
Video *VideoFacet `json:"video"`
Location *LocationFacet `json:"location"`
Deleted *DeletedFacet `json:"deleted"`
// Instance attributes
ConflictBehaviour string `json:"@name.conflictBehavior"`
ContentDownloadURL string `json:"@content.downloadUrl"`
GraphDownloadURL string `json:"@microsoft.graph.downloadUrl"`
SourceURL string `json:"@content.sourceUrl"`
// Relationships
Content []byte `json:"content"`
Children []*Item `json:"children"`
Thumbnails *ThumbnailSet `json:"thumbnails"`
}
// Get returns an item with the specified ID.
func (is *ItemService) Get(ctx context.Context, opt ItemOption) (*Item, *http.Response, error) {
req, err := is.newRequest("GET", opt.ItemPath(), nil, nil)
if err != nil {
return nil, nil, err
}
item := new(Item)
resp, err := is.do(req.WithContext(ctx), item)
if err != nil {
return nil, resp, err
}
return item, resp, nil
}
// ListChildren returns a collection of all the Items under an Item
func (is *ItemService) ListChildren(ctx context.Context, itemID string) (*Items, *http.Response, error) {
reqURI := fmt.Sprintf("/drive/items/%s/children", itemID)
req, err := is.newRequest("GET", reqURI, nil, nil)
if err != nil {
return nil, nil, err
}
items := new(Items)
resp, err := is.do(req.WithContext(ctx), items)
if err != nil {
return nil, resp, err
}
return items, resp, nil
}
type newFolder struct {
Name string `json:"name"`
Folder *FolderFacet `json:"folder"`
}
// CreateFolder creates a new folder within the parent.
func (is *ItemService) CreateFolder(ctx context.Context, parentID, folderName string) (*Item, *http.Response, error) {
folder := newFolder{
Name: folderName,
Folder: new(FolderFacet),
}
path := fmt.Sprintf("/drive/items/%s/children/%s", parentID, folderName)
req, err := is.newRequest("PUT", path, nil, folder)
if err != nil {
return nil, nil, err
}
item := new(Item)
resp, err := is.do(req.WithContext(ctx), item)
if err != nil {
return nil, resp, err
}
return item, resp, nil
}
type newWebUpload struct {
SourceURL string `json:"@content.sourceUrl"`
Name string `json:"name"`
File *FileFacet `json:"file"`
}
// Update updates the metadata for a OneDrive Item resource.
// See: http://onedrive.github.io/items/update.htm
func (is ItemService) Update(ctx context.Context, item *Item, ifMatch bool) (*Item, *http.Response, error) {
requestHeaders := make(map[string]string)
if ifMatch {
requestHeaders["if-match"] = item.ETag
}
path := fmt.Sprintf("/drive/items/%s", item.ID)
req, err := is.newRequest("PATCH", path, requestHeaders, item)
if err != nil {
return nil, nil, err
}
resp, err := is.do(req.WithContext(ctx), item)
if err != nil {
return nil, resp, err
}
return item, resp, nil
}
// Delete removed a OneDrive item by using its ID. Note that deleting items
// using this method will move the items to the Recycle Bin, instead of
// permanently deleting them.
// See: http://onedrive.github.io/items/delete.htm
func (is *ItemService) Delete(ctx context.Context, itemID, eTag string) (bool, *http.Response, error) {
requestHeaders := make(map[string]string)
if eTag != "" {
requestHeaders["if-match"] = eTag
}
path := fmt.Sprintf("/drive/items/%s", itemID)
req, err := is.newRequest("DELETE", path, requestHeaders, nil)
if err != nil {
return false, nil, err
}
resp, err := is.do(req.WithContext(ctx), nil)
if err != nil {
return false, resp, err
}
return (resp.StatusCode == statusNoContent), resp, err
}
// Move changes the parent folder for a OneDrive Item resource.
// See: http://onedrive.github.io/items/move.htm
func (is ItemService) Move(ctx context.Context, itemID, parentReference ItemReference) (*Item, *http.Response, error) {
path := fmt.Sprintf("/drive/items/%s", itemID)
req, err := is.newRequest("PATCH", path, nil, parentReference)
if err != nil {
return nil, nil, err
}
item := new(Item)
resp, err := is.do(req.WithContext(ctx), item)
if err != nil {
return nil, resp, err
}
return item, resp, nil
}
// Copy creates a copy of a OneDrive Item resource (including children) under a new parent item.
// See: http://onedrive.github.io/items/copy.htm
func (is ItemService) Copy(ctx context.Context, itemID, name string, parentReference ItemReference) (*Item, *http.Response, error) {
copyAction := struct {
ParentReference *ItemReference `json:"parentReference"`
Name string `json:"name,omitempty"`
}{&parentReference, name}
// The copy action requires a Prefer: respond-async header
headers := map[string]string{"Prefer": "respond-async"}
path := fmt.Sprintf("/drive/items/%s/action.copy", itemID)
req, err := is.newRequest("POST", path, headers, copyAction)
if err != nil {
return nil, nil, err
}
item := new(Item)
resp, err := is.do(req.WithContext(ctx), item)
if err != nil {
return nil, resp, err
}
return item, resp, nil
}