|
|
|
@ -1,16 +1,31 @@
|
|
|
|
|
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"
|
|
|
|
|
"github.com/qiniu/go-sdk/v7/auth/qbox"
|
|
|
|
|
"github.com/qiniu/go-sdk/v7/storage"
|
|
|
|
|
"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 ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @title 东师理想智慧校园支撑系统
|
|
|
|
|
// @version 2.0
|
|
|
|
|
// @description
|
|
|
|
@ -28,19 +43,65 @@ func main() {
|
|
|
|
|
var logo = FileUtil.ReadFileContent("./Config/logo.txt")
|
|
|
|
|
fmt.Print(logo)
|
|
|
|
|
|
|
|
|
|
//http://127.0.0.1:8010/getQiNiuToken
|
|
|
|
|
//http://192.168.100.100:8010/upload
|
|
|
|
|
//post
|
|
|
|
|
//controller注册
|
|
|
|
|
r.GET("/getQiNiuToken", func(context *gin.Context) {
|
|
|
|
|
putPolicy := storage.PutPolicy{
|
|
|
|
|
Scope: ConfigUtil.BUCKET,
|
|
|
|
|
//上传文件
|
|
|
|
|
r.MaxMultipartMemory = 80 << 20 // 80 MiB
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
mac := qbox.NewMac(ConfigUtil.QiNiuAK, ConfigUtil.QiNiuSK)
|
|
|
|
|
upToken := putPolicy.UploadToken(mac)
|
|
|
|
|
|
|
|
|
|
context.JSON(200, gin.H{
|
|
|
|
|
"success": true,
|
|
|
|
|
"token": upToken,
|
|
|
|
|
})
|
|
|
|
|
//扩展名
|
|
|
|
|
ext := Ext(file.Filename)
|
|
|
|
|
ext = strings.ToLower(ext)
|
|
|
|
|
//真实文件名
|
|
|
|
|
uuid := CommonUtil.GetUUID()
|
|
|
|
|
trueName := uuid + ext
|
|
|
|
|
filename := ConfigUtil.SavePath + 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, 150, 200, imaging.Lanczos)
|
|
|
|
|
thumbFileName := uuid + "_thumb" + ext
|
|
|
|
|
thumbTrueName := ConfigUtil.SavePath + 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)
|
|
|
|
|