浏览代码

上传优化

yxh 4 年之前
父节点
当前提交
043beca901
共有 5 个文件被更改,包括 42 次插入26 次删除
  1. 0 23
      app/common/adapter/upload.go
  2. 16 0
      app/common/adapter/upload_local.go
  3. 17 0
      app/common/adapter/upload_tencent_cos.go
  4. 3 1
      config/config.toml
  5. 6 2
      library/utils.go

+ 0 - 23
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)
 }

+ 16 - 0
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

+ 17 - 0
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

+ 3 - 1
config/config.toml

@@ -85,4 +85,6 @@
         UpPath =    "/gfast/"
         RawUrl =    "https://您的cos空间域名.cos.ap-chongqing.myqcloud.com"
         SecretID =  "填写您的SecretID"
-        SecretKey = "填写您的SecretKey"
+        SecretKey = "填写您的SecretKey"
+    [upload.local] #本地上传配置
+        UpPath = "/pub_upload/" #上传路径

+ 6 - 2
library/utils.go

@@ -213,7 +213,8 @@ func GetRealFilesUrl(r *ghttp.Request, path string) (realPath string, err error)
 //获取附件相对路径
 func GetFilesPath(fileUrl string) (path string, err error) {
 	upType := gstr.ToLower(g.Cfg().GetString("upload.type"))
-	if upType != "local" || (upType == "local" && !gstr.ContainsI(fileUrl, "http")) {
+	upPath := gstr.Trim(g.Cfg().GetString("upload.local.UpPath"), "/")
+	if upType != "local" || (upType == "local" && !gstr.ContainsI(fileUrl, upPath)) {
 		path = fileUrl
 		return
 	}
@@ -223,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
 }