/
p-attachments.go
57 lines (49 loc) · 1.31 KB
/
p-attachments.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
package nut
import (
"github.com/facebookgo/inject"
"github.com/gin-gonic/gin"
"github.com/go-pg/pg"
"github.com/kapmahc/axe/web"
"github.com/urfave/cli"
)
// AttachmentsPlugin attachments plugin
type AttachmentsPlugin struct {
UEditor *web.UEditor `inject:""`
I18n *web.I18n `inject:""`
Cache *web.Cache `inject:""`
Router *gin.Engine `inject:""`
DB *pg.DB `inject:""`
Layout *Layout `inject:""`
S3 *web.S3 `inject:""`
Jwt *web.Jwt `inject:""`
}
// Init init beans
func (p *AttachmentsPlugin) Init(*inject.Graph) error {
return nil
}
// Shell console commands
func (p *AttachmentsPlugin) Shell() []cli.Command {
return []cli.Command{}
}
// Mount register
func (p *AttachmentsPlugin) Mount() error {
ueditor := p.UEditor.Upload(
p.upload, p.list(func(a *Attachment) bool {
return a.IsPicture()
}),
p.list(func(a *Attachment) bool {
return !a.IsPicture()
}),
)
htm := p.Router.Group("/attachments")
htm.GET("/ueditor", ueditor)
htm.POST("/ueditor", ueditor)
api := p.Router.Group("/api", p.Layout.MustSignInMiddleware)
api.GET("/attachments", p.Layout.JSON(p.index))
api.POST("/attachments", p.Layout.JSON(p.create))
api.DELETE("/attachments/:id", p.canEdit, p.Layout.JSON(p.destroy))
return nil
}
func init() {
web.Register(&AttachmentsPlugin{})
}