-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFilePlugin.go
150 lines (118 loc) · 3.55 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
package files
import (
"github.com/go-catupiry/catu"
files_processor "github.com/go-catupiry/files/processor"
"github.com/gookit/event"
"github.com/sirupsen/logrus"
)
type FilePlugin struct {
catu.Pluginer
Name string
FileController *FileController
ImageController *ImageController
Storages map[string]Storager
FileStorageName string
ImageStorageName string
MaxImageWidth uint
MaxImageHeight uint
Processor files_processor.FileProcessor
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 catu.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 catu.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)
return nil
}
func (p *FilePlugin) setTemplateFunctions(app catu.App) error {
app.SetTemplateFunction("image", imageTPLHelper)
app.SetTemplateFunction("images", imagesTPLHelper)
return nil
}
type FilePluginCfgs struct {
Storages map[string]Storager
ImageStyles map[string]ImageStyleCfg
FileStorageName string
ImageStorageName string
MaxImageWidth uint
MaxImageHeight uint
Processor files_processor.FileProcessor
}
type ImageStyleCfg struct {
Width int
Height int
// image format to convert with lowercase like jpg. Default: webp
Format string
}
func NewPlugin(cfgs *FilePluginCfgs) *FilePlugin {
p := FilePlugin{
Name: "files",
FileStorageName: "local",
ImageStorageName: "local",
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
}
return &p
}