/
client.go
67 lines (59 loc) · 1.4 KB
/
client.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 gdutils
import (
"google.golang.org/api/drive/v3"
"io"
"net/http"
)
type ServerClient drive.Service
func (s *ServerClient) GetDrive(DriveId string) (*Drive, error) {
d, e := s.Drives.Get(DriveId).Do()
if e != nil {
return nil, e
}
return &Drive{
Drive: d,
ServerClient: s,
}, nil
}
// GetDriveList 获取为共享drive的id
//不包含自己的drive
func (s *ServerClient) GetDriveList(PageSize int64) (*drive.DriveList, error) {
return s.Drives.List().PageSize(PageSize).Do()
}
func (s *ServerClient) GetFile(FileId string) (*File, error) {
f, e := s.Files.Get(FileId).
SupportsAllDrives(true).Do()
return &File{
File: f,
s: s,
}, e
}
// Upload 上传文件
func (s *ServerClient) Upload(FileName string, FolderId string, Reader io.Reader) (*File, error) {
f, e := s.Files.Create(&drive.File{
Name: FileName,
Parents: []string{FolderId}},
).Media(Reader).SupportsAllDrives(true).Do()
return &File{
File: f,
s: s,
}, e
}
// Download 下载文件
func (s *ServerClient) Download(FileId string, opt *DownloadOpt) (*http.Response, error) {
req := s.Files.Get(FileId).
SupportsAllDrives(true)
if opt != nil {
header := req.Header()
if opt.Range != "" {
header.Set("Range", opt.Range)
}
}
return req.Download()
}
// Delete 删除文件
func (s *ServerClient) Delete(FileId string) error {
return s.Files.Delete(FileId).
SupportsAllDrives(true).
Do()
}