From 8b399dbe66a019d33a55889624f074a93f4f9332 Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Fri, 28 Oct 2022 10:29:26 +0800 Subject: [PATCH] update img upload --- internal/base/middleware/avatar.go | 33 ++++++++++++----------------- internal/service/uploader/upload.go | 32 +++++++++++++++------------- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/internal/base/middleware/avatar.go b/internal/base/middleware/avatar.go index f2bee0b4..402ded09 100644 --- a/internal/base/middleware/avatar.go +++ b/internal/base/middleware/avatar.go @@ -30,13 +30,12 @@ func NewAvatarMiddleware(serviceConfig *service_config.ServiceConfig, func (am *AvatarMiddleware) AvatarThumb() gin.HandlerFunc { return func(ctx *gin.Context) { - //?width=100&height=100 - wstr := ctx.Query("width") - hstr := ctx.Query("height") - w := converter.StringToInt(wstr) - h := converter.StringToInt(hstr) u := ctx.Request.RequestURI if strings.Contains(u, "/uploads/avatar/") { + wstr := ctx.Query("width") + hstr := ctx.Query("height") + w := converter.StringToInt(wstr) + h := converter.StringToInt(hstr) uUrl, err := url.Parse(u) if err != nil { ctx.Next() @@ -45,25 +44,19 @@ func (am *AvatarMiddleware) AvatarThumb() gin.HandlerFunc { _, urlfileName := filepath.Split(uUrl.Path) uploadPath := am.serviceConfig.UploadPath filePath := fmt.Sprintf("%s/avatar/%s", uploadPath, urlfileName) + var avatarfile []byte if w == 0 && h == 0 { - avatarfile, err := ioutil.ReadFile(filePath) - if err != nil { - ctx.Next() - return - } - ctx.Writer.WriteString(string(avatarfile)) - ctx.Abort() - return + avatarfile, err = ioutil.ReadFile(filePath) } else { - avatarfile, err := am.uploaderService.AvatarThumbFile(ctx, uploadPath, urlfileName, w, h) - if err != nil { - ctx.Next() - return - } - ctx.Writer.WriteString(string(avatarfile)) - ctx.Abort() + avatarfile, err = am.uploaderService.AvatarThumbFile(ctx, uploadPath, urlfileName, w, h) + } + if err != nil { + ctx.Next() return } + ctx.Writer.WriteString(string(avatarfile)) + ctx.Abort() + return } ctx.Next() diff --git a/internal/service/uploader/upload.go b/internal/service/uploader/upload.go index 3582a76f..1fc094d6 100644 --- a/internal/service/uploader/upload.go +++ b/internal/service/uploader/upload.go @@ -52,6 +52,16 @@ func (us *UploaderService) UploadAvatarFile(ctx *gin.Context, file *multipart.Fi return us.uploadFile(ctx, file, avatarFilePath) } +var FormatExts = map[string]imaging.Format{ + ".jpg": imaging.JPEG, + ".jpeg": imaging.JPEG, + ".png": imaging.PNG, + ".gif": imaging.GIF, + ".tif": imaging.TIFF, + ".tiff": imaging.TIFF, + ".bmp": imaging.BMP, +} + func (us *UploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileName string, w, h int) ( avatarfile []byte, err error) { thumbFileName := fmt.Sprintf("%d_%d@%s", w, h, fileName) @@ -63,26 +73,18 @@ func (us *UploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileNam filePath := fmt.Sprintf("%s/avatar/%s", uploadPath, fileName) avatarfile, err = ioutil.ReadFile(filePath) if err != nil { - return avatarfile, err + return avatarfile, errors.InternalServer(reason.UnknownError).WithError(err).WithStack() } reader := bytes.NewReader(avatarfile) img, err := imaging.Decode(reader) if err != nil { - return avatarfile, err + return avatarfile, errors.InternalServer(reason.UnknownError).WithError(err).WithStack() } new_image := imaging.Fill(img, w, h, imaging.Center, imaging.Linear) var buf bytes.Buffer fileSuffix := path.Ext(fileName) - formatExts := map[string]imaging.Format{ - ".jpg": imaging.JPEG, - ".jpeg": imaging.JPEG, - ".png": imaging.PNG, - ".gif": imaging.GIF, - ".tif": imaging.TIFF, - ".tiff": imaging.TIFF, - ".bmp": imaging.BMP, - } - _, ok := formatExts[fileSuffix] + + _, ok := FormatExts[fileSuffix] if !ok { return avatarfile, fmt.Errorf("img extension not exist") @@ -90,7 +92,7 @@ func (us *UploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileNam err = imaging.Encode(&buf, new_image, formatExts[fileSuffix]) if err != nil { - return avatarfile, err + return avatarfile, errors.InternalServer(reason.UnknownError).WithError(err).WithStack() } thumbReader := bytes.NewReader(buf.Bytes()) dir.CreateDirIfNotExist(path.Join(us.serviceConfig.UploadPath, avatarThumbSubPath)) @@ -98,12 +100,12 @@ func (us *UploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileNam savefilePath := path.Join(us.serviceConfig.UploadPath, avatarFilePath) out, err := os.Create(savefilePath) if err != nil { - return avatarfile, err + return avatarfile, errors.InternalServer(reason.UnknownError).WithError(err).WithStack() } defer out.Close() _, err = io.Copy(out, thumbReader) if err != nil { - return avatarfile, err + return avatarfile, errors.InternalServer(reason.UnknownError).WithError(err).WithStack() } return buf.Bytes(), nil }