-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
76 lines (63 loc) · 2.41 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
package hakucho
import (
"fmt"
"os"
"github.com/yokoe/hakucho/option"
"google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi"
)
func (c *Client) GetFile(fileID string, fileFields []string) (*drive.File, error) {
return c.driveService.Files.Get(fileID).Fields(escapedFields(fileFields)).Do()
}
func (c *Client) DeleteFile(fileID string) error {
_, err := c.driveService.Files.Update(fileID, &drive.File{Trashed: true}).Do()
return err
}
// UploadFile uploads file to Google Drive
func (c *Client) UploadFile(localFile string, uploadFilename string, options ...option.UploadOption) (*drive.File, error) {
uploadFile, err := os.Open(localFile)
if err != nil {
return nil, err
}
f := &drive.File{Name: uploadFilename}
mediaOptions := []googleapi.MediaOption{}
for _, option := range options {
mediaOptions = append(mediaOptions, option.MediaOption())
}
res, err := c.driveService.Files.Create(f).Media(uploadFile, mediaOptions...).Do()
if err != nil {
return nil, err
}
return res, nil
}
func (c *Client) UploadFileToFolder(localFile string, uploadFilename string, folderID string, options ...option.UploadOption) (*drive.File, error) {
f, err := c.UploadFile(localFile, uploadFilename, options...)
if err != nil {
return nil, fmt.Errorf("upload error: %w", err)
}
if err = c.AddParentFolder(f.Id, folderID); err != nil {
return nil, fmt.Errorf("add parents error: %w", err)
}
return f, nil
}
func (c *Client) AddParentFolder(fileID string, folderID string) error {
_, err := c.driveService.Files.Update(fileID, &drive.File{}).AddParents(folderID).Do()
return err
}
func (c *Client) createPermission(fileID string, email string, role string, sendEmail bool) error {
_, err := c.driveService.Permissions.Create(fileID, &drive.Permission{EmailAddress: email, Role: role, Type: "user"}).SendNotificationEmail(sendEmail).Do()
return err
}
func (c *Client) GrantUserReaderPemission(fileID string, email string, sendEmail bool) error {
return c.createPermission(fileID, email, "reader", sendEmail)
}
func (c *Client) GrantUserWriterPemission(fileID string, email string, sendEmail bool) error {
return c.createPermission(fileID, email, "writer", sendEmail)
}
func (c *Client) ListPermissions(fileID string) ([]*drive.Permission, error) {
list, err := c.driveService.Permissions.List(fileID).Fields("permissions(emailAddress, role)").Do()
if err != nil {
return nil, err
}
return list.Permissions, nil
}