/
c-attachments.go
130 lines (111 loc) · 3.07 KB
/
c-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
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
package auth
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/kapmahc/fly/web/widgets"
)
type fmAttachmentNew struct {
// Type string `form:"type" binding:"required,max=255"`
// ID uint `form:"id"`
}
func (p *Plugin) newAttachment(c *gin.Context, lang string) (gin.H, error) {
return gin.H{
"type": c.Query("type"),
"id": c.Query("id"),
"title": p.I18n.T(lang, "buttons.upload"),
}, nil
}
func (p *Plugin) createAttachment(c *gin.Context, l string, o interface{}) (interface{}, error) {
user := c.MustGet(CurrentUser).(*User)
// fm := o.(*fmAttachmentNew)
file, header, err := c.Request.FormFile("file")
if err != nil {
return nil, err
}
url, size, err := p.Uploader.Save(header)
if err != nil {
return nil, err
}
// http://golang.org/pkg/net/http/#DetectContentType
buf := make([]byte, 512)
file.Seek(0, 0)
if _, err = file.Read(buf); err != nil {
return nil, err
}
a := Attachment{
Title: header.Filename,
URL: url,
UserID: user.ID,
MediaType: http.DetectContentType(buf),
Length: size / 1024,
ResourceType: DefaultResourceType, //fm.Type,
ResourceID: DefaultResourceID, //fm.ID,
}
if err := p.Db.Create(&a).Error; err != nil {
return nil, err
}
return a, nil
}
func (p *Plugin) editAttachment(c *gin.Context, lang string) (gin.H, error) {
title := p.I18n.T(lang, "buttons.edit")
id := c.Param("id")
item := c.MustGet("attachment").(*Attachment)
fm := widgets.NewForm(
c.Request,
lang,
"/attachments/"+id,
"/attachments",
title,
widgets.NewTextField("title", p.I18n.T(lang, "attributes.title"), item.Title),
)
return gin.H{"form": fm, "title": title}, nil
}
type fmAttachmentEdit struct {
Title string `form:"title" binding:"required,max=255"`
}
func (p *Plugin) updateAttachment(c *gin.Context, l string, o interface{}) (interface{}, error) {
a := c.MustGet("attachment").(*Attachment)
fm := o.(*fmAttachmentEdit)
if err := p.Db.Model(a).Update("title", fm.Title).Error; err != nil {
return nil, err
}
return gin.H{}, nil
}
func (p *Plugin) destroyAttachment(c *gin.Context, l string) (interface{}, error) {
a := c.MustGet("attachment").(*Attachment)
err := p.Db.Delete(a).Error
if err != nil {
return nil, err
}
return a, p.Uploader.Remove(a.URL)
}
func (p *Plugin) indexAttachments(c *gin.Context, l string) (gin.H, error) {
user := c.MustGet(CurrentUser).(*User)
isa := c.MustGet(IsAdmin).(bool)
var items []Attachment
qry := p.Db
if !isa {
qry = qry.Where("user_id = ?", user.ID)
}
if err := qry.Order("updated_at DESC").Find(&items).Error; err != nil {
return nil, err
}
return gin.H{
"title": p.I18n.T(l, "auth.attachments.index.title"),
"items": items,
}, nil
}
func (p *Plugin) canEditAttachment(c *gin.Context) {
user := c.MustGet(CurrentUser).(*User)
var a Attachment
err := p.Db.Where("id = ?", c.Param("id")).First(&a).Error
if err == nil {
if user.ID == a.UserID || c.MustGet(IsAdmin).(bool) {
c.Set("attachment", &a)
return
}
c.AbortWithStatus(http.StatusForbidden)
} else {
c.AbortWithError(http.StatusInternalServerError, err)
}
}