master
huanghai 5 years ago
parent 842a8b0e2b
commit 18e6aaffb3

@ -3,11 +3,15 @@ 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"
)
@ -26,6 +30,8 @@ func Routers(r *gin.RouterGroup) {
rr.GET("/presignedUrl", presignedUrl)
//获取下载URL地址
rr.GET("/getUrl", getUrl)
//显示缩略图
rr.GET("/scale", scale)
return
}
@ -43,8 +49,86 @@ func init() {
}
}
// Minio 图片永久访问的问题处理
// https://blog.csdn.net/kylinregister/article/details/88910556
/**
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
@ -63,7 +147,6 @@ func getUrl(c *gin.Context) {
c.String(http.StatusOK, presignedURL.String())
}
/**

@ -7,7 +7,7 @@ backupPrefix = baseServiceDbBackup
remainDays = 15
[minio]
endpoint = 10.10.14.242:9000
endpoint = 10.10.14.241:9000
ak = 5D2Q3FZ04LW4DIDW7R22
sk = C98uUVskiHjPaVmjTBPFdE6rh+wOJKtQIn1wvqvv
bucketName = dsmin
bucketName = dsideal

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
@ -126,3 +127,13 @@ func GetUUID() string {
u2 := strings.ToUpper(uuid.NewV4().String())
return u2
}
/**
2020-06-03
*/
func ConvertStringToInt(s string) int {
int, _ := strconv.Atoi(s)
return int
}

Loading…
Cancel
Save