This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch feat/1.4.2/file
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git


The following commit(s) were added to refs/heads/feat/1.4.2/file by this push:
     new d085c836 refactor(uploader): improve file upload and download 
functionality subpath constants - Adjust file download
d085c836 is described below

commit d085c836249b77379b5a0467bd3383ddea18c00f
Author: LinkinStars <[email protected]>
AuthorDate: Wed Dec 4 14:55:19 2024 +0800

    refactor(uploader): improve file upload and download functionality subpath 
constants
    - Adjust file download
---
 internal/base/constant/upload.go    |  9 +++++++
 internal/router/static_router.go    | 28 +++++++++++-----------
 internal/service/uploader/upload.go | 48 ++++++++++++++++---------------------
 3 files changed, 44 insertions(+), 41 deletions(-)

diff --git a/internal/base/constant/upload.go b/internal/base/constant/upload.go
new file mode 100644
index 00000000..5a884ae7
--- /dev/null
+++ b/internal/base/constant/upload.go
@@ -0,0 +1,9 @@
+package constant
+
+const (
+       AvatarSubPath      = "avatar"
+       AvatarThumbSubPath = "avatar_thumb"
+       PostSubPath        = "post"
+       BrandingSubPath    = "branding"
+       FilesPostSubPath   = "files/post"
+)
diff --git a/internal/router/static_router.go b/internal/router/static_router.go
index 3fbb12da..71457f49 100644
--- a/internal/router/static_router.go
+++ b/internal/router/static_router.go
@@ -20,6 +20,7 @@
 package router
 
 import (
+       "github.com/apache/incubator-answer/internal/base/constant"
        "github.com/apache/incubator-answer/internal/service/service_config"
        "github.com/gin-gonic/gin"
        "path/filepath"
@@ -40,20 +41,19 @@ func NewStaticRouter(serviceConfig 
*service_config.ServiceConfig) *StaticRouter
 
 // RegisterStaticRouter register static api router
 func (a *StaticRouter) RegisterStaticRouter(r *gin.RouterGroup) {
-       r.Static("/uploads", a.serviceConfig.UploadPath)
-
-       r.GET("/download/*filepath", func(c *gin.Context) {
-               // The filePath such as /download/hash/123.png
+       r.Static("/uploads/"+constant.AvatarSubPath, 
filepath.Join(a.serviceConfig.UploadPath, constant.AvatarSubPath))
+       r.Static("/uploads/"+constant.AvatarThumbSubPath, 
filepath.Join(a.serviceConfig.UploadPath, constant.AvatarThumbSubPath))
+       r.Static("/uploads/"+constant.PostSubPath, 
filepath.Join(a.serviceConfig.UploadPath, constant.PostSubPath))
+       r.Static("/uploads/"+constant.BrandingSubPath, 
filepath.Join(a.serviceConfig.UploadPath, constant.BrandingSubPath))
+       r.GET("/uploads/"+constant.FilesPostSubPath+"/*filepath", func(c 
*gin.Context) {
+               // The filepath such as hash/123.pdf
                filePath := c.Param("filepath")
-               // The download filename is 123.png
-               downloadFilename := filepath.Base(filePath)
-
-               // After trimming, the downloadLink is /uploads/hash
-               downloadLink := strings.TrimSuffix(filePath, 
"/"+downloadFilename)
-               // After add the extension, the downloadLink is 
/uploads/hash.png
-               downloadLink += filepath.Ext(downloadFilename)
-
-               downloadLink = filepath.Join(a.serviceConfig.UploadPath, 
downloadLink)
-               c.FileAttachment(downloadLink, downloadFilename)
+               // The original filename is 123.pdf
+               originalFilename := filepath.Base(filePath)
+               // The real filename is hash.pdf
+               realFilename := strings.TrimSuffix(filePath, 
"/"+originalFilename) + filepath.Ext(originalFilename)
+               // The file local path is /uploads/files/post/hash.pdf
+               fileLocalPath := filepath.Join(a.serviceConfig.UploadPath, 
constant.FilesPostSubPath, realFilename)
+               c.FileAttachment(fileLocalPath, originalFilename)
        })
 }
diff --git a/internal/service/uploader/upload.go 
b/internal/service/uploader/upload.go
index 6baf00d1..8530d17f 100644
--- a/internal/service/uploader/upload.go
+++ b/internal/service/uploader/upload.go
@@ -31,6 +31,7 @@ import (
        "path/filepath"
        "strings"
 
+       "github.com/apache/incubator-answer/internal/base/constant"
        "github.com/apache/incubator-answer/internal/base/reason"
        "github.com/apache/incubator-answer/internal/service/service_config"
        "github.com/apache/incubator-answer/internal/service/siteinfo_common"
@@ -45,19 +46,13 @@ import (
        "github.com/segmentfault/pacman/log"
 )
 
-const (
-       avatarSubPath      = "avatar"
-       avatarThumbSubPath = "avatar_thumb"
-       postSubPath        = "post"
-       brandingSubPath    = "branding"
-)
-
 var (
        subPathList = []string{
-               avatarSubPath,
-               avatarThumbSubPath,
-               postSubPath,
-               brandingSubPath,
+               constant.AvatarSubPath,
+               constant.AvatarThumbSubPath,
+               constant.PostSubPath,
+               constant.BrandingSubPath,
+               constant.FilesPostSubPath,
        }
        supportedThumbFileExtMapping = map[string]imaging.Format{
                ".jpg":  imaging.JPEG,
@@ -123,7 +118,7 @@ func (us *uploaderService) UploadAvatarFile(ctx 
*gin.Context) (url string, err e
        }
 
        newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt)
-       avatarFilePath := path.Join(avatarSubPath, newFilename)
+       avatarFilePath := path.Join(constant.AvatarSubPath, newFilename)
        return us.uploadImageFile(ctx, fileHeader, avatarFilePath)
 }
 
@@ -131,19 +126,19 @@ func (us *uploaderService) AvatarThumbFile(ctx 
*gin.Context, fileName string, si
        fileSuffix := path.Ext(fileName)
        if _, ok := supportedThumbFileExtMapping[fileSuffix]; !ok {
                // if file type is not supported, return original file
-               return path.Join(us.serviceConfig.UploadPath, avatarSubPath, 
fileName), nil
+               return path.Join(us.serviceConfig.UploadPath, 
constant.AvatarSubPath, fileName), nil
        }
        if size > 1024 {
                size = 1024
        }
 
        thumbFileName := fmt.Sprintf("%d_%d@%s", size, size, fileName)
-       thumbFilePath := fmt.Sprintf("%s/%s/%s", us.serviceConfig.UploadPath, 
avatarThumbSubPath, thumbFileName)
+       thumbFilePath := fmt.Sprintf("%s/%s/%s", us.serviceConfig.UploadPath, 
constant.AvatarThumbSubPath, thumbFileName)
        avatarFile, err := os.ReadFile(thumbFilePath)
        if err == nil {
                return thumbFilePath, nil
        }
-       filePath := fmt.Sprintf("%s/%s/%s", us.serviceConfig.UploadPath, 
avatarSubPath, fileName)
+       filePath := fmt.Sprintf("%s/%s/%s", us.serviceConfig.UploadPath, 
constant.AvatarSubPath, fileName)
        avatarFile, err = os.ReadFile(filePath)
        if err != nil {
                return "", 
errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
@@ -160,11 +155,11 @@ func (us *uploaderService) AvatarThumbFile(ctx 
*gin.Context, fileName string, si
                return "", 
errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
        }
 
-       if err = dir.CreateDirIfNotExist(path.Join(us.serviceConfig.UploadPath, 
avatarThumbSubPath)); err != nil {
+       if err = dir.CreateDirIfNotExist(path.Join(us.serviceConfig.UploadPath, 
constant.AvatarThumbSubPath)); err != nil {
                return "", 
errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
        }
 
-       avatarFilePath := path.Join(avatarThumbSubPath, thumbFileName)
+       avatarFilePath := path.Join(constant.AvatarThumbSubPath, thumbFileName)
        saveFilePath := path.Join(us.serviceConfig.UploadPath, avatarFilePath)
        out, err := os.Create(saveFilePath)
        if err != nil {
@@ -206,7 +201,7 @@ func (us *uploaderService) UploadPostFile(ctx *gin.Context) 
(
 
        fileExt := strings.ToLower(path.Ext(fileHeader.Filename))
        newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt)
-       avatarFilePath := path.Join(postSubPath, newFilename)
+       avatarFilePath := path.Join(constant.PostSubPath, newFilename)
        return us.uploadImageFile(ctx, fileHeader, avatarFilePath)
 }
 
@@ -237,7 +232,7 @@ func (us *uploaderService) UploadPostAttachment(ctx 
*gin.Context) (
 
        fileExt := strings.ToLower(path.Ext(fileHeader.Filename))
        newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt)
-       avatarFilePath := path.Join(postSubPath, newFilename)
+       avatarFilePath := path.Join(constant.FilesPostSubPath, newFilename)
        return us.uploadAttachmentFile(ctx, fileHeader, fileHeader.Filename, 
avatarFilePath)
 }
 
@@ -268,7 +263,7 @@ func (us *uploaderService) UploadBrandingFile(ctx 
*gin.Context) (
        }
 
        newFilename := fmt.Sprintf("%s%s", uid.IDStr12(), fileExt)
-       avatarFilePath := path.Join(brandingSubPath, newFilename)
+       avatarFilePath := path.Join(constant.BrandingSubPath, newFilename)
        return us.uploadImageFile(ctx, fileHeader, avatarFilePath)
 }
 
@@ -316,15 +311,14 @@ func (us *uploaderService) uploadAttachmentFile(ctx 
*gin.Context, file *multipar
                return "", 
errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
        }
 
-       // The original filename is 123.png
-       // The local saved path is /UploadPath/hash.png
-       // The download link wil be /download/hash/123.png.
-       // When downloading, the download link will be redirect to the local 
saved path. And the download filename will be 123.png.
-       ext := filepath.Ext(fileSubPath)
        // Need url encode the original filename. Because the filename may 
contain special characters that conflict with the markdown syntax.
        originalFilename = url.QueryEscape(originalFilename)
-       downloadPath := strings.TrimSuffix(fileSubPath, ext) + "/" + 
originalFilename
-       downloadUrl = fmt.Sprintf("%s/download/%s", siteGeneral.SiteUrl, 
downloadPath)
+
+       // The original filename is 123.pdf
+       // The local saved path is /UploadPath/hash.pdf
+       // When downloading, the download link will be redirect to the local 
saved path. And the download filename will be 123.png.
+       downloadPath := strings.TrimSuffix(fileSubPath, 
filepath.Ext(fileSubPath)) + "/" + originalFilename
+       downloadUrl = fmt.Sprintf("%s/uploads/%s", siteGeneral.SiteUrl, 
downloadPath)
        return downloadUrl, nil
 }
 

Reply via email to