-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
storer.go
133 lines (103 loc) · 2.8 KB
/
storer.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
package driver
import (
"context"
"io"
"os"
"sort"
"github.com/admpub/color"
"github.com/admpub/log"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/admpub/nging/v4/application/model/file/storer"
)
// Sizer 尺寸接口
type Sizer interface {
Size() int64
}
// Storer 文件存储引擎接口
type Storer interface {
// 引擎名
Name() string
ErrIsNotExist(err error) bool
// FileDir 文件夹物理路径
FileDir(subpath string) string
// URLDir 文件夹网址路径
URLDir(subpath string) string
// Put 保存文件
Put(dst string, src io.Reader, size int64) (savePath string, viewURL string, err error)
// Get 获取文件
Get(file string) (io.ReadCloser, error)
// Exists 文件是否存在
Exists(file string) (bool, error)
// FileInfo 文件信息
FileInfo(file string) (os.FileInfo, error)
// SendFile 输出文件到浏览器
SendFile(ctx echo.Context, file string) error
// Delete 删除文件
Delete(file string) error
// DeleteDir 删除目录
DeleteDir(dir string) error
// Move 移动文件
Move(src, dst string) error
// PublicURL 文件物理路径转网址
PublicURL(dst string) string
// URLToFile 网址转文件存储路径(非完整路径)
URLToFile(viewURL string) string
// URLToPath 网址转文件路径(完整路径)
URLToPath(viewURL string) string
// 根网址(末尾不含"/")
SetBaseURL(baseURL string)
BaseURL() string
// FixURL 修正网址
FixURL(content string, embedded ...bool) string
// Close 关闭连接
Close() error
}
// Constructor 存储引擎构造函数
type Constructor func(ctx context.Context, typ string) (Storer, error)
var storers = map[string]Constructor{}
// DefaultConstructor 默认构造器
var DefaultConstructor Constructor
// Register 存储引擎注册
func Register(engine string, constructor Constructor) {
log.Debug(color.CyanString(`storer.register:`), engine)
storers[engine] = constructor
}
// Get 获取存储引擎构造器
func Get(engine string) Constructor {
constructor, ok := storers[engine]
if !ok {
return DefaultConstructor
}
return constructor
}
// GetBySettings 获取存储引擎构造器
func GetBySettings() Constructor {
engine := `local`
storerConfig, ok := storer.GetOk()
if ok {
engine = storerConfig.Name
}
return Get(engine)
}
// All 存储引擎集合
func All() map[string]Constructor {
return storers
}
// AllNames 存储引擎集合
func AllNames() []string {
var names []string
for name := range storers {
names = append(names, name)
}
sort.Strings(names)
return names
}
func New(ctx echo.Context, subdir string) (Storer, error) {
storerInfo := storer.Get()
newStore := Get(storerInfo.Name)
if newStore == nil {
return nil, ctx.NewError(code.Unsupported, `存储引擎“%s”未被登记`, storerInfo.Name)
}
return newStore(ctx, subdir)
}