-
Notifications
You must be signed in to change notification settings - Fork 0
/
fscloudstorage.go
111 lines (94 loc) · 2.98 KB
/
fscloudstorage.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
package fscloudstorage
import (
"github.com/pkg6/go-flysystem"
"github.com/pkg6/gfs"
"github.com/pkg6/gfs/cloudstoragefs"
"io"
"net/url"
"sync"
)
type FSCloudStorage struct {
gfs.AbstractAdapter
Config *Config
lock *sync.Mutex
}
func New(config *Config) flysystem.IAdapter {
return NewCloudStorage(config)
}
func NewCloudStorage(config *Config) *FSCloudStorage {
a := &FSCloudStorage{Config: config, lock: &sync.Mutex{}}
a.SetPathPrefix(a.Config.PathPrefix)
return a
}
func (a *FSCloudStorage) GFSAdapter() gfs.IAdapter {
return cloudstoragefs.NewGCS(&cloudstoragefs.Config{
CDN: a.Config.CDN,
Bucket: a.Config.Bucket,
WithTimeout: a.Config.WithTimeout,
CredentialsFile: a.Config.CredentialsFile,
CredentialsJSON: a.Config.CredentialsJSON,
Option: a.Config.Option,
})
}
func (a *FSCloudStorage) URL(path string) (*url.URL, error) {
path = a.ApplyPathPrefix(path)
return a.GFSAdapter().URL(path)
}
func (a *FSCloudStorage) Exists(path string) (bool, error) {
path = a.ApplyPathPrefix(path)
return a.GFSAdapter().Exist(path)
}
func (a *FSCloudStorage) WriteReader(path string, reader io.Reader) (string, error) {
path = a.ApplyPathPrefix(path)
err := a.GFSAdapter().WriteReader(path, reader)
return path, err
}
func (a *FSCloudStorage) Write(path string, contents []byte) (string, error) {
path = a.ApplyPathPrefix(path)
err := a.GFSAdapter().Write(path, contents)
return path, err
}
func (a *FSCloudStorage) WriteStream(path, resource string) (string, error) {
path = a.ApplyPathPrefix(path)
err := a.GFSAdapter().WriteStream(path, resource)
return path, err
}
func (a *FSCloudStorage) Read(path string) ([]byte, error) {
path = a.ApplyPathPrefix(path)
return a.GFSAdapter().Read(path)
}
func (a *FSCloudStorage) Delete(path string) (int64, error) {
path = a.ApplyPathPrefix(path)
return a.GFSAdapter().Delete(path)
}
func (a *FSCloudStorage) Size(path string) (int64, error) {
path = a.ApplyPathPrefix(path)
return a.GFSAdapter().Size(path)
}
func (a *FSCloudStorage) Update(path string, contents []byte) (string, error) {
path = a.ApplyPathPrefix(path)
err := a.GFSAdapter().Update(path, contents)
return path, err
}
func (a *FSCloudStorage) UpdateStream(path, resource string) (string, error) {
path = a.ApplyPathPrefix(path)
err := a.GFSAdapter().UpdateStream(path, resource)
return path, err
}
func (a *FSCloudStorage) MimeType(path string) (string, error) {
path = a.ApplyPathPrefix(path)
return a.GFSAdapter().MimeType(path)
}
func (a *FSCloudStorage) Move(source, destination string) (bool, error) {
source = a.ApplyPathPrefix(source)
destination = a.ApplyPathPrefix(destination)
return a.GFSAdapter().Move(source, destination)
}
func (a *FSCloudStorage) Copy(source, destination string) (bool, error) {
source = a.ApplyPathPrefix(source)
destination = a.ApplyPathPrefix(destination)
return a.GFSAdapter().Copy(source, destination)
}
func (a *FSCloudStorage) DiskName() string {
return flysystem.DiskNameGoogleCloudCloudStorage
}