diff --git a/dsSupport/Business/Minio/MinioController.go b/dsSupport/Business/Minio/MinioController.go index 150f0a64..24738589 100644 --- a/dsSupport/Business/Minio/MinioController.go +++ b/dsSupport/Business/Minio/MinioController.go @@ -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()) } - /** 功能:生成上传的签名 作者:黄海 diff --git a/dsSupport/Config/Config.ini b/dsSupport/Config/Config.ini index b8245988..ec067955 100644 --- a/dsSupport/Config/Config.ini +++ b/dsSupport/Config/Config.ini @@ -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 \ No newline at end of file +bucketName = dsideal \ No newline at end of file diff --git a/dsSupport/TempFiles/IMG_20200512_114233.jpg b/dsSupport/TempFiles/IMG_20200512_114233.jpg new file mode 100644 index 00000000..9d9b5e66 Binary files /dev/null and b/dsSupport/TempFiles/IMG_20200512_114233.jpg differ diff --git a/dsSupport/TempFiles/IMG_20200512_114233_800_600.jpg b/dsSupport/TempFiles/IMG_20200512_114233_800_600.jpg new file mode 100644 index 00000000..93870b45 Binary files /dev/null and b/dsSupport/TempFiles/IMG_20200512_114233_800_600.jpg differ diff --git a/dsSupport/Utils/CommonUtil/CommonUtil.go b/dsSupport/Utils/CommonUtil/CommonUtil.go index 9fb18b98..276f7b9f 100644 --- a/dsSupport/Utils/CommonUtil/CommonUtil.go +++ b/dsSupport/Utils/CommonUtil/CommonUtil.go @@ -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 +}