forked from go-catupiry/files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilePlugin.go
175 lines (138 loc) · 4.18 KB
/
FilePlugin.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
package files
import (
"github.com/go-bolo/bolo"
"github.com/go-bolo/files/migrations"
files_processor "github.com/go-bolo/files/processor"
"github.com/gookit/event"
"github.com/sirupsen/logrus"
)
type FilePlugin struct {
bolo.Pluginer
Name string
FileController *FileController
ImageController *ImageController
Storages map[string]Storager
FileStorageName string
ImageStorageName string
MaxImageWidth uint
MaxImageHeight uint
Processor files_processor.FileProcessor
ImageFormat string
ImageStyles map[string]ImageStyleCfg
}
func (p *FilePlugin) GetName() string {
return p.Name
}
func (p *FilePlugin) GetStorage(fileTypeName string) Storager {
return p.Storages[fileTypeName]
}
func (p *FilePlugin) SetStorage(fileTypeName string, s Storager) error {
p.Storages[fileTypeName] = s
return nil
}
func (p *FilePlugin) Init(app bolo.App) error {
logrus.Debug(p.GetName() + " Init")
p.FileController = NewFileController(&FileControllerConfiguration{
App: app,
})
p.ImageController = NewImageController(&ImageControllerConfiguration{
App: app,
})
app.GetEvents().On("bindRoutes", event.ListenerFunc(func(e event.Event) error {
return p.BindRoutes(app)
}), event.Normal)
app.GetEvents().On("setTemplateFunctions", event.ListenerFunc(func(e event.Event) error {
return p.setTemplateFunctions(app)
}), event.Normal)
return nil
}
func (p *FilePlugin) BindRoutes(app bolo.App) error {
logrus.Debug(p.GetName() + " BindRoutes")
ctl := p.ImageController
ctlFile := p.FileController
routerAvatar := app.SetRouterGroup("avatar", "/avatar")
routerAvatar.GET("/:userID", ctl.GetAvatar)
routerAPI := app.SetRouterGroup("image-api", "/api/v1/image")
routerAPI.GET("", ctl.Query)
routerAPI.GET("/:id", ctl.FindOne)
routerAPI.POST("/:id", ctl.Update)
routerAPI.POST("/:id/reprocess", ctl.UpdateImageToReprocess)
// routerAPI.GET("/:id", ctl.Delete)
routerAPI.GET("/:style/:id", ctl.FindOne)
routerAPI.GET("/:id/data", ctl.FindOneData)
routerAPI.POST("", ctl.UploadFile)
routerFileAPI := app.SetRouterGroup("file-api", "/api/v1/file")
routerFileAPI.GET("", ctlFile.Query)
routerFileAPI.GET("/:id", ctlFile.FindOne)
routerFileAPI.POST("/:id", ctlFile.Update)
// routerAPI.GET("/:id", ctl.Delete)
routerFileAPI.GET("/:style/:id", ctlFile.FindOne)
routerFileAPI.GET("/:id/data", ctlFile.FindOneData)
routerFileAPI.POST("", ctlFile.UploadFile)
routerV2 := app.SetRouterGroup("images-v2-api", "/api/v2/image")
app.SetResource("images-v2", NewImageController(&ImageControllerConfiguration{
App: app,
}), routerV2)
routerV2.GET("/:id/reset-styles", ctl.ResetImageStyles)
routerFileV2 := app.SetRouterGroup("files-v2-api", "/api/v2/file")
app.SetResource("files-v2", NewFileController(&FileControllerConfiguration{
App: app,
}), routerFileV2)
return nil
}
func (p *FilePlugin) setTemplateFunctions(app bolo.App) error {
app.SetTemplateFunction("image", imageTPLHelper)
app.SetTemplateFunction("images", imagesTPLHelper)
return nil
}
func (p *FilePlugin) GetMigrations() []*bolo.Migration {
return []*bolo.Migration{
migrations.GetInitMigration(),
migrations.GetMigration2(),
}
}
type FilePluginCfgs struct {
Storages map[string]Storager
ImageFormat string
ImageStyles map[string]ImageStyleCfg
FileStorageName string
ImageStorageName string
MaxImageWidth uint
MaxImageHeight uint
Processor files_processor.FileProcessor
}
type ImageStyleCfg struct {
Width int
Height int
}
func NewPlugin(cfgs *FilePluginCfgs) *FilePlugin {
p := FilePlugin{
Name: "files",
FileStorageName: "local",
ImageStorageName: "local",
ImageFormat: cfgs.ImageFormat,
ImageStyles: cfgs.ImageStyles,
MaxImageWidth: 2560,
MaxImageHeight: 1700,
Processor: cfgs.Processor,
}
if cfgs.Storages != nil {
p.Storages = cfgs.Storages
}
if cfgs.FileStorageName != "" {
p.FileStorageName = cfgs.FileStorageName
}
if cfgs.ImageStorageName != "" {
p.ImageStorageName = cfgs.ImageStorageName
}
if cfgs.MaxImageWidth != 0 {
p.MaxImageWidth = cfgs.MaxImageWidth
}
if cfgs.MaxImageHeight != 0 {
p.MaxImageHeight = cfgs.MaxImageHeight
}
if p.ImageFormat == "" {
p.ImageFormat = "png"
}
return &p
}