-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
seaweedfs.go
170 lines (146 loc) · 4.26 KB
/
seaweedfs.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package seaweedfs
import (
"context"
"io"
"net/url"
"github.com/admpub/errors"
"github.com/admpub/goseaweedfs"
uploadLibrary "github.com/admpub/nging/v5/application/library/upload"
"github.com/admpub/nging/v5/application/model"
"github.com/admpub/nging/v5/application/registry/upload"
"github.com/admpub/nging/v5/application/registry/upload/driver/local"
)
const Name = `seaweedfs`
var _ upload.Storer = &Seaweedfs{}
func init() {
upload.StorerRegister(Name, func(ctx context.Context, subdir string) (upload.Storer, error) {
return NewSeaweedfs(ctx, subdir)
})
}
func NewSeaweedfs(ctx context.Context, subdir string) (*Seaweedfs, error) {
m, err := model.GetCloudStorage(ctx)
if err != nil {
return nil, errors.WithMessage(err, Name)
}
a, err := DefaultConfig.New()
if err != nil {
return nil, errors.WithMessage(err, Name)
}
return &Seaweedfs{
config: DefaultConfig,
model: m,
instance: a,
Filesystem: local.NewFilesystem(ctx, subdir),
}, nil
}
type Seaweedfs struct {
config *Config
model *model.CloudStorage
instance *goseaweedfs.Seaweed
*local.Filesystem
}
func (s *Seaweedfs) Name() string {
return Name
}
func (s *Seaweedfs) filepath(fname string) string {
return s.URLDir(fname)
}
func (s *Seaweedfs) xPut(dstFile string, src io.Reader, size int64) (savePath string, viewURL string, err error) {
savePath = s.filepath(dstFile)
var rs *goseaweedfs.FilerUploadResult
rs, err = s.instance.Filers()[0].Upload(src, size, savePath, s.Subdir, s.config.TTL)
if err != nil {
err = errors.WithMessage(err, Name)
return
}
//com.Dump(rs)
// {
// "name": "config.go",
// "url": "http://127.0.0.1:9001/6,070894a14c",
// "fid": "6,070894a14c",
// "size": 1734
// }
viewURL = rs.FileID //TODO: fileID VS filePath
//viewURL = s.instance.Filers[0]+savePath
return
}
/*
func (s *Seaweedfs) xGet(dstFile string) (io.ReadCloser, error) {
filer := s.instance.Filers()[0]
_, readCloser, err := filer.Get(dstFile, nil, nil)
if err != nil {
err = errors.WithMessage(err, Name)
}
return readCloser, err
}
*/
func (s *Seaweedfs) PublicURL(dstFile string) string {
return s.config.Filers[0].Public + dstFile
}
func (f *Seaweedfs) FixURL(content string, embedded ...bool) string {
rowsByID := f.model.CachedList()
return uploadLibrary.ReplacePlaceholder(content, func(id string) string {
r, y := rowsByID[id]
if !y {
return ``
}
if len(r.Baseurl) > 0 {
return r.Baseurl
}
return f.PublicURL(``)
})
}
func (s *Seaweedfs) xDelete(dstFile string) error {
filer := s.instance.Filers()[0]
err := filer.Delete(dstFile, nil)
if err != nil {
err = errors.WithMessage(err, Name)
}
return err
}
func (s *Seaweedfs) xDeleteDir(dstDir string) error {
err := s.instance.Filers()[0].Delete(dstDir, nil)
if err != nil {
err = errors.WithMessage(err, Name)
}
return err
}
func (s *Seaweedfs) apiPut(dstFile string, src io.Reader, size int64) (fID string, viewURL string, err error) {
var part *goseaweedfs.FilePart
part, err = s.instance.Upload(src, dstFile, size, s.Subdir, s.config.TTL)
if err != nil {
return
}
viewURL, err = s.instance.LookupFileID(part.FileID, url.Values{}, true)
return
}
/*
func (s *Seaweedfs) apiGet(fileID string) (io.ReadCloser, error) {
_, readCloser, err := s.instance.Download(fileID, nil)
if err != nil {
err = errors.WithMessage(err, Name)
}
return readCloser, err
}
*/
func (s *Seaweedfs) apiDelete(fileID string) error {
err := s.instance.DeleteFile(fileID, nil)
if err != nil {
err = errors.WithMessage(err, Name)
}
return err
}