-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrive.go
107 lines (86 loc) · 2.63 KB
/
drive.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
package cloudStorage
import (
"io"
"log"
"mime"
"os"
"google.golang.org/api/drive/v3"
)
// DriveServices manage all drive action
type DriveServices struct {
driveService *drive.Service
}
// NewDrive function return a new mongo client based on singleton pattern
func NewDrive() Filestore {
currentSession := &DriveServices{nil}
driveService, err := drive.NewService(ctx)
if err != nil {
log.Fatalf("Unable to retrieve GOOGLE_APPLICATION_CREDENTIALS %v", err)
}
currentSession.driveService = driveService
log.Println("Connected to Google Drive")
return currentSession
}
// Search ...
func (dr *DriveServices) Search(fileModel *FileModel) (interface{}, error) {
return nil, nil
}
// Metadata ...
func (dr *DriveServices) Metadata(fileModel *FileModel) (interface{}, error) {
return nil, nil
}
// List function return all files
func (dr *DriveServices) List(fileModel *FileModel) (interface{}, error) {
files, err := dr.driveService.Files.List().Do()
return files, err
}
// Upload function upload file to drive
func (dr *DriveServices) Upload(fileModel *FileModel) (interface{}, error) {
f := &drive.File{
MimeType: fileModel.MimeType,
Name: fileModel.Name,
// Parents: []string{parentID},
}
result, err := dr.driveService.Files.Create(f).Media(fileModel.Content).Do()
if err != nil {
return nil, err
}
return result, nil
}
// Download function will return a file base on fileID
func (dr *DriveServices) Download(fileModel *FileModel) (interface{}, error) {
res, err := dr.driveService.Files.Get(fileModel.SourcesID).Download()
if err != nil {
return nil, err
}
// Get file extension
fileExtension, err := mime.ExtensionsByType(res.Header.Get("Content-Type"))
if err != nil {
log.Println("Could not get file extension: " + err.Error())
}
// Create empty file with extension
outFile, err := os.Create("uname" + fileExtension[0])
if err != nil {
return nil, err
}
defer outFile.Close()
// Copy content to file that is created
_, err = io.Copy(outFile, res.Body)
if err != nil {
log.Println("Could not copy content to file: " + err.Error())
}
return "uname" + fileExtension[0], nil
}
// Delete function will delete a file base on fileID
func (dr *DriveServices) Delete(fileModel *FileModel) error {
err := dr.driveService.Files.Delete(fileModel.SourcesID).Do()
return err
}
// Move function will move a file base on 'Sources' and 'Destination'
func (dr *DriveServices) Move(fileModel *FileModel) (interface{}, []error) {
return nil, nil
}
// CreateFolder function will create a folder base on 'Destination'
func (dr *DriveServices) CreateFolder(fileModel *FileModel) (interface{}, error) {
return nil, nil
}