-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handle_attachment.go
168 lines (140 loc) · 4.37 KB
/
handle_attachment.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
package server
import (
"fmt"
"os"
"strconv"
"github.com/ecnepsnai/web"
)
func (h *handle) AttachmentList(request web.Request) (interface{}, *web.Error) {
return AttachmentStore.AllAttachments(), nil
}
func (h *handle) AttachmentUpload(request web.Request) (interface{}, *web.Error) {
session := request.UserData.(*Session)
pathStr := request.HTTP.FormValue("Path")
inheritStr := request.HTTP.FormValue("Inherit")
inherit := inheritStr == "true"
uidStr := request.HTTP.FormValue("UID")
gidStr := request.HTTP.FormValue("GID")
modeStr := request.HTTP.FormValue("Mode")
uid, err := strconv.Atoi(uidStr)
if err != nil {
return nil, web.ValidationError("Invalid uid '%s'", uidStr)
}
gid, err := strconv.Atoi(gidStr)
if err != nil {
return nil, web.ValidationError("Invalid gid '%s'", gidStr)
}
mode, err := strconv.ParseUint(modeStr, 10, 32)
if err != nil {
return nil, web.ValidationError("Invalid mode '%s'", modeStr)
}
fileUpload, info, err := request.HTTP.FormFile("file")
if err != nil {
log.Error("Error getting form file: %s", err.Error())
return nil, web.CommonErrors.BadRequest
}
req := newAttachmentParameters{
Data: fileUpload,
Path: pathStr,
Name: info.Filename,
MimeType: info.Header.Get("Content-Type"),
Owner: RunAs{
Inherit: inherit,
UID: uint32(uid),
GID: uint32(gid),
},
Mode: uint32(mode),
Size: uint64(info.Size),
}
attachment, erro := AttachmentStore.NewAttachment(req)
if erro != nil {
if erro.Server {
return nil, web.CommonErrors.ServerError
}
return nil, web.ValidationError(erro.Message)
}
EventStore.AttachmentAdded(attachment, session.Username)
return attachment, nil
}
func (h *handle) AttachmentGet(request web.Request) (interface{}, *web.Error) {
attachmentID := request.Params.ByName("id")
return AttachmentStore.AttachmentWithID(attachmentID), nil
}
func (v *view) AttachmentDownload(request web.Request, writer web.Writer) (response web.Response) {
attachmentID := request.Params.ByName("id")
attachment := AttachmentStore.AttachmentWithID(attachmentID)
f, err := os.OpenFile(attachment.FilePath(), os.O_RDONLY, 0644)
if err != nil {
response.Status = 500
return
}
response.ContentType = attachment.MimeType
response.Headers = map[string]string{
"Content-Disposition": fmt.Sprintf("attachment; filename=\"%s\"", attachment.Name),
}
response.Reader = f
return
}
func (h *handle) AttachmentEdit(request web.Request) (interface{}, *web.Error) {
session := request.UserData.(*Session)
attachmentID := request.Params.ByName("id")
pathStr := request.HTTP.FormValue("Path")
inheritStr := request.HTTP.FormValue("Inherit")
inherit := inheritStr == "true"
uidStr := request.HTTP.FormValue("UID")
gidStr := request.HTTP.FormValue("GID")
modeStr := request.HTTP.FormValue("Mode")
uid, err := strconv.Atoi(uidStr)
if err != nil {
return nil, web.ValidationError("Invalid uid '%s'", uidStr)
}
gid, err := strconv.Atoi(gidStr)
if err != nil {
return nil, web.ValidationError("Invalid gid '%s'", gidStr)
}
mode, err := strconv.ParseUint(modeStr, 10, 32)
if err != nil {
return nil, web.ValidationError("Invalid mode '%s'", modeStr)
}
fileUpload, info, err := request.HTTP.FormFile("file")
if err != nil && err.Error() != "http: no such file" {
log.Error("Error getting form file: %s", err.Error())
return nil, web.CommonErrors.BadRequest
}
req := editAttachmentParams{
Path: pathStr,
Owner: RunAs{
Inherit: inherit,
UID: uint32(uid),
GID: uint32(gid),
},
Mode: uint32(mode),
}
if info != nil {
req.Data = fileUpload
req.Name = info.Filename
req.MimeType = info.Header.Get("Content-Type")
req.Size = uint64(info.Size)
}
attachment, erro := AttachmentStore.EditAttachment(attachmentID, req)
if erro != nil {
if erro.Server {
return nil, web.CommonErrors.ServerError
}
return nil, web.ValidationError(erro.Message)
}
EventStore.AttachmentModified(attachment, session.Username)
return attachment, nil
}
func (h *handle) AttachmentDelete(request web.Request) (interface{}, *web.Error) {
session := request.UserData.(*Session)
attachmentID := request.Params.ByName("id")
if err := AttachmentStore.DeleteAttachment(attachmentID); err != nil {
if err.Server {
return nil, web.CommonErrors.ServerError
}
return nil, web.ValidationError(err.Message)
}
EventStore.AttachmentDeleted(attachmentID, session.Username)
return true, nil
}