This repository was archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.go
More file actions
261 lines (220 loc) · 6.75 KB
/
Copy pathFile.go
File metadata and controls
261 lines (220 loc) · 6.75 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package box
import (
"bytes"
"encoding/json"
"errors"
"image"
"image/jpeg"
"image/png"
"io/ioutil"
"log"
"mime/multipart"
"os"
"reflect"
"strconv"
"strings"
)
const (
fileURL = "https://api.box.com/2.0/files/"
uploadURL = "https://upload.box.com/api/2.0/files/content"
)
// GetFileInfo : Get information about a file.
func (sdk *SDK) GetFileInfo(fileID string) (*FileObject, error) {
response, err := sdk.request("GET", fileURL+fileID, nil, nil)
if err != nil {
log.Println(err)
return nil, err
}
fileObject := &FileObject{}
json.Unmarshal(response, &fileObject)
return fileObject, nil
}
// GetThumbnail gets a thumbnail image for the requested file.
func (sdk *SDK) GetThumbnail(fileID, extension string, minHeight, minWidth int) (image.Image, error) {
opts := "?min_height=" + strconv.Itoa(minHeight) + "&min_width=" + strconv.Itoa(minWidth)
response, err := sdk.request("GET", fileURL+fileID+"/thumbnail."+extension+opts, nil, nil)
if err != nil {
log.Println(err)
return nil, err
}
var image []byte
json.Unmarshal(response, &image)
switch extension {
case "png":
return png.Decode(bytes.NewReader(image))
case "jpg":
case "jpeg":
return jpeg.Decode(bytes.NewReader(image))
}
return nil, nil
}
// CopyFile copies a file. The version and a new name can be optionally supplied.
func (sdk *SDK) CopyFile(fileID, folderID, name, version string) (*FileObject, error) {
body := map[string]interface{}{"parent": map[string]string{"id": folderID}}
if name != "" {
body["name"] = name
}
if version != "" {
body["version"] = version
}
payload, err := json.Marshal(body)
headers := map[string]string{"Content-Type": "application/json"}
response, err := sdk.request("POST", fileURL+fileID+"/copy", bytes.NewBufferString(string(payload)), headers)
if err != nil {
log.Println(err)
return nil, err
}
fileObject := &FileObject{}
json.Unmarshal(response, &fileObject)
return fileObject, nil
}
// GetEmbedLink returns information about the file with 'ID' fileID.
func (sdk *SDK) GetEmbedLink(fileID string) (*EmbeddedFile, error) {
sdk.RequestAccessToken()
response, err := sdk.request("GET", fileURL+fileID+"?fields=expiring_embed_link", nil, nil)
if err != nil {
log.Println(err)
return nil, err
}
fileObject := &EmbeddedFile{}
json.Unmarshal(response, &fileObject)
return fileObject, nil
}
// UpdateFile updates the file with a new name and content.
func (sdk *SDK) UpdateFile(fileID, newFilename string) error {
return errors.New("No implementation for (SDK)UpdateFile()")
}
// DeleteFile deletes a file in a specific folder with an 'ID' matching fileID.
func (sdk *SDK) DeleteFile(fileID, etag string) error {
headers := make(map[string]string)
headers["If-Match"] = etag
_, err := sdk.request("DELETE", fileURL+fileID, nil, headers)
if err != nil {
log.Println(err)
return err
}
return nil
}
// DownloadFile : Retrieves the actual data of the file. An optional version
// parameter can be set to download a previous version of the file.
func (sdk *SDK) DownloadFile(fileID, location string) error {
response, err := sdk.request("GET", fileURL+fileID+"/content", nil, nil)
if err != nil {
log.Println(err)
return err
}
if location == "." || location == ".." || !strings.HasSuffix(location, "/") {
location += "/"
}
fInfo, err := sdk.GetFileInfo(fileID)
file, err := os.Create(location + fInfo.Name)
if err != nil {
log.Fatalln(err)
return err
}
defer file.Close()
_, err = file.Write(response)
if err != nil {
log.Fatalln(err)
return err
}
return nil
}
// UploadFile uses the Upload API to allow users to add a new file. The user
// can then upload a file by specifying the destination folder for the file.
// If the user provides a file name that already exists in the destination
// folder, the user will receive an error.
func (sdk *SDK) UploadFile(inFile interface{}, newFilename, folderID string) (*PathCollection, error) {
fileInputType := reflect.TypeOf(inFile)
var filename string
var contents []byte
if fileInputType.Name() == "" {
// Passed a file object.
contents = inFile.([]byte)
} else if fileInputType.Name() == "string" {
// Passed a filename rather than a file.
filename = inFile.(string)
file, err := os.Open(filename)
if err != nil {
log.Fatalln(err)
return nil, err
}
defer file.Close()
contents, err = ioutil.ReadAll(file)
if err != nil {
log.Fatalln(err)
return nil, err
}
}
// Did not specify a new file name, so keep anme the same.
if newFilename == "" && filename != "" {
newFilename = filename
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", newFilename)
if err != nil {
log.Fatalln(err)
return nil, err
}
part.Write(contents)
fields := map[string]string{
"filename": filename,
"attributes": `{"name":"` + newFilename + `","parent":{"id":"` + folderID + `"}}`,
}
for field, value := range fields {
err = writer.WriteField(field, value)
if err != nil {
log.Fatalln(err)
return nil, err
}
}
err = writer.Close()
if err != nil {
log.Fatalln(err)
return nil, err
}
headers := map[string]string{
"Content-Type": writer.FormDataContentType(),
"Content-Length": string(body.Len()),
}
response, err := sdk.request("POST", uploadURL, body, headers)
if err != nil {
log.Println(err)
return nil, err
}
pathCollection := &PathCollection{}
json.Unmarshal(response, &pathCollection)
return pathCollection, nil
}
///////////////////////////////////////////////////////////////////////////////
// FILE VERSION
///////////////////////////////////////////////////////////////////////////////
// UploadFileVersion : Uploading a new file version is performed in the same
// way as uploading a file. This method is used to upload a new version of an
// existing file in a user’s account.
func (sdk *SDK) UploadFileVersion(fileID, newName string) {}
///////////////////////////////////////////////////////////////////////////////
// UPLOAD (CUNKED)
///////////////////////////////////////////////////////////////////////////////
// Session TODO: Add definition
type Session struct {
sessionID string
fileSize int32
}
// NewFile TODO: Add definition
func (s *Session) NewFile(folderID string, fileSize int32, fileName string) {}
// NewVersion TODO: Add definition
func (s *Session) NewVersion(folderID string, fileSize int32, fileName string) {}
// UploadPart TODO: Add definition
func (s *Session) UploadPart() {}
// ListParts TODO: Add definition
func (s *Session) ListParts(offset int, limit int) {}
// CommitUpload TODO: Add definition
func (s *Session) CommitUpload(partID string, offset int, size int32) {}
// Abort TODO: Add definition
func (s *Session) Abort() {}
// PreflightCheck TODO: Add definition
func PreflightCheck(name string, parentID string, size int32) bool {
return true
}