forked from flipped-aurora/gin-vue-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_file_local.go
54 lines (52 loc) · 1.38 KB
/
upload_file_local.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
package utils
import (
"gin-vue-admin/global"
"go.uber.org/zap"
"io"
"mime/multipart"
"os"
"path"
"strings"
"time"
)
func UploadFileLocal(file *multipart.FileHeader) (err error, localPath string, key string) {
// 读取文件后缀
ext := path.Ext(file.Filename)
// 读取文件名并加密
fileName := strings.TrimSuffix(file.Filename, ext)
fileName = MD5V([]byte(fileName))
// 拼接新文件名
lastName := fileName + "_" + time.Now().Format("20060102150405") + ext
// 读取全局变量的定义路径
savePath := global.GVA_CONFIG.Local.Path
// 尝试创建此路径
err = os.MkdirAll(savePath, os.ModePerm)
if err != nil {
global.GVA_LOG.Error("upload local file fail:", zap.Any("err", err))
return err, "", ""
}
// 拼接路径和文件名
dst := savePath + "/" + lastName
// 下面为上传逻辑
// 打开文件 defer 关闭
src, err := file.Open()
if err != nil {
global.GVA_LOG.Error("upload local file fail:", zap.Any("err", err))
return err, "", ""
}
defer src.Close()
// 创建文件 defer 关闭
out, err := os.Create(dst)
if err != nil {
global.GVA_LOG.Error("upload local file fail:", zap.Any("err", err))
return err, "", ""
}
defer out.Close()
// 传输(拷贝)文件
_, err = io.Copy(out, src)
if err != nil {
global.GVA_LOG.Error("upload local file fail:", zap.Any("err", err))
return err, "", ""
}
return nil, dst, lastName
}