-
Notifications
You must be signed in to change notification settings - Fork 2
/
file-local-custom.go
136 lines (114 loc) · 3.56 KB
/
file-local-custom.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
package storage
import (
"encoding/json"
"io"
"log"
"os"
"sync"
"github.com/gammazero/workerpool"
"github.com/golang-common-packages/hash"
multierror "github.com/hashicorp/go-multierror"
)
// CustomFileClient manage all custom file action
type CustomFileClient struct {
config *CustomFile
}
var (
// customFileClientSessionMapping singleton pattern
customFileClientSessionMapping = make(map[string]*CustomFileClient)
)
// newCustomFile init new instance
func newCustomFile(config *CustomFile) IFILE {
hasher := &hash.Client{}
configAsJSON, err := json.Marshal(config)
if err != nil {
log.Fatalln("Unable to marshal local custom file configuration: ", err)
}
configAsString := hasher.SHA1(string(configAsJSON))
currentCustomFileClientSession := customFileClientSessionMapping[configAsString]
if currentCustomFileClientSession == nil {
currentCustomFileClientSession = &CustomFileClient{config: config}
customFileClientSessionMapping[configAsString] = currentCustomFileClientSession
log.Println("File custom is ready")
}
return currentCustomFileClientSession
}
// List all of file and folder
func (cf *CustomFileClient) List(pageSize int64, pageToken ...string) (interface{}, error) {
f, err := os.Open(cf.config.RootServiceDirectory)
if err != nil {
log.Println("Unable to open root servie directory: ", err)
return nil, err
}
files, err := f.Readdir(-1)
defer f.Close()
if err != nil {
log.Println("Unable to read directory: ", err)
return nil, err
}
return files, nil
}
// GetMetaData from file based on fileID (that is file name)
func (cf *CustomFileClient) GetMetaData(fileID string) (interface{}, error) {
return os.Stat(fileID)
}
// CreateFolder on root service directory
func (cf *CustomFileClient) CreateFolder(name string, parents ...string) (interface{}, error) {
return nil, os.MkdirAll(cf.config.RootServiceDirectory+name, os.ModePerm)
}
// Upload file to root service directory
func (cf *CustomFileClient) Upload(name string, fileContent io.Reader, parents ...string) (interface{}, error) {
// Open file using READ & WRITE permission, and check if file exists
var _, err = os.Stat(cf.config.RootServiceDirectory + name)
// create file if not exists
if os.IsNotExist(err) {
var file, err = os.Create(cf.config.RootServiceDirectory + name)
if err != nil {
log.Println("Unable to create file: ", err)
return nil, err
}
defer file.Close()
// Write content to file.
_, err = file.Write(streamToByte(fileContent))
if err != nil {
log.Println("Unable to write to file: ", err)
return nil, err
}
}
return nil, nil
}
// Download feature doesn't implemented for this service
func (cf *CustomFileClient) Download(fileID string) (interface{}, error) {
return nil, nil
}
// Move file to new location based on fileID, oldParentID, newParentID
func (cf *CustomFileClient) Move(fileID, oldParentID, newParentID string) (interface{}, error) {
err := os.Rename(oldParentID+fileID, newParentID+fileID)
if err != nil {
log.Println("Unable to move file: ", err)
return nil, err
}
return nil, nil
}
// Delete file/folder based on IDs (that is the list of file name)
func (cf *CustomFileClient) Delete(fileIDs []string) error {
var mu sync.Mutex
var errs *multierror.Error
dwp := workerpool.New(cf.config.PoolSize)
for _, fileID := range fileIDs {
fileID := fileID
dwp.Submit(func() {
if err := os.Remove(fileID); err != nil {
mu.Lock()
errs = multierror.Append(errs, err)
mu.Unlock()
}
})
}
dwp.StopWait()
// Return an error if any failed
if err := errs.ErrorOrNil(); err != nil {
return err
}
return nil
}