forked from ggordan/go-onedrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
67 lines (54 loc) · 1.67 KB
/
upload.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
package onedrive
import (
"fmt"
"net/http"
"os"
)
const oneHundredMB = 104857600
// UploadFromURL allows your app to upload an item to OneDrive by providing a URL.
// OneDrive will download the file directly from a remote server so your app
// doesn't have to upload the file's bytes.
// See: http://onedrive.github.io/items/upload_url.htm
func (is *ItemService) UploadFromURL(parentID, name, webURL string) (*Item, *http.Response, error) {
requestHeaders := map[string]string{
"Prefer": "respond-async",
}
newFile := newWebUpload{
webURL, name, new(FileFacet),
}
path := fmt.Sprintf("/drive/items/%s/children", parentID)
req, err := is.newRequest("POST", path, requestHeaders, newFile)
if err != nil {
return nil, nil, err
}
item := new(Item)
resp, err := is.do(req, item)
if err != nil {
return nil, resp, err
}
return item, resp, nil
}
// SimpleUpload allows you to provide the contents of a new file or update the
// contents of an existing file in a single API call. This method only supports
// files up to 100MB in size. For larger files use ResumableUpload().
// See: https://dev.onedrive.com/items/upload_put.htm
func (is ItemService) SimpleUpload(folderID string, file *os.File) (*Item, *http.Response, error) {
fileInfo, err := file.Stat()
if err != nil {
return nil, nil, err
}
if fileInfo.Size() >= oneHundredMB {
return nil, nil, ErrFileTooLarge
}
path := fmt.Sprintf("/drive/items/%s/children/%s/content", folderID, file.Name())
req, err := is.newRequest("PUT", path, nil, file)
if err != nil {
return nil, nil, err
}
item := new(Item)
resp, err := is.do(req, item)
if err != nil {
return nil, resp, err
}
return item, resp, nil
}