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.
refactor(upload): extract uploader service interface
LinkinStars7662e9a5523e
diff --git a/internal/base/middleware/avatar.go b/internal/base/middleware/avatar.goindex d38e76c9..82e6ceab 100644--- a/internal/base/middleware/avatar.go+++ b/internal/base/middleware/avatar.go@@ -17,12 +17,12 @@ import (type AvatarMiddleware struct {serviceConfig *service_config.ServiceConfig- uploaderService *uploader.UploaderService+ uploaderService uploader.UploaderService}// NewAvatarMiddleware new auth user middlewarefunc NewAvatarMiddleware(serviceConfig *service_config.ServiceConfig,- uploaderService *uploader.UploaderService,+ uploaderService uploader.UploaderService,) *AvatarMiddleware {return &AvatarMiddleware{serviceConfig: serviceConfig,diff --git a/internal/controller/upload_controller.go b/internal/controller/upload_controller.goindex 9429eba5..3154cc85 100644--- a/internal/controller/upload_controller.go+++ b/internal/controller/upload_controller.go@@ -21,11 +21,11 @@ const (// UploadController upload controllertype UploadController struct {- uploaderService *uploader.UploaderService+ uploaderService uploader.UploaderService}// NewUploadController new controller-func NewUploadController(uploaderService *uploader.UploaderService) *UploadController {+func NewUploadController(uploaderService uploader.UploaderService) *UploadController {return &UploadController{uploaderService: uploaderService,}diff --git a/internal/controller/user_controller.go b/internal/controller/user_controller.goindex 88e57ca3..51fbc4ea 100644--- a/internal/controller/user_controller.go+++ b/internal/controller/user_controller.go@@ -24,7 +24,7 @@ type UserController struct {userService *service.UserServiceauthService *auth.AuthServiceactionService *action.CaptchaService- uploaderService *uploader.UploaderService+ uploaderService uploader.UploaderServiceemailService *export.EmailServicesiteInfoCommonService *siteinfo_common.SiteInfoCommonService}@@ -35,7 +35,7 @@ func NewUserController(userService *service.UserService,actionService *action.CaptchaService,emailService *export.EmailService,- uploaderService *uploader.UploaderService,+ uploaderService uploader.UploaderService,siteInfoCommonService *siteinfo_common.SiteInfoCommonService,) *UserController {return &UserController{diff --git a/internal/service/uploader/upload.go b/internal/service/uploader/upload.goindex 3de12ff2..3b978521 100644--- a/internal/service/uploader/upload.go+++ b/internal/service/uploader/upload.go@@ -51,29 +51,36 @@ var (})-// UploaderService user service-type UploaderService struct {+type UploaderService interface {+ UploadAvatarFile(ctx *gin.Context) (url string, err error)+ AvatarThumbFile(ctx *gin.Context, uploadPath, fileName string, size int) (avatarFile []byte, err error)+ UploadPostFile(ctx *gin.Context) (url string, err error)+ UploadBrandingFile(ctx *gin.Context) (url string, err error)+}++// uploaderService uploader service+type uploaderService struct {serviceConfig *service_config.ServiceConfigsiteInfoService *siteinfo_common.SiteInfoCommonService}// NewUploaderService new upload servicefunc NewUploaderService(serviceConfig *service_config.ServiceConfig,- siteInfoService *siteinfo_common.SiteInfoCommonService) *UploaderService {+ siteInfoService *siteinfo_common.SiteInfoCommonService) UploaderService {for _, subPath := range subPathList {err := dir.CreateDirIfNotExist(filepath.Join(serviceConfig.UploadPath, subPath))if err != nil {panic(err)}}- return &UploaderService{+ return &uploaderService{serviceConfig: serviceConfig,siteInfoService: siteInfoService,}}// UploadAvatarFile upload avatar file-func (us *UploaderService) UploadAvatarFile(ctx *gin.Context) (url string, err error) {+func (us *uploaderService) UploadAvatarFile(ctx *gin.Context) (url string, err error) {url, err = us.tryToUploadByPlugin(ctx, plugin.UserAvatar)if err != nil {return "", err@@ -98,7 +105,7 @@ func (us *UploaderService) UploadAvatarFile(ctx *gin.Context) (url string, err ereturn us.uploadFile(ctx, file, avatarFilePath)}-func (us *UploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileName string, size int) (+func (us *uploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileName string, size int) (avatarfile []byte, err error) {if size > 1024 {size = 1024@@ -151,7 +158,7 @@ func (us *UploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileNamreturn buf.Bytes(), nil}-func (us *UploaderService) UploadPostFile(ctx *gin.Context) (+func (us *uploaderService) UploadPostFile(ctx *gin.Context) (url string, err error) {url, err = us.tryToUploadByPlugin(ctx, plugin.UserAvatar)if err != nil {@@ -177,7 +184,7 @@ func (us *UploaderService) UploadPostFile(ctx *gin.Context) (return us.uploadFile(ctx, file, avatarFilePath)}-func (us *UploaderService) UploadBrandingFile(ctx *gin.Context) (+func (us *uploaderService) UploadBrandingFile(ctx *gin.Context) (url string, err error) {url, err = us.tryToUploadByPlugin(ctx, plugin.UserAvatar)if err != nil {@@ -204,7 +211,7 @@ func (us *UploaderService) UploadBrandingFile(ctx *gin.Context) (return us.uploadFile(ctx, file, avatarFilePath)}-func (us *UploaderService) uploadFile(ctx *gin.Context, file *multipart.FileHeader, fileSubPath string) (+func (us *uploaderService) uploadFile(ctx *gin.Context, file *multipart.FileHeader, fileSubPath string) (url string, err error) {siteGeneral, err := us.siteInfoService.GetSiteGeneral(ctx)if err != nil {@@ -230,7 +237,7 @@ func (us *UploaderService) uploadFile(ctx *gin.Context, file *multipart.FileHeadreturn url, nil}-func (us *UploaderService) tryToUploadByPlugin(ctx *gin.Context, source plugin.UploadSource) (+func (us *uploaderService) tryToUploadByPlugin(ctx *gin.Context, source plugin.UploadSource) (url string, err error) {_ = plugin.CallStorage(func(fn plugin.Storage) error {resp := fn.UploadFile(ctx, source)