Repository navigation
Expand file tree
/
Copy pathupload_controller.go
More file actions
154 lines (134 loc) · 3.31 KB
/
Copy pathupload_controller.go
File metadata and controls
154 lines (134 loc) · 3.31 KB
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
package api
import (
"bbs-go/pkg/uploader"
"io/ioutil"
"strconv"
"github.com/kataras/iris/v12"
"github.com/mlogclub/simple"
"github.com/sirupsen/logrus"
"bbs-go/services"
)
const uploadMaxM = 10
const uploadMaxBytes int64 = 1024 * 1024 * 1024 * uploadMaxM
type UploadController struct {
Ctx iris.Context
}
func (c *UploadController) Post() *simple.JsonResult {
user := services.UserTokenService.GetCurrent(c.Ctx)
if err := services.UserService.CheckPostStatus(user); err != nil {
return simple.JsonError(err)
}
file, header, err := c.Ctx.FormFile("image")
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
defer file.Close()
if header.Size > uploadMaxBytes {
return simple.JsonErrorMsg("图片不能超过" + strconv.Itoa(uploadMaxM) + "M")
}
contentType := header.Header.Get("Content-Type")
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
logrus.Info("上传文件:", header.Filename, " size:", header.Size)
url, err := uploader.PutImage(fileBytes, contentType)
if err != nil {
return simple.JsonErrorMsg(err.Error())
}
return simple.NewEmptyRspBuilder().Put("url", url).JsonResult()
}
// vditor上传
func (c *UploadController) PostEditor() {
errFiles := make([]string, 0)
succMap := make(map[string]string)
user := services.UserTokenService.GetCurrent(c.Ctx)
if err := services.UserService.CheckPostStatus(user); err != nil {
_, _ = c.Ctx.JSON(iris.Map{
"msg": err.Message,
"code": err.Code,
"data": iris.Map{
"errFiles": errFiles,
"succMap": succMap,
},
})
return
}
maxSize := c.Ctx.Application().ConfigurationReadOnly().GetPostMaxMemory()
err := c.Ctx.Request().ParseMultipartForm(maxSize)
if err != nil {
c.Ctx.StatusCode(iris.StatusInternalServerError)
_, _ = c.Ctx.WriteString(err.Error())
return
}
form := c.Ctx.Request().MultipartForm
files := form.File["file[]"]
for _, file := range files {
contentType := file.Header.Get("Content-Type")
f, err := file.Open()
if err != nil {
logrus.Error(err)
errFiles = append(errFiles, file.Filename)
continue
}
fileBytes, err := ioutil.ReadAll(f)
if err != nil {
logrus.Error(err)
errFiles = append(errFiles, file.Filename)
continue
}
url, err := uploader.PutImage(fileBytes, contentType)
if err != nil {
logrus.Error(err)
errFiles = append(errFiles, file.Filename)
continue
}
succMap[file.Filename] = url
}
_, _ = c.Ctx.JSON(iris.Map{
"msg": "",
"code": 0,
"data": iris.Map{
"errFiles": errFiles,
"succMap": succMap,
},
})
}
// vditor 拷贝第三方图片
func (c *UploadController) PostFetch() {
user := services.UserTokenService.GetCurrent(c.Ctx)
if err := services.UserService.CheckPostStatus(user); err != nil {
_, _ = c.Ctx.JSON(iris.Map{
"msg": err.Message,
"code": err.Code,
"data": iris.Map{},
})
return
}
data := make(map[string]string)
err := c.Ctx.ReadJSON(&data)
if err != nil {
_, _ = c.Ctx.JSON(iris.Map{
"msg": err.Error(),
"code": 0,
"data": iris.Map{},
})
return
}
url := data["url"]
output, err := uploader.CopyImage(url)
if err != nil {
_, _ = c.Ctx.JSON(iris.Map{
"msg": err.Error(),
"code": 0,
})
}
_, _ = c.Ctx.JSON(iris.Map{
"msg": "",
"code": 0,
"data": iris.Map{
"originalURL": url,
"url": output,
},
})
}