opt: Only create actived upload service

This commit is contained in:
RockYang 2023-09-08 06:46:24 +08:00
parent f7fbaa534d
commit b676f80110
3 changed files with 23 additions and 22 deletions

View File

@ -100,7 +100,7 @@ func (h *MidJourneyHandler) Notify(c *gin.Context) {
return return
} }
// download image // download image
imgURL, err := h.uploaderManager.GetActiveService().PutImg(data.Image.URL) imgURL, err := h.uploaderManager.GetUploadHandler().PutImg(data.Image.URL)
if err != nil { if err != nil {
logger.Error("error with download image: ", err) logger.Error("error with download image: ", err)
resp.SUCCESS(c) resp.SUCCESS(c)

View File

@ -21,7 +21,7 @@ func NewUploadHandler(app *core.AppServer, db *gorm.DB, manager *oss.UploaderMan
} }
func (h *UploadHandler) Upload(c *gin.Context) { func (h *UploadHandler) Upload(c *gin.Context) {
fileURL, err := h.uploaderManager.GetActiveService().PutFile(c, "file") fileURL, err := h.uploaderManager.GetUploadHandler().PutFile(c, "file")
if err != nil { if err != nil {
resp.ERROR(c, err.Error()) resp.ERROR(c, err.Error())
return return

View File

@ -6,8 +6,7 @@ import (
) )
type UploaderManager struct { type UploaderManager struct {
active string handler Uploader
uploadServices map[string]Uploader
} }
const Local = "LOCAL" const Local = "LOCAL"
@ -15,27 +14,29 @@ const Minio = "MINIO"
const QiNiu = "QINIU" const QiNiu = "QINIU"
func NewUploaderManager(config *types.AppConfig) (*UploaderManager, error) { func NewUploaderManager(config *types.AppConfig) (*UploaderManager, error) {
services := make(map[string]Uploader)
if config.OSS.Minio.AccessKey != "" {
minioService, err := NewMinioService(config)
if err != nil {
return nil, err
}
services[Minio] = minioService
}
if config.OSS.Local.BasePath != "" {
services[Local] = NewLocalStorageService(config)
}
if config.OSS.QiNiu.AccessKey != "" {
services[QiNiu] = NewQiNiuService(config)
}
active := Local active := Local
if config.OSS.Active != "" { if config.OSS.Active != "" {
active = strings.ToUpper(config.OSS.Active) active = strings.ToUpper(config.OSS.Active)
} }
return &UploaderManager{uploadServices: services, active: active}, nil var handler Uploader
switch active {
case Local:
handler = NewLocalStorageService(config)
break
case Minio:
service, err := NewMinioService(config)
if err != nil {
return nil, err
}
handler = service
break
case QiNiu:
handler = NewQiNiuService(config)
}
return &UploaderManager{handler: handler}, nil
} }
func (m *UploaderManager) GetActiveService() Uploader { func (m *UploaderManager) GetUploadHandler() Uploader {
return m.uploadServices[m.active] return m.handler
} }