commit
continuous-integration/drone/push Build is passing Details

master
黄海 4 years ago
parent b07a562dc1
commit 39ea765018

@ -25,6 +25,16 @@ func Ext(path string) string {
}
return ""
}
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// @title 东师理想智慧校园支撑系统
// @version 2.0
@ -37,31 +47,40 @@ func main() {
gin.SetMode(gin.DebugMode)
// 开启gin服务器
r := gin.Default()
//设置静态资源
r.Static("/static", "./static")
// 允许跨域
r.Use(Utils.Cors())
// 显示Logo
var logo = FileUtil.ReadFileContent("./Config/logo.txt")
fmt.Print(logo)
//http://192.168.100.100:8010/static/upload.html
//http://192.168.100.100:8010/upload
//post
//controller注册
//上传文件
r.MaxMultipartMemory = 80 << 20 // 80 MiB
r.MaxMultipartMemory = 100 << 20 // 设置最大上传大小为100M
r.POST("/upload", func(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
return
}
//扩展名
ext := Ext(file.Filename)
ext = strings.ToLower(ext)
//真实文件名
uuid := CommonUtil.GetUUID()
//子目录
p := ConfigUtil.SavePath + uuid[0:2]
exist, _ := PathExists(p)
if !exist {
os.Mkdir(p, os.ModePerm)
}
trueName := uuid + ext
filename := ConfigUtil.SavePath + trueName
filename := p + "/" + trueName
if err := c.SaveUploadedFile(file, filename); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
return
@ -77,9 +96,9 @@ func main() {
return
}
//生成缩略图尺寸150*200并保持到为文件2.jpg
image = imaging.Resize(image, 150, 200, imaging.Lanczos)
image = imaging.Resize(image, 0, 150, imaging.Lanczos)
thumbFileName := uuid + "_thumb" + ext
thumbTrueName := ConfigUtil.SavePath + thumbFileName
thumbTrueName := p + "/" + thumbFileName
err = imaging.Save(image, thumbTrueName)
if err != nil {
fmt.Println(err)
@ -92,9 +111,9 @@ func main() {
//base64压缩
sourceString := base64.StdEncoding.EncodeToString(sourceBuffer[:n])
c.JSON(200, gin.H{
"success": true,
"url": trueName,
"thumbContent": sourceString,
"success": true,
"url": trueName,
"thumbContent": sourceString,
})
} else {
c.JSON(200, gin.H{

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>上传测试</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<p><input type="file" name="file"></p>
<p><input type="submit" value="submit"></p>
</form>
</body>
</html>
Loading…
Cancel
Save