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.

139 lines
3.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package FileRelateController
import (
"bytes"
"dsSzxy/Business/FileRelate/FileRelateDao"
"dsSzxy/Utils/CommonUtil"
"dsSzxy/Utils/ConfigUtil"
"dsSzxy/Utils/MinioUtil"
"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"
)
// Routers 模块的路由配置
func Routers(r *gin.RouterGroup) {
rr := r.Group("/fileRelate")
//文件表
rr.POST("/fileUpload", fileUpload)
//rr.POST("/fileRecord", fileRecord)
//rr.POST("/fileUpdate", fileUpdate)
//rr.POST("/fileCopy", fileCopy)
//树型结构
}
//文件上传
func fileUpload(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
}
//文件大小
content, err := ioutil.ReadFile(filename)
if err == nil {
fmt.Println("file size is ", len(content))
}
size := len(content)
// 初使化 minio client对象。
minioClient, err := MinioUtil.GetMinioClient()
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "info": "minio无法连接"})
return
}
// 使用FPutObject上传一个zip文件。
contentType := "application/octet-stream"
_, err = minioClient.FPutObject(ConfigUtil.MinioBucket, trueName[0:2]+"/"+trueName, filename, minio.PutObjectOptions{ContentType: contentType})
if err != nil {
log.Fatalln(err)
}
//记录数据库
identityId := CommonUtil.ConvertStringToInt32(c.PostForm("identity_id"))
personId := CommonUtil.ConvertStringToInt32(c.PostForm("person_id"))
targetGroupId := CommonUtil.ConvertStringToInt32(c.PostForm("target_group_id"))
targetIdentityId := CommonUtil.ConvertStringToInt32(c.PostForm("target_identity_id"))
targetPersonId := CommonUtil.ConvertStringToInt32(c.PostForm("target_person_id"))
systemId := CommonUtil.ConvertStringToInt32(c.PostForm("system_id"))
FileRelateDao.FileRecord(filename, identityId, personId, targetGroupId, targetIdentityId, targetPersonId, systemId, int32(size), ext)
//返回
s := FileRelateDao.GetFileDownLoadUrl(trueName[0:2]+"/"+trueName, file.Filename)
originUrl := ConfigUtil.MinioDownloadPrefix + ConfigUtil.MinioBucket + "/" + trueName[0:2] + "/" + trueName
//判断扩展名是不是图片
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,
"originUrl": originUrl,
"url": s,
"thumbContent": sourceString,
"size": size,
})
} else {
//删除原始文件
os.Remove(filename)
c.JSON(200, gin.H{
"success": true,
"url": s,
"size": size,
})
}
}