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.

167 lines
4.2 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 Minio
import (
"dsSupport/Utils/CommonUtil"
"dsSupport/Utils/ConfigUtil"
"dsSupport/Utils/FileUtil"
"dsSupport/Utils/ImageUtil"
"github.com/gin-gonic/gin"
"github.com/minio/minio-go/v6"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
// 多集群实验(minio 集群搭建)
// https://www.cnblogs.com/rongfengliang/p/9197315.html
// TODO
// 多台机器,多块硬盘的实验
// 模拟损坏一块硬盘,模拟损坏一台(N台机器
//模块的路由配置
func Routers(r *gin.RouterGroup) {
//注册接口路径
rr := r.Group("/minio")
//上传URL签名
rr.GET("/presignedUrl", presignedUrl)
//获取下载URL地址
rr.GET("/getUrl", getUrl)
//显示缩略图
rr.GET("/scale", scale)
return
}
//声明s3的客户端实例
var s3Client *minio.Client
var err error
var bucketName string
//初始化
func init() {
bucketName = ConfigUtil.MinioBacketName
s3Client, err = minio.New(ConfigUtil.MinioEndpoint, ConfigUtil.MinioAk, ConfigUtil.MinioSk, false)
if err != nil {
log.Fatalln(err)
}
}
/**
功能在minio上支持图片的缩放
作者:黄海
时间: 2020-07-16
用例: http://10.10.24.100:9000/dsSupport/minio/scale?key=IMG_20200512_114233.jpg&w=800&h=600
*/
func scale(c *gin.Context) {
//minio中的图片地址
key := c.Query("key")
//宽
w := c.Query("w")
//高
h := c.Query("h")
//Content_Type
_extMap := make(map[string]string, 0)
_extMap["png"] = "image/png"
_extMap["jpg"] = "image/jpeg"
_extMap["jpeg"] = "image/jpeg"
_extMap["bmp"] = "image/bmp"
//TODO
//w和h的整数范围检查
//在minio中是不是已经存在目标文件如果存在就不用再费劲了~
//(1)下载回来
url := "http://" + ConfigUtil.MinioEndpoint + "/" + ConfigUtil.MinioBacketName + "/" + key
res, err := http.Get(url)
if err != nil {
c.String(http.StatusOK, "文件在minio中没有找到")
return
}
src := CommonUtil.GetCurrentPath() + "/TempFiles/" + key
if FileUtil.Exists(src) {
os.Remove(src)
}
f, err := os.Create(src)
if err != nil {
c.String(http.StatusOK, "在本地创建缓存文件失败!")
return
}
io.Copy(f, res.Body)
//(2)生成缩略图
//打开源文件
fIn, _ := os.Open(src)
defer fIn.Close()
//创建目标文件
dst := strings.Replace(src, ".", "_"+w+"_"+h+".", 1)
fOut, _ := os.Create(dst)
defer fOut.Close()
//缩放
err = ImageUtil.Scale(fIn, fOut, CommonUtil.ConvertStringToInt(w), CommonUtil.ConvertStringToInt(h), 100)
if err != nil {
c.String(http.StatusOK, "生成缩略图失败!")
return
}
//(3)上传回去
file, _ := os.Open(dst)
defer file.Close()
fileStat, _ := file.Stat()
dkey := strings.Replace(key, ".", "_"+w+"_"+h+".", 1)
//需要配置一下图片的header头
extArr := strings.Split(dkey, ".")
ext := extArr[1]
if _, ok := _extMap[ext]; !ok {
//不存在
c.String(http.StatusInternalServerError, "不支持的扩展名!")
return
}
_, err = s3Client.PutObject(ConfigUtil.MinioBacketName, dkey, file, fileStat.Size(),
minio.PutObjectOptions{ContentType: _extMap[ext]})
if err != nil {
c.String(http.StatusInternalServerError, "上传Minio失败")
return
}
//(4)返回结果路径
c.String(http.StatusOK, "http://"+ConfigUtil.MinioEndpoint+"/"+ConfigUtil.MinioBacketName+"/"+dkey)
return
}
/**
功能获取下载URL地址
作者:黄海
时间2020-07-16
http://127.0.0.1:9000/dsSupport/minio/getUrl?name=62922248-63CD-4AA5-AD5C-1AA44ADAC603.mov
*/
func getUrl(c *gin.Context) {
name := c.Query("name")
reqParams := make(url.Values)
reqParams.Set("response-content-disposition", "attachment; filename=\""+name+"\"")
presignedURL, err := s3Client.PresignedGetObject(bucketName, name, time.Duration(1000)*time.Second*60, reqParams)
if err != nil {
log.Fatalln(err)
}
c.String(http.StatusOK, presignedURL.String())
}
/**
功能:生成上传的签名
作者:黄海
时间2020-07-16
*/
func presignedUrl(c *gin.Context) {
//换成uuid文件格式
name := c.Query("name")
ext := strings.Split(name, ".")[1]
name = CommonUtil.GetUUID() + "." + ext
presignedURL, err := s3Client.PresignedPutObject(bucketName, name, time.Duration(1000)*time.Second*300)
if err != nil {
c.String(http.StatusInternalServerError, "发生了严重错误!")
return
}
c.String(http.StatusOK, presignedURL.String())
}