This commit is contained in:
XM-GO
2023-08-22 17:29:11 +08:00
parent 5be575e982
commit 3ac5ecc55a
20 changed files with 88 additions and 128 deletions

View File

@@ -3,18 +3,21 @@ package api
import (
"fmt"
"github.com/XM-GO/PandaKit/biz"
"github.com/XM-GO/PandaKit/oss"
"github.com/XM-GO/PandaKit/restfulx"
"net/http"
"os"
"pandax/pkg/config"
"pandax/pkg/global"
"pandax/pkg/tool"
"path"
"time"
)
type UploadApi struct{}
const filePath = "uploads/file"
// UploadImage 图片上传
// UploadImage 图片上传
func (up *UploadApi) UploadImage(rc *restfulx.ReqCtx) {
_, fileHeader, err := rc.Request.Request.FormFile("file")
@@ -25,6 +28,19 @@ func (up *UploadApi) UploadImage(rc *restfulx.ReqCtx) {
rc.ResData = map[string]string{"fileName": fileName, "filePath": link}
}
// UplaodResOsses 上传文件ResOsses
func (p *UploadApi) UplaodToOss(rc *restfulx.ReqCtx) {
_, handler, _ := rc.Request.Request.FormFile("file")
yunFileTmpPath := "uploads/" + time.Now().Format("2006-01-02") + "/" + handler.Filename
// 读取本地文件。
f, openError := handler.Open()
biz.ErrIsNil(openError, "function file.Open() Failed")
config := global.Conf.Oss
biz.ErrIsNil(NewOss(*config).PutObj(yunFileTmpPath, f), "上传OSS失败")
rc.ResData = fmt.Sprintf("http://%s/%s/%s", config.Endpoint, config.BucketName, yunFileTmpPath)
}
func (up *UploadApi) GetImage(rc *restfulx.ReqCtx) {
actual := path.Join(filePath, restfulx.PathParam(rc, "subpath"))
http.ServeFile(
@@ -39,3 +55,14 @@ func (up *UploadApi) DeleteImage(rc *restfulx.ReqCtx) {
err := os.Remove(fmt.Sprintf("%s/%s", filePath, fileName))
biz.ErrIsNil(err, "文件删除失败")
}
func NewOss(ens config.Oss) oss.Driver {
return oss.NewMiniOss(oss.MiniOConfig{
BucketName: ens.BucketName,
Endpoint: ens.Endpoint,
AccessKeyID: ens.AccessKey,
SecretAccessKey: ens.SecretKey,
UseSSL: ens.UseSSL,
Location: "cn-north-1",
})
}