tiger1103 3 éve
szülő
commit
5781ba4958
4 módosított fájl, 0 hozzáadás és 254 törlés
  1. 0 47
      library/upload/init.go
  2. 0 57
      library/upload/local.go
  3. 0 75
      library/upload/qiniou.go
  4. 0 75
      library/upload/tencent.go

+ 0 - 47
library/upload/init.go

@@ -1,47 +0,0 @@
-package upload
-
-import (
-	"context"
-	"github.com/gogf/gf/v2/net/ghttp"
-	"github.com/tiger1103/gfast/v3/api/v1/system"
-)
-
-const (
-	SourceLocal   UploaderType = iota //  上传到本地
-	SourceTencent                     //  上传至腾讯云
-	SourceQiniu                       //  上传到七牛云
-)
-
-type UploaderType int
-
-type IUpload interface {
-	Upload(ctx context.Context, file *ghttp.UploadFile) (result system.UploadResponse, err error)
-}
-
-var uploadCollection map[UploaderType]IUpload
-
-// 注册上传接口
-func RegisterUploader(key UploaderType, value IUpload) {
-	if uploadCollection == nil {
-		uploadCollection = make(map[UploaderType]IUpload)
-	}
-	uploadCollection[key] = value
-}
-
-// 获取上传接口
-func GetUploader(key UploaderType) IUpload {
-	if uploadCollection == nil {
-		return nil
-	}
-	if v, ok := uploadCollection[key]; ok {
-		return v
-	}
-	return nil
-}
-
-func init() {
-
-	RegisterUploader(SourceLocal, &Local{})
-	RegisterUploader(SourceTencent, &Tencent{})
-	RegisterUploader(SourceQiniu, &Qiniou{})
-}

+ 0 - 57
library/upload/local.go

@@ -1,57 +0,0 @@
-package upload
-
-import (
-	"context"
-	"errors"
-	"fmt"
-	"github.com/gogf/gf/v2/frame/g"
-	"github.com/gogf/gf/v2/net/ghttp"
-	"github.com/tiger1103/gfast/v3/api/v1/system"
-	"github.com/tiger1103/gfast/v3/internal/app/common/consts"
-	"strings"
-	"time"
-)
-
-type Local struct {
-}
-
-func (s *Local) Upload(ctx context.Context, file *ghttp.UploadFile) (result system.UploadResponse, err error) {
-	if file == nil {
-		err = errors.New("文件必须!")
-		return
-	}
-	r := g.RequestFromCtx(ctx)
-	urlPerfix := fmt.Sprintf("http://%s/", r.Host)
-	p := strings.Trim(consts.UploadPath, "/")
-	sp := s.getStaticPath(ctx)
-	if sp != "" {
-		sp = strings.Trim(sp, "/")
-	}
-	nowData := time.Now().Format("2006-01-02")
-	// 包含静态文件夹的路径
-	fullDirPath := sp + "/" + p + "/" + nowData
-	fileName, err := file.Save(fullDirPath, true)
-	if err != nil {
-		return
-	}
-	// 不含静态文件夹的路径
-	fullPath := p + "/" + nowData + "/" + fileName
-
-	result = system.UploadResponse{
-		Size:     file.Size,
-		Path:     fullPath,
-		FullPath: urlPerfix + fullPath,
-		Name:     file.Filename,
-		Type:     file.Header.Get("Content-type"),
-	}
-	return
-}
-
-// 静态文件夹目录
-func (s *Local) getStaticPath(ctx context.Context) string {
-	value, _ := g.Cfg().Get(ctx, "server.serverRoot")
-	if !value.IsEmpty() {
-		return value.String()
-	}
-	return ""
-}

+ 0 - 75
library/upload/qiniou.go

@@ -1,75 +0,0 @@
-package upload
-
-import (
-	"context"
-	"github.com/gogf/gf/v2/frame/g"
-	"github.com/gogf/gf/v2/net/ghttp"
-	"github.com/gogf/gf/v2/util/guid"
-	"github.com/qiniu/go-sdk/v7/auth/qbox"
-	"github.com/qiniu/go-sdk/v7/storage"
-	"github.com/tiger1103/gfast/v3/api/v1/system"
-	"path"
-)
-
-type Qiniou struct{}
-
-func (s *Qiniou) Upload(ctx context.Context, file *ghttp.UploadFile) (result system.UploadResponse, err error) {
-
-	url, err := s.toQiniou(ctx, file)
-	if err != nil {
-		return
-	}
-	result = system.UploadResponse{
-		Size:     file.Size,
-		Path:     url,
-		FullPath: url,
-		Name:     file.Filename,
-		Type:     file.Header.Get("Content-type"),
-	}
-
-	return
-}
-
-func (s *Qiniou) toQiniou(ctx context.Context, f *ghttp.UploadFile) (url string, err error) {
-	file, err := f.Open()
-	if err != nil {
-		return
-	}
-	defer file.Close()
-	v, err := g.Cfg().Get(ctx, "upload.qiniou")
-	if err != nil {
-		return
-	}
-	m := v.MapStrVar()
-	var AccessKey = m["accessKey"].String()
-	var SerectKey = m["sercetKey"].String()
-	var Bucket = m["bucket"].String()
-	var ImgUrl = m["qiniuServer"].String()
-
-	putPlicy := storage.PutPolicy{
-		Scope: Bucket,
-	}
-	mac := qbox.NewMac(AccessKey, SerectKey)
-	upToken := putPlicy.UploadToken(mac)
-	cfg := storage.Config{
-		Zone:          &storage.ZoneHuanan,
-		UseCdnDomains: false,
-		UseHTTPS:      false,
-	}
-	putExtra := storage.PutExtra{
-		MimeType: f.Header.Get("Content-type"),
-	}
-	formUploader := storage.NewFormUploader(&cfg)
-	ret := storage.PutRet{}
-	filename := guid.S() + path.Ext(f.Filename)
-	fileSize := f.Size
-	//err = formUploader.PutWithoutKey(context.Background(), &ret, upToken, file, fileSize, &putExtra)
-	err = formUploader.Put(context.Background(), &ret, upToken, filename, file, fileSize, &putExtra)
-	if err != nil {
-		return
-	}
-
-	url = ImgUrl + "/" + filename
-	return url, nil
-
-}

+ 0 - 75
library/upload/tencent.go

@@ -1,75 +0,0 @@
-package upload
-
-import (
-	"context"
-	"github.com/gogf/gf/v2/frame/g"
-	"github.com/gogf/gf/v2/net/ghttp"
-	"github.com/gogf/gf/v2/os/gfile"
-	"github.com/gogf/gf/v2/os/gtime"
-	"github.com/gogf/gf/v2/util/grand"
-	"github.com/tencentyun/cos-go-sdk-v5"
-	"github.com/tencentyun/cos-go-sdk-v5/debug"
-	"github.com/tiger1103/gfast/v3/api/v1/system"
-	"io"
-	"net/http"
-	"net/url"
-	"strconv"
-	"strings"
-)
-
-type Tencent struct {
-}
-
-func (s *Tencent) Upload(ctx context.Context, file *ghttp.UploadFile) (result system.UploadResponse, err error) {
-	v, err := g.Cfg().Get(ctx, "upload.tencentCOS")
-	if err != nil {
-		return
-	}
-	m := v.MapStrVar()
-	var (
-		upPath    = m["upPath"].String()
-		rawUrl    = m["rawUrl"].String()
-		secretID  = m["secretID"].String()
-		secretKey = m["secretKey"].String()
-	)
-	name := gfile.Basename(file.Filename)
-	name = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
-	name = name + gfile.Ext(file.Filename)
-
-	path := upPath + name
-
-	url, _ := url.Parse(rawUrl)
-	b := &cos.BaseURL{BucketURL: url}
-	client := cos.NewClient(b, &http.Client{
-		Transport: &cos.AuthorizationTransport{
-			SecretID:  secretID,
-			SecretKey: secretKey,
-			Transport: &debug.DebugRequestTransport{
-				RequestHeader:  false,
-				RequestBody:    false,
-				ResponseHeader: false,
-				ResponseBody:   false,
-			},
-		},
-	})
-	opt := &cos.ObjectPutOptions{
-		ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
-			ContentLength: int64(file.Size),
-		},
-	}
-	var f io.ReadCloser
-	f, err = file.Open()
-	if err != nil {
-		return
-	}
-	defer f.Close()
-	_, err = client.Object.Put(context.Background(), path, f, opt)
-	result = system.UploadResponse{
-		Size:     file.Size,
-		Path:     rawUrl + path,
-		FullPath: rawUrl + path,
-		Name:     file.Filename,
-		Type:     file.Header.Get("Content-type"),
-	}
-	return
-}