feat: add oss service factory implements, add support for setting custom upload handler, localstorage and minio oss

This commit is contained in:
RockYang
2023-08-20 22:29:08 +08:00
parent 89b30bcf58
commit 46f96e94ec
12 changed files with 227 additions and 100 deletions

View File

@@ -4,6 +4,7 @@ import (
"chatplus/core"
"chatplus/core/types"
"chatplus/service/function"
"chatplus/service/oss"
"chatplus/store"
"chatplus/store/model"
"chatplus/utils"
@@ -36,23 +37,23 @@ type Image struct {
type MidJourneyHandler struct {
BaseHandler
leveldb *store.LevelDB
db *gorm.DB
mjFunc function.FuncMidJourney
//minio *service.MinioService
leveldb *store.LevelDB
db *gorm.DB
mjFunc function.FuncMidJourney
uploaderManager *oss.UploaderManager
}
func NewMidJourneyHandler(
app *core.AppServer,
leveldb *store.LevelDB,
db *gorm.DB,
//minio *service.MinioService,
manager *oss.UploaderManager,
functions map[string]function.Function) *MidJourneyHandler {
h := MidJourneyHandler{
leveldb: leveldb,
db: db,
//minio: minio,
mjFunc: functions[types.FuncMidJourney].(function.FuncMidJourney)}
leveldb: leveldb,
db: db,
uploaderManager: manager,
mjFunc: functions[types.FuncMidJourney].(function.FuncMidJourney)}
h.App = app
return &h
}
@@ -98,22 +99,15 @@ func (h *MidJourneyHandler) Notify(c *gin.Context) {
resp.SUCCESS(c)
return
}
// TODO: 下载本地或者 OSS提供可配置的选项
// 下载图片到本地服务器
filePath, err := utils.GenUploadPath(h.App.Config.StaticDir, data.Image.Filename)
if err != nil {
logger.Error("error with generate image dir: ", err)
resp.SUCCESS(c)
return
}
err = utils.DownloadFile(data.Image.URL, filePath, h.App.Config.ProxyURL)
// download image
imgURL, err := h.uploaderManager.GetActiveService().PutImg(data.Image.URL)
if err != nil {
logger.Error("error with download image: ", err)
resp.SUCCESS(c)
return
}
data.Image.URL = utils.GenUploadUrl(h.App.Config.StaticDir, h.App.Config.StaticUrl, filePath)
data.Image.URL = imgURL
message := model.HistoryMessage{
UserId: task.UserId,
ChatId: task.ChatId,

View File

@@ -2,7 +2,7 @@ package handler
import (
"chatplus/core"
"chatplus/utils"
"chatplus/service/oss"
"chatplus/utils/resp"
"fmt"
"github.com/gin-gonic/gin"
@@ -11,33 +11,22 @@ import (
type UploadHandler struct {
BaseHandler
db *gorm.DB
db *gorm.DB
uploaderManager *oss.UploaderManager
}
func NewUploadHandler(app *core.AppServer, db *gorm.DB) *UploadHandler {
handler := &UploadHandler{db: db}
func NewUploadHandler(app *core.AppServer, db *gorm.DB, manager *oss.UploaderManager) *UploadHandler {
handler := &UploadHandler{db: db, uploaderManager: manager}
handler.App = app
return handler
}
func (h *UploadHandler) Upload(c *gin.Context) {
file, err := c.FormFile("file")
fileURL, err := h.uploaderManager.GetActiveService().PutFile(c)
if err != nil {
resp.ERROR(c, fmt.Sprintf("文件上传失败: %s", err.Error()))
return
}
filePath, err := utils.GenUploadPath(h.App.Config.StaticDir, file.Filename)
if err != nil {
resp.ERROR(c, fmt.Sprintf("文件上传失败: %s", err.Error()))
return
}
// 将文件保存到指定路径
err = c.SaveUploadedFile(file, filePath)
if err != nil {
resp.ERROR(c, fmt.Sprintf("文件保存失败: %s", err.Error()))
return
}
resp.SUCCESS(c, utils.GenUploadUrl(h.App.Config.StaticDir, h.App.Config.StaticUrl, filePath))
resp.SUCCESS(c, fileURL)
}