From b1878227c8381aca55e28b590409be71fdff2d61 Mon Sep 17 00:00:00 2001 From: yxh Date: Tue, 26 Oct 2021 14:49:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/adapter/upload.go | 23 ----------------------- app/common/adapter/upload_local.go | 16 ++++++++++++++++ app/common/adapter/upload_tencent_cos.go | 17 +++++++++++++++++ config/config.toml | 4 +++- library/utils.go | 9 +++++++-- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/app/common/adapter/upload.go b/app/common/adapter/upload.go index 463737b..10d2650 100644 --- a/app/common/adapter/upload.go +++ b/app/common/adapter/upload.go @@ -36,29 +36,6 @@ var ( Upload *upload ) -func init() { - var adp UploadAdapter - switch upType { - case "local": - //使用本地上传 - adp = UploadLocalAdapter{ - UpPath: "/pub_upload/", - UploadPath: g.Cfg().GetString("server.ServerRoot") + "/pub_upload/", - } - case "tencentCOS": - //使用腾讯云COS上传 - adp = UploadTencentCOSAdapter{ - UpPath: g.Cfg().GetString("upload.tencentCOS.UpPath"), - RawUrl: g.Cfg().GetString("upload.tencentCOS.RawUrl"), - SecretID: g.Cfg().GetString("upload.tencentCOS.SecretID"), - SecretKey: g.Cfg().GetString("upload.tencentCOS.SecretKey"), - } - } - Upload = &upload{ - adapter: adp, - } -} - func (u upload) UpImg(file *ghttp.UploadFile) (fileInfo *FileInfo, err error) { return u.adapter.UpImg(file) } diff --git a/app/common/adapter/upload_local.go b/app/common/adapter/upload_local.go index 33fde70..8fdfeb0 100644 --- a/app/common/adapter/upload_local.go +++ b/app/common/adapter/upload_local.go @@ -11,6 +11,7 @@ import ( "gfast/app/common/model" "gfast/app/common/service" "github.com/gogf/gf/errors/gerror" + "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/text/gregex" @@ -18,6 +19,21 @@ import ( "github.com/gogf/gf/util/gconv" ) +func init() { + var adp UploadAdapter + if upType == "local" { + //使用本地上传 + upPath := g.Cfg().GetString("upload.local.UpPath") + adp = UploadLocalAdapter{ + UpPath: "/pub_upload/", + UploadPath: g.Cfg().GetString("server.ServerRoot") + upPath, + } + Upload = &upload{ + adapter: adp, + } + } +} + type UploadLocalAdapter struct { UpPath string UploadPath string diff --git a/app/common/adapter/upload_tencent_cos.go b/app/common/adapter/upload_tencent_cos.go index 21e6337..00017c2 100644 --- a/app/common/adapter/upload_tencent_cos.go +++ b/app/common/adapter/upload_tencent_cos.go @@ -12,6 +12,7 @@ import ( "gfast/app/common/model" "gfast/app/common/service" "github.com/gogf/gf/errors/gerror" + "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/os/gfile" "github.com/gogf/gf/os/gtime" @@ -28,6 +29,22 @@ import ( "strings" ) +func init() { + var adp UploadAdapter + if upType == "tencentCOS" { + //使用腾讯云COS上传 + adp = UploadTencentCOSAdapter{ + UpPath: g.Cfg().GetString("upload.tencentCOS.UpPath"), + RawUrl: g.Cfg().GetString("upload.tencentCOS.RawUrl"), + SecretID: g.Cfg().GetString("upload.tencentCOS.SecretID"), + SecretKey: g.Cfg().GetString("upload.tencentCOS.SecretKey"), + } + Upload = &upload{ + adapter: adp, + } + } +} + type UploadTencentCOSAdapter struct { UpPath string SecretID string diff --git a/config/config.toml b/config/config.toml index 7825293..b9c4af7 100644 --- a/config/config.toml +++ b/config/config.toml @@ -85,4 +85,6 @@ UpPath = "/gfast/" RawUrl = "https://您的cos空间域名.cos.ap-chongqing.myqcloud.com" SecretID = "填写您的SecretID" - SecretKey = "填写您的SecretKey" \ No newline at end of file + SecretKey = "填写您的SecretKey" + [upload.local] #本地上传配置 + UpPath = "/pub_upload/" #上传路径 diff --git a/library/utils.go b/library/utils.go index 26a2c27..4f6f87a 100644 --- a/library/utils.go +++ b/library/utils.go @@ -212,7 +212,9 @@ func GetRealFilesUrl(r *ghttp.Request, path string) (realPath string, err error) //获取附件相对路径 func GetFilesPath(fileUrl string) (path string, err error) { - if !gstr.ContainsI(fileUrl, "http") { + upType := gstr.ToLower(g.Cfg().GetString("upload.type")) + upPath := gstr.Trim(g.Cfg().GetString("upload.local.UpPath"), "/") + if upType != "local" || (upType == "local" && !gstr.ContainsI(fileUrl, upPath)) { path = fileUrl return } @@ -222,7 +224,10 @@ func GetFilesPath(fileUrl string) (path string, err error) { err = gerror.New("解析附件路径失败") return } - path = gstr.TrimLeft(pathInfo["path"], "/") + pos := gstr.PosI(pathInfo["path"], upPath) + if pos >= 0 { + path = gstr.SubStr(pathInfo["path"], pos) + } return }