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