-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider.slocal.go
115 lines (96 loc) · 2.13 KB
/
provider.slocal.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
package provider
import (
"context"
"io"
"mime/multipart"
"os"
"path"
"github.com/diki-haryadi/govega/config"
"github.com/diki-haryadi/govega/file"
"github.com/diki-haryadi/govega/log"
)
type SLocal struct {
Filepath string `json:"filepath" mapstructure:"filepath"`
Secret string `json:"secret"`
encryptor file.Encryptor
}
func NewSLocalStorage(conf config.Getter) (StorageProvider, error) {
var slocal *SLocal
if err := conf.Unmarshal(&slocal); err != nil {
return nil, err
}
err := os.MkdirAll(slocal.Filepath, os.ModePerm)
if err != nil {
log.WithError(err).Errorln(slocal)
return nil, err
}
slocal.encryptor = file.NewAESEncryptor(slocal.Secret)
return slocal, nil
}
func (l *SLocal) Get(ctx context.Context, fullpath string) (io.Reader, error) {
var (
data *os.File
err error
)
fn := path.Join(l.Filepath, fullpath)
data, err = os.Open(fn)
if err != nil {
return nil, err
}
// defer data.Close()
pr, pw := io.Pipe()
go func() {
defer pw.Close()
if err := l.encryptor.Decrypt(data, pw); err != nil {
log.WithError(err).Errorln("Error Decrypting")
}
}()
return pr, nil
}
func (l *SLocal) Put(ctx context.Context, fullpath string, f io.Reader) (err error) {
var (
ff *os.File
r io.Reader
)
pr, pw := io.Pipe()
fn := path.Join(l.Filepath, fullpath)
switch file := f.(type) {
case *os.File:
log.Infoln("os.File")
// If renaming fails we try the normal copying method.
// Renaming could fail if the files are on different devices.
ff = file
ff.Seek(0, 0)
r = ff
case multipart.File:
log.Infoln("multipart.File")
// when f cannot cast to *os.File and f is multipart.sectionReadCloser
file.Seek(0, 0)
r = file
default:
log.Infoln("default")
r = file
}
go func() {
defer pw.Close()
if err := l.encryptor.Encrypt(r, pw); err != nil {
log.WithError(err).Errorln("Error Ecrypting")
}
}()
ff, err = os.Create(fn)
if err != nil {
return err
}
defer func() {
e := ff.Close()
if err == nil {
err = e
}
}()
_, err = copyZeroAlloc(ff, pr)
if err != nil {
log.WithError(err).Errorln("[Put] Error when save the file")
return err
}
return nil
}