2023-06-27 12:11:55 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"chatplus/core"
|
2023-08-20 22:29:08 +08:00
|
|
|
"chatplus/service/oss"
|
2023-06-27 12:11:55 +08:00
|
|
|
"chatplus/utils/resp"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UploadHandler struct {
|
|
|
|
BaseHandler
|
2023-08-20 22:29:08 +08:00
|
|
|
db *gorm.DB
|
|
|
|
uploaderManager *oss.UploaderManager
|
2023-06-27 12:11:55 +08:00
|
|
|
}
|
|
|
|
|
2023-08-20 22:29:08 +08:00
|
|
|
func NewUploadHandler(app *core.AppServer, db *gorm.DB, manager *oss.UploaderManager) *UploadHandler {
|
|
|
|
handler := &UploadHandler{db: db, uploaderManager: manager}
|
2023-06-27 12:11:55 +08:00
|
|
|
handler.App = app
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UploadHandler) Upload(c *gin.Context) {
|
2023-09-08 06:46:24 +08:00
|
|
|
fileURL, err := h.uploaderManager.GetUploadHandler().PutFile(c, "file")
|
2023-06-27 12:11:55 +08:00
|
|
|
if err != nil {
|
2023-08-21 10:55:49 +08:00
|
|
|
resp.ERROR(c, err.Error())
|
2023-06-27 12:11:55 +08:00
|
|
|
return
|
|
|
|
}
|
2023-09-08 06:46:24 +08:00
|
|
|
|
2023-08-20 22:29:08 +08:00
|
|
|
resp.SUCCESS(c, fileURL)
|
2023-06-27 12:11:55 +08:00
|
|
|
}
|