mirror of https://gitee.com/answerdev/answer.git
refactor(upload): extract uploader service interface
This commit is contained in:
parent
8431f9ab13
commit
7662e9a552
|
@ -17,12 +17,12 @@ import (
|
|||
|
||||
type AvatarMiddleware struct {
|
||||
serviceConfig *service_config.ServiceConfig
|
||||
uploaderService *uploader.UploaderService
|
||||
uploaderService uploader.UploaderService
|
||||
}
|
||||
|
||||
// NewAvatarMiddleware new auth user middleware
|
||||
func NewAvatarMiddleware(serviceConfig *service_config.ServiceConfig,
|
||||
uploaderService *uploader.UploaderService,
|
||||
uploaderService uploader.UploaderService,
|
||||
) *AvatarMiddleware {
|
||||
return &AvatarMiddleware{
|
||||
serviceConfig: serviceConfig,
|
||||
|
|
|
@ -21,11 +21,11 @@ const (
|
|||
|
||||
// UploadController upload controller
|
||||
type 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,
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ type UserController struct {
|
|||
userService *service.UserService
|
||||
authService *auth.AuthService
|
||||
actionService *action.CaptchaService
|
||||
uploaderService *uploader.UploaderService
|
||||
uploaderService uploader.UploaderService
|
||||
emailService *export.EmailService
|
||||
siteInfoCommonService *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{
|
||||
|
|
|
@ -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.ServiceConfig
|
||||
siteInfoService *siteinfo_common.SiteInfoCommonService
|
||||
}
|
||||
|
||||
// NewUploaderService new upload service
|
||||
func 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 e
|
|||
return 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, fileNam
|
|||
return 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.FileHead
|
|||
return 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)
|
||||
|
|
Loading…
Reference in New Issue