/
upload.go
163 lines (152 loc) · 3.65 KB
/
upload.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
package controllers
import (
"github.com/astaxie/beego"
"os"
"path"
"strconv"
"time"
"github.com/Milo233/go-blog/syserrors"
"mime/multipart"
"io/ioutil"
"encoding/json"
"io"
)
type UploadController struct {
BaseController
ImagesFilePath string
VideosFilePath string
FilesFilePath string
}
type UploadimgFroala struct {
url string
title string
original string
state string
}
const (
PATH_IMAGE = "assert/images"
PATH_VIDEO = "assert/videos"
PATH_FILE = "assert/files"
)
func (ctx *UploadController) NestPrepare() {
ctx.MustLogin()
year, month, _ := time.Now().Date()
ctx.ImagesFilePath = PATH_IMAGE + "/" + strconv.Itoa(year) + month.String() + "/"
if err := os.MkdirAll(ctx.ImagesFilePath, 0777); err != nil {
beego.Error(err)
}
ctx.VideosFilePath = PATH_VIDEO + "/" + strconv.Itoa(year) + month.String() + "/"
if err := os.MkdirAll(ctx.VideosFilePath, 0777); err != nil {
beego.Error(err)
}
ctx.FilesFilePath = PATH_FILE + "/" + strconv.Itoa(year) + month.String() + "/"
if err := os.MkdirAll(ctx.FilesFilePath, 0777); err != nil {
beego.Error(err)
}
}
//图片上传
// @router /uploadimg [post]
func (c *UploadController) UploadImg() {
_, h, err := c.GetFile("file")
if err != nil {
c.Abort500(err)
}
fileSuffix := path.Ext(h.Filename)
newname := strconv.FormatInt(time.Now().UnixNano(), 10) + fileSuffix
var path string
if h != nil {
//保存附件
path = c.ImagesFilePath + newname
err = c.SaveToFile("file", path)
if err != nil {
c.Abort500(err)
}
go saveFileinfo(path, h)
c.JSONOkH("上传成功", H{
"link": "/" + path,
})
} else {
c.Abort500(syserrors.NewError("上传失败", nil))
}
}
func saveFileinfo(path string, f *multipart.FileHeader) error {
bs, err := json.Marshal(f)
if err != nil {
return err
}
return ioutil.WriteFile(path+".json", bs, 0777)
}
// @router /uploadfile [post]
func (c *UploadController) UploadFile() {
_, h, err := c.GetFile("file")
if err != nil {
c.Abort500(err)
}
fileSuffix := path.Ext(h.Filename)
newname := strconv.FormatInt(time.Now().UnixNano(), 10) + fileSuffix
var path string
if h != nil {
//保存附件
path = c.FilesFilePath + newname
err = c.SaveToFile("file", path) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
c.Abort500(err)
}
go saveFileinfo(path, h)
c.JSONOkH("上传成功", H{
"link": "/" + path,
})
} else {
c.Abort500(syserrors.NewError("上传失败", nil))
}
}
//@router /wangeditorfiles [post]
func (c *UploadController) WangeditorUploadFile() {
hs, err := c.GetFiles("files")
if err != nil {
c.JSONOkH("", H{
"errno": -1,
"msg": "上传失败:" + err.Error(),
})
return
}
var len= len(hs)
paths := make([]string, len)
for index, h := range hs {
fileSuffix := path.Ext(h.Filename)
newname := strconv.FormatInt(time.Now().UnixNano(), 10) + fileSuffix
//保存附件
path := c.FilesFilePath + newname
err = saveFile(h, path) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
c.JSONOkH("", H{
"errno": -1,
"msg": "上传失败:" + err.Error(),
})
}
go saveFileinfo(path, h)
paths[index] = "/" + path
}
c.JSONOkH("", H{
"errno": 0,
"data": paths,
})
}
func saveFile(f *multipart.FileHeader,path string) error {
file, err := f.Open()
defer file.Close()
if err != nil {
return err
}
//create destination file making sure the path is writeable.
dst, err := os.Create(path)
defer dst.Close()
if err != nil {
return err
}
//copy the uploaded file to the destination file
if _, err := io.Copy(dst, file); err != nil {
return err
}
return nil
}