A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat(upload): support upload gif image
LinkinStarsad6e2824162d
diff --git a/internal/base/middleware/avatar.go b/internal/base/middleware/avatar.goindex dadb93c3..88671842 100644--- a/internal/base/middleware/avatar.go+++ b/internal/base/middleware/avatar.go@@ -61,15 +61,12 @@ func (am *AvatarMiddleware) AvatarThumb() gin.HandlerFunc {return} else {- uUrl, err := url.Parse(uri)+ urlInfo, err := url.Parse(uri)if err != nil {ctx.Next()return}- _, urlfileName := filepath.Split(uUrl.Path)- uploadPath := am.serviceConfig.UploadPath- filePath := fmt.Sprintf("%s/%s", uploadPath, urlfileName)- ext := strings.ToLower(path.Ext(filePath)[1:])+ ext := strings.TrimPrefix(filepath.Ext(urlInfo.Path), ".")ctx.Header("content-type", fmt.Sprintf("image/%s", ext))}ctx.Next()diff --git a/internal/service/uploader/upload.go b/internal/service/uploader/upload.goindex 19bf6322..3dc2fa0f 100644--- a/internal/service/uploader/upload.go+++ b/internal/service/uploader/upload.go@@ -4,7 +4,6 @@ import ("bytes""fmt""io"- "io/ioutil""mime/multipart""net/http""os"@@ -44,7 +43,7 @@ var (".jpg": imaging.JPEG,".jpeg": imaging.JPEG,".png": imaging.PNG,- //".gif": imaging.GIF,+ ".gif": imaging.GIF,//".tif": imaging.TIFF,//".tiff": imaging.TIFF,//".bmp": imaging.BMP,@@ -112,50 +111,49 @@ func (us *uploaderService) AvatarThumbFile(ctx *gin.Context, fileName string, sithumbFileName := fmt.Sprintf("%d_%d@%s", size, size, fileName)thumbFilePath := fmt.Sprintf("%s/%s/%s", us.serviceConfig.UploadPath, avatarThumbSubPath, thumbFileName)- avatarfile, err := os.ReadFile(thumbFilePath)+ avatarFile, err := os.ReadFile(thumbFilePath)if err == nil {return thumbFilePath, nil}filePath := fmt.Sprintf("%s/avatar/%s", us.serviceConfig.UploadPath, fileName)- avatarfile, err = os.ReadFile(filePath)+ avatarFile, err = os.ReadFile(filePath)if err != nil {return "", errors.InternalServer(reason.UnknownError).WithError(err).WithStack()}- reader := bytes.NewReader(avatarfile)+ reader := bytes.NewReader(avatarFile)img, err := imaging.Decode(reader)if err != nil {return "", errors.InternalServer(reason.UnknownError).WithError(err).WithStack()}- new_image := imaging.Fill(img, size, size, imaging.Center, imaging.Linear)- var buf bytes.Buffer- fileSuffix := path.Ext(fileName)-- _, ok := FormatExts[fileSuffix]- if !ok {- return "", fmt.Errorf("img extension not exist")+ fileSuffix := path.Ext(fileName)+ if _, ok := FormatExts[fileSuffix]; !ok {+ return "", fmt.Errorf("unsupported image format: %s", fileSuffix)}- err = imaging.Encode(&buf, new_image, FormatExts[fileSuffix])- if err != nil {++ var buf bytes.Buffer+ newImage := imaging.Fill(img, size, size, imaging.Center, imaging.Linear)+ if err = imaging.Encode(&buf, newImage, FormatExts[fileSuffix]); err != nil {return "", errors.InternalServer(reason.UnknownError).WithError(err).WithStack()}- thumbReader := bytes.NewReader(buf.Bytes())- err = dir.CreateDirIfNotExist(path.Join(us.serviceConfig.UploadPath, avatarThumbSubPath))- if err != nil {++ if err = dir.CreateDirIfNotExist(path.Join(us.serviceConfig.UploadPath, avatarThumbSubPath)); err != nil {return "", errors.InternalServer(reason.UnknownError).WithError(err).WithStack()}+avatarFilePath := path.Join(avatarThumbSubPath, thumbFileName)- savefilePath := path.Join(us.serviceConfig.UploadPath, avatarFilePath)- out, err := os.Create(savefilePath)+ saveFilePath := path.Join(us.serviceConfig.UploadPath, avatarFilePath)+ out, err := os.Create(saveFilePath)if err != nil {return "", errors.InternalServer(reason.UnknownError).WithError(err).WithStack()}defer out.Close()- _, err = io.Copy(out, thumbReader)- if err != nil {++ thumbReader := bytes.NewReader(buf.Bytes())+ if _, err = io.Copy(out, thumbReader); err != nil {return "", errors.InternalServer(reason.UnknownError).WithError(err).WithStack()}- return savefilePath, nil+ return saveFilePath, nil}func (us *uploaderService) UploadPostFile(ctx *gin.Context) (@@ -227,7 +225,10 @@ func (us *uploaderService) uploadFile(ctx *gin.Context, file *multipart.FileHeadreturn "", errors.InternalServer(reason.UnknownError).WithError(err).WithStack()}defer src.Close()- Dexif(filePath, filePath)++ if err := removeExif(filePath); err != nil {+ log.Error(err)+ }if !checker.IsSupportedImageFile(src, filepath.Ext(fileSubPath)) {return "", errors.BadRequest(reason.UploadFileUnsupportedFileFormat)@@ -252,18 +253,20 @@ func (us *uploaderService) tryToUploadByPlugin(ctx *gin.Context, source plugin.Ureturn url, err}-func Dexif(filepath string, destpath string) error {- img, err := ioutil.ReadFile(filepath)- if err != nil {- return err+// removeExif remove exif+// only support jpg/jpeg/png+func removeExif(path string) error {+ ext := strings.ToUpper(strings.TrimPrefix(filepath.Ext(path), "."))+ if ext != "JPEG" && ext != "JPG" && ext != "PNG" {+ return nil}- noExifBytes, err := exifremove.Remove(img)+ img, err := os.ReadFile(path)if err != nil {return err}- err = os.WriteFile(destpath, noExifBytes, 0644)+ noExifBytes, err := exifremove.Remove(img)if err != nil {return err}- return nil+ return os.WriteFile(path, noExifBytes, 0644)}diff --git a/pkg/checker/file_type.go b/pkg/checker/file_type.goindex 71f9f2a5..24845ab5 100644--- a/pkg/checker/file_type.go+++ b/pkg/checker/file_type.go@@ -11,7 +11,6 @@ import (// IsSupportedImageFile currently answers support image type is `image/jpeg,image/jpg,image/png, image/gif`func IsSupportedImageFile(file io.Reader, ext string) bool {-ext = strings.TrimPrefix(ext, ".")var err errorswitch strings.ToUpper(ext) {diff --git a/plugin/storage.go b/plugin/storage.goindex 51f4bb84..0d046623 100644--- a/plugin/storage.go+++ b/plugin/storage.go@@ -19,6 +19,7 @@ var (".jpg": true,".jpeg": true,".png": true,+ ".gif": true,},AdminBranding: {".jpg": true,