You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
3.6 KiB
145 lines
3.6 KiB
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/minio/minio-go/v6"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// @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("/dsSzxy/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("/dsSzxy/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 := CommonUtil.Ext(file.Filename)
|
|
ext = strings.ToLower(ext)
|
|
//真实文件名
|
|
uuid := CommonUtil.GetUUID()
|
|
//子目录
|
|
p := ConfigUtil.SavePath + uuid[0:2]
|
|
exist := CommonUtil.Exists(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
|
|
}
|
|
//拼接的下载路径
|
|
downUrl := "/dsSzxy/down/" + trueName[0:2] + "/" + trueName
|
|
|
|
//文件大小
|
|
content, err := ioutil.ReadFile(filename)
|
|
if err == nil {
|
|
fmt.Println("file size is ", len(content))
|
|
}
|
|
size := len(content)
|
|
|
|
// 初使化 minio client对象。
|
|
minioClient, err := minio.New(ConfigUtil.Minio_endpoint, ConfigUtil.Minio_accessKeyId, ConfigUtil.Minio_secretAccessKey, false)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
// 使用FPutObject上传一个zip文件。
|
|
contentType := "application/octet-stream"
|
|
_, err = minioClient.FPutObject(ConfigUtil.Minio_bucket, trueName[0:2]+"/"+trueName, filename, minio.PutObjectOptions{ContentType: contentType})
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
//判断扩展名是不是图片
|
|
if ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" {
|
|
//读取本地文件
|
|
imgData, _ := ioutil.ReadFile(filename)
|
|
buf := bytes.NewBuffer(imgData)
|
|
image, err := imaging.Decode(buf)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
//生成缩略图
|
|
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])
|
|
|
|
//删除原始文件
|
|
os.Remove(filename)
|
|
//删除缩略图文件
|
|
os.Remove(thumbTrueName)
|
|
//返回
|
|
c.JSON(200, gin.H{
|
|
"success": true,
|
|
"url": downUrl,
|
|
"thumbContent": sourceString,
|
|
"size": size,
|
|
})
|
|
} else {
|
|
//删除原始文件
|
|
os.Remove(filename)
|
|
c.JSON(200, gin.H{
|
|
"success": true,
|
|
"url": downUrl,
|
|
"size": size,
|
|
})
|
|
}
|
|
})
|
|
// 监听并在 0.0.0.0:8010 上启动服务
|
|
r.Run(":" + ConfigUtil.ServerPort)
|
|
}
|