-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
138 lines (109 loc) · 2.92 KB
/
provider.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
package storage
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/zenorachi/dynamic-user-segmentation/internal/config"
"github.com/zenorachi/dynamic-user-segmentation/internal/entity"
"github.com/zenorachi/dynamic-user-segmentation/pkg/logger"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
)
type FileInput struct {
Data []byte
Name string
ContentType string
}
type Provider interface {
UploadFile(ctx context.Context, input FileInput) (string, error)
IsAvailable() bool
}
type GDriveStorage struct {
gDrive *drive.Service
isAvailable bool
}
func NewProvider(cfg *config.GDriveConfig) *GDriveStorage {
if cfg.Credentials == "" {
return &GDriveStorage{isAvailable: false}
}
gDrive, err := drive.NewService(context.Background(), option.WithCredentialsFile(cfg.Credentials))
if err != nil {
logger.Error("google drive", err)
return &GDriveStorage{isAvailable: false}
}
return &GDriveStorage{
gDrive: gDrive,
isAvailable: true,
}
}
func (g *GDriveStorage) IsAvailable() bool {
return g.isAvailable
}
func (g *GDriveStorage) UploadFile(ctx context.Context, input FileInput) (string, error) {
fileId, err := g.getFileIdByName(ctx, input.Name)
if err != nil {
if !errors.Is(err, entity.ErrFileNotFound) {
return "", err
}
id, err := g.createFile(ctx, input)
if err != nil {
return "", err
}
return g.generateFileUrl(id), nil
}
err = g.updateFile(ctx, fileId, input.Data)
if err != nil {
return "", err
}
return g.generateFileUrl(fileId), nil
}
func (g *GDriveStorage) createFile(ctx context.Context, input FileInput) (string, error) {
file := &drive.File{
Name: input.Name,
MimeType: input.ContentType,
}
permissions := &drive.Permission{
Type: "anyone",
Role: "reader",
}
_, err := g.gDrive.Files.Create(file).Context(ctx).Media(bytes.NewReader(input.Data)).Do()
if err != nil {
return "", err
}
fileID, err := g.getFileIdByName(ctx, input.Name)
if err != nil {
return "", err
}
_, err = g.gDrive.Permissions.Create(fileID, permissions).Context(ctx).Do()
if err != nil {
return "", err
}
return fileID, nil
}
func (g *GDriveStorage) updateFile(ctx context.Context, id string, data []byte) error {
_, err := g.gDrive.Files.Update(id, &drive.File{}).Context(ctx).Media(bytes.NewReader(data)).Do()
return err
}
func (g *GDriveStorage) getFileIdByName(ctx context.Context, name string) (string, error) {
files, err := g.getAllFiles(ctx)
if err != nil {
return "", err
}
for _, file := range files {
if file.Name == name {
return file.Id, nil
}
}
return "", entity.ErrFileNotFound
}
func (g *GDriveStorage) getAllFiles(ctx context.Context) ([]*drive.File, error) {
r, err := g.gDrive.Files.List().Context(ctx).Do()
if err != nil {
return nil, err
}
return r.Files, nil
}
func (g *GDriveStorage) generateFileUrl(id string) string {
return fmt.Sprintf("https://drive.google.com/file/d/%s/view?usp=sharing", id)
}