|
|
package main
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
"dsSzxy/Utils"
|
|
|
"dsSzxy/Utils/CommonUtil"
|
|
|
"dsSzxy/Utils/ConfigUtil"
|
|
|
"dsSzxy/Utils/FileUtil"
|
|
|
_ "dsSzxy/docs"
|
|
|
"encoding/base64"
|
|
|
"fmt"
|
|
|
"github.com/disintegration/imaging"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"io/ioutil"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
func Ext(path string) string {
|
|
|
for i := len(path) - 1; i >= 0 && path[i] != '/'; i-- {
|
|
|
if path[i] == '.' {
|
|
|
return path[i:]
|
|
|
}
|
|
|
}
|
|
|
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
|
|
|
// @description
|
|
|
// @host 127.0.0.1:8010
|
|
|
func main() {
|
|
|
// 发布模式
|
|
|
//gin.SetMode(gin.ReleaseMode)
|
|
|
// 开发模式
|
|
|
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 = 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 := p + "/" + trueName
|
|
|
if err := c.SaveUploadedFile(file, filename); err != nil {
|
|
|
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
|
|
|
return
|
|
|
}
|
|
|
//判断扩展名是不是图片
|
|
|
if ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" {
|
|
|
//读取本地文件,本地文件尺寸300*400
|
|
|
imgData, _ := ioutil.ReadFile(filename)
|
|
|
buf := bytes.NewBuffer(imgData)
|
|
|
image, err := imaging.Decode(buf)
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
return
|
|
|
}
|
|
|
//生成缩略图,尺寸150*200,并保持到为文件2.jpg
|
|
|
image = imaging.Resize(image, 0, 150, imaging.Lanczos)
|
|
|
thumbFileName := uuid + "_thumb" + ext
|
|
|
thumbTrueName := p + "/" + thumbFileName
|
|
|
err = imaging.Save(image, thumbTrueName)
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
}
|
|
|
//读入生成图片的base64编码
|
|
|
ff, _ := os.Open(thumbTrueName)
|
|
|
defer ff.Close()
|
|
|
sourceBuffer := make([]byte, 500000)
|
|
|
n, _ := ff.Read(sourceBuffer)
|
|
|
//base64压缩
|
|
|
sourceString := base64.StdEncoding.EncodeToString(sourceBuffer[:n])
|
|
|
c.JSON(200, gin.H{
|
|
|
"success": true,
|
|
|
"url": trueName,
|
|
|
"thumbContent": sourceString,
|
|
|
})
|
|
|
} else {
|
|
|
c.JSON(200, gin.H{
|
|
|
"success": true,
|
|
|
"url": trueName,
|
|
|
})
|
|
|
}
|
|
|
})
|
|
|
// 监听并在 0.0.0.0:8010 上启动服务
|
|
|
r.Run(":" + ConfigUtil.ServerPort)
|
|
|
}
|