forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediaStorage.go
239 lines (214 loc) · 5.51 KB
/
mediaStorage.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/jlaffaye/ftp"
)
func (a *goBlog) initMediaStorage() {
a.mediaStorageInit.Do(func() {
type initFunc func() mediaStorage
for _, fc := range []initFunc{a.initBunnyCdnMediaStorage, a.initFtpMediaStorage, a.initLocalMediaStorage} {
a.mediaStorage = fc()
if a.mediaStorage != nil {
break
}
}
})
}
func (a *goBlog) mediaStorageEnabled() bool {
a.initMediaStorage()
return a.mediaStorage != nil
}
type mediaStorageSaveFunc func(filename string, file io.Reader) (location string, err error)
func (a *goBlog) saveMediaFile(filename string, f io.Reader) (string, error) {
a.initMediaStorage()
if a.mediaStorage == nil {
return "", errors.New("no media storage configured")
}
loc, err := a.mediaStorage.save(filename, f)
if err != nil {
return "", err
}
return a.getFullAddress(loc), nil
}
func (a *goBlog) deleteMediaFile(filename string) error {
a.initMediaStorage()
if a.mediaStorage == nil {
return errors.New("no media storage configured")
}
return a.mediaStorage.delete(filepath.Base(filename))
}
type mediaFile struct {
Name string
Location string
Time time.Time
Size int64
}
func (a *goBlog) mediaFiles() ([]*mediaFile, error) {
a.initMediaStorage()
if a.mediaStorage == nil {
return nil, errors.New("no media storage configured")
}
return a.mediaStorage.files()
}
func (a *goBlog) mediaFileLocation(name string) string {
a.initMediaStorage()
if a.mediaStorage == nil {
return ""
}
return a.mediaStorage.location(name)
}
type mediaStorage interface {
save(filename string, file io.Reader) (location string, err error)
delete(filename string) (err error)
files() (files []*mediaFile, err error)
location(filename string) (location string)
}
type localMediaStorage struct {
mediaURL string // optional
path string // required
}
func (a *goBlog) initLocalMediaStorage() mediaStorage {
ms := &localMediaStorage{
path: mediaFilePath,
}
if config := a.cfg.Micropub.MediaStorage; config != nil && config.MediaURL != "" {
ms.mediaURL = config.MediaURL
}
return ms
}
func (l *localMediaStorage) save(filename string, file io.Reader) (location string, err error) {
if err = saveToFile(file, filepath.Join(l.path, filename)); err != nil {
return "", err
}
return l.location(filename), nil
}
func (l *localMediaStorage) delete(filename string) (err error) {
if err = os.MkdirAll(l.path, 0777); err != nil {
return err
}
return os.Remove(filepath.Join(l.path, filename))
}
func (l *localMediaStorage) files() (files []*mediaFile, err error) {
if err = os.MkdirAll(l.path, 0777); err != nil {
return nil, err
}
entries, err := os.ReadDir(l.path)
if err != nil {
return nil, err
}
for _, e := range entries {
fi, er := e.Info()
if er != nil {
continue
}
if fi.Mode().IsRegular() {
files = append(files, &mediaFile{
Name: fi.Name(),
Location: l.location(fi.Name()),
Time: fi.ModTime(),
Size: int64(fi.Size()),
})
}
}
return files, nil
}
func (l *localMediaStorage) location(name string) string {
if l.mediaURL != "" {
return fmt.Sprintf("%s/%s", l.mediaURL, name)
}
return fmt.Sprintf("/m/%s", name)
}
func (a *goBlog) initBunnyCdnMediaStorage() mediaStorage {
config := a.cfg.Micropub.MediaStorage
if config == nil || config.BunnyStorageName == "" || config.BunnyStorageKey == "" || config.MediaURL == "" {
return nil
}
address := "storage.bunnycdn.com:21"
if config.BunnyStorageRegion != "" {
address = fmt.Sprintf("%s.%s", strings.ToLower(config.BunnyStorageRegion), address)
}
return &ftpMediaStorage{
address: address,
user: config.BunnyStorageName,
password: config.BunnyStorageKey,
mediaURL: config.MediaURL,
}
}
type ftpMediaStorage struct {
address string // required
user string // required
password string // required
mediaURL string // required
}
func (a *goBlog) initFtpMediaStorage() mediaStorage {
config := a.cfg.Micropub.MediaStorage
if config == nil || config.FTPAddress == "" || config.FTPUser == "" || config.FTPPassword == "" {
return nil
}
return &ftpMediaStorage{
address: config.FTPAddress,
user: config.FTPUser,
password: config.FTPPassword,
mediaURL: config.MediaURL,
}
}
func (f *ftpMediaStorage) save(filename string, file io.Reader) (location string, err error) {
c, err := f.connection()
if err != nil {
return "", err
}
defer c.Quit()
if err = c.Stor(filename, file); err != nil {
return "", err
}
return f.location(filename), nil
}
func (f *ftpMediaStorage) delete(filename string) (err error) {
c, err := f.connection()
if err != nil {
return err
}
defer c.Quit()
return c.Delete(filename)
}
func (f *ftpMediaStorage) files() (files []*mediaFile, err error) {
c, err := f.connection()
if err != nil {
return nil, err
}
defer c.Quit()
w := c.Walk("")
for w.Next() {
if s := w.Stat(); s.Type == ftp.EntryTypeFile {
files = append(files, &mediaFile{
Name: s.Name,
Location: f.location(s.Name),
Time: s.Time,
Size: int64(s.Size),
})
}
}
return files, nil
}
func (f *ftpMediaStorage) location(name string) string {
return fmt.Sprintf("%s/%s", f.mediaURL, name)
}
func (f *ftpMediaStorage) connection() (*ftp.ServerConn, error) {
if f.address == "" || f.user == "" || f.password == "" {
return nil, errors.New("missing FTP config")
}
c, err := ftp.Dial(f.address, ftp.DialWithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if err = c.Login(f.user, f.password); err != nil {
return nil, err
}
return c, nil
}