mirror of
				https://github.com/cloudreve/cloudreve.git
				synced 2025-10-31 08:39:10 +08:00 
			
		
		
		
	Feat: adapt new uploader for COS policy
This commit is contained in:
		| @ -283,21 +283,6 @@ func OneDriveCallbackAuth() gin.HandlerFunc { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| // COSCallbackAuth 腾讯云COS回调签名验证 |  | ||||||
| func COSCallbackAuth() gin.HandlerFunc { |  | ||||||
| 	return func(c *gin.Context) { |  | ||||||
| 		//// 验证key并查找用户 |  | ||||||
| 		//resp, _ := uploadCallbackCheck(c) |  | ||||||
| 		//if resp.Code != 0 { |  | ||||||
| 		//	c.JSON(401, serializer.GeneralUploadCallbackFailed{Error: resp.Msg}) |  | ||||||
| 		//	c.Abort() |  | ||||||
| 		//	return |  | ||||||
| 		//} |  | ||||||
|  |  | ||||||
| 		c.Next() |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // S3CallbackAuth Amazon S3回调签名验证 | // S3CallbackAuth Amazon S3回调签名验证 | ||||||
| func S3CallbackAuth() gin.HandlerFunc { | func S3CallbackAuth() gin.HandlerFunc { | ||||||
| 	return func(c *gin.Context) { | 	return func(c *gin.Context) { | ||||||
|  | |||||||
| @ -3,8 +3,6 @@ package model | |||||||
| import ( | import ( | ||||||
| 	"encoding/gob" | 	"encoding/gob" | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" |  | ||||||
| 	"net/url" |  | ||||||
| 	"path" | 	"path" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| @ -231,7 +229,7 @@ func (policy *Policy) IsUploadPlaceholderWithSize() bool { | |||||||
| 		return true | 		return true | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if util.ContainsString([]string{"onedrive", "oss", "qiniu"}, policy.Type) { | 	if util.ContainsString([]string{"onedrive", "oss", "qiniu", "cos"}, policy.Type) { | ||||||
| 		return policy.OptionsSerialized.PlaceholderWithSize | 		return policy.OptionsSerialized.PlaceholderWithSize | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @ -243,39 +241,6 @@ func (policy *Policy) CanStructureBeListed() bool { | |||||||
| 	return policy.Type != "local" && policy.Type != "remote" | 	return policy.Type != "local" && policy.Type != "remote" | ||||||
| } | } | ||||||
|  |  | ||||||
| // GetUploadURL 获取文件上传服务API地址 |  | ||||||
| func (policy *Policy) GetUploadURL() string { |  | ||||||
| 	server, err := url.Parse(policy.Server) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return policy.Server |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	controller, _ := url.Parse("") |  | ||||||
| 	switch policy.Type { |  | ||||||
| 	case "local", "onedrive": |  | ||||||
| 		return "/api/v3/file/upload" |  | ||||||
| 	case "remote": |  | ||||||
| 		controller, _ = url.Parse("/api/v3/slave/upload") |  | ||||||
| 	case "oss": |  | ||||||
| 		return "https://" + policy.BucketName + "." + policy.Server |  | ||||||
| 	case "cos": |  | ||||||
| 		return policy.Server |  | ||||||
| 	case "upyun": |  | ||||||
| 		return "https://v0.api.upyun.com/" + policy.BucketName |  | ||||||
| 	case "s3": |  | ||||||
| 		if policy.Server == "" { |  | ||||||
| 			return fmt.Sprintf("https://%s.s3.%s.amazonaws.com/", policy.BucketName, |  | ||||||
| 				policy.OptionsSerialized.Region) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		if !strings.Contains(policy.Server, policy.BucketName) { |  | ||||||
| 			controller, _ = url.Parse("/" + policy.BucketName) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return server.ResolveReference(controller).String() |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // SaveAndClearCache 更新并清理缓存 | // SaveAndClearCache 更新并清理缓存 | ||||||
| func (policy *Policy) SaveAndClearCache() error { | func (policy *Policy) SaveAndClearCache() error { | ||||||
| 	err := DB.Save(policy).Error | 	err := DB.Save(policy).Error | ||||||
|  | |||||||
| @ -357,8 +357,9 @@ func (handler Driver) Token(ctx context.Context, ttl int64, uploadSession *seria | |||||||
|  |  | ||||||
| 	res, err := handler.getUploadCredential(ctx, postPolicy, keyTime, savePath) | 	res, err := handler.getUploadCredential(ctx, postPolicy, keyTime, savePath) | ||||||
| 	if err == nil { | 	if err == nil { | ||||||
|  | 		res.SessionID = uploadSession.Key | ||||||
| 		res.Callback = apiURL | 		res.Callback = apiURL | ||||||
| 		res.Key = uploadSession.Key | 		res.UploadURLs = []string{handler.Policy.Server} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return res, err | 	return res, err | ||||||
| @ -418,7 +419,7 @@ func (handler Driver) getUploadCredential(ctx context.Context, policy UploadPoli | |||||||
| 		Policy:     policyEncoded, | 		Policy:     policyEncoded, | ||||||
| 		Path:       savePath, | 		Path:       savePath, | ||||||
| 		AccessKey:  handler.Policy.AccessKey, | 		AccessKey:  handler.Policy.AccessKey, | ||||||
| 		Token:     fmt.Sprintf("%x", signature), | 		Credential: fmt.Sprintf("%x", signature), | ||||||
| 		KeyTime:    keyTime, | 		KeyTime:    keyTime, | ||||||
| 	}, nil | 	}, nil | ||||||
| } | } | ||||||
|  | |||||||
| @ -27,13 +27,12 @@ type UploadCredential struct { | |||||||
| 	Credential string   `json:"credential,omitempty"` | 	Credential string   `json:"credential,omitempty"` | ||||||
| 	UploadID   string   `json:"uploadID,omitempty"` | 	UploadID   string   `json:"uploadID,omitempty"` | ||||||
| 	Callback   string   `json:"callback,omitempty"` // 回调地址 | 	Callback   string   `json:"callback,omitempty"` // 回调地址 | ||||||
|  | 	Path       string   `json:"path,omitempty"`     // 存储路径 | ||||||
|  | 	AccessKey  string   `json:"ak,omitempty"` | ||||||
|  | 	KeyTime    string   `json:"keyTime,omitempty"` // COS用有效期 | ||||||
|  | 	Policy     string   `json:"policy,omitempty"` | ||||||
|  |  | ||||||
| 	Token     string `json:"token"` | 	Token string `json:"token,omitempty"` | ||||||
| 	Policy    string `json:"policy"` |  | ||||||
| 	Path      string `json:"path"` // 存储路径 |  | ||||||
| 	AccessKey string `json:"ak"` |  | ||||||
| 	KeyTime   string `json:"key_time,omitempty"` // COS用有效期 |  | ||||||
| 	Key       string `json:"key,omitempty"`      // 文件标识符,通常为回调key |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // UploadSession 上传会话 | // UploadSession 上传会话 | ||||||
|  | |||||||
| @ -279,8 +279,8 @@ func InitMasterRouter() *gin.Engine { | |||||||
| 			} | 			} | ||||||
| 			// 腾讯云COS策略上传回调 | 			// 腾讯云COS策略上传回调 | ||||||
| 			callback.GET( | 			callback.GET( | ||||||
| 				"cos/:key", | 				"cos/:sessionID", | ||||||
| 				middleware.COSCallbackAuth(), | 				middleware.UseUploadSession("cos"), | ||||||
| 				controllers.COSCallback, | 				controllers.COSCallback, | ||||||
| 			) | 			) | ||||||
| 			// AWS S3策略上传回调 | 			// AWS S3策略上传回调 | ||||||
|  | |||||||
| @ -200,17 +200,16 @@ func (service *COSCallback) PreProcess(c *gin.Context) serializer.Response { | |||||||
| 	defer fs.Recycle() | 	defer fs.Recycle() | ||||||
|  |  | ||||||
| 	// 获取回调会话 | 	// 获取回调会话 | ||||||
| 	callbackSessionRaw, _ := c.Get("callbackSession") | 	uploadSession := c.MustGet(filesystem.UploadSessionCtx).(*serializer.UploadSession) | ||||||
| 	callbackSession := callbackSessionRaw.(*serializer.UploadSession) |  | ||||||
|  |  | ||||||
| 	// 获取文件信息 | 	// 获取文件信息 | ||||||
| 	info, err := fs.Handler.(cos.Driver).Meta(context.Background(), callbackSession.SavePath) | 	info, err := fs.Handler.(cos.Driver).Meta(context.Background(), uploadSession.SavePath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err) | 		return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// 验证实际文件信息与回调会话中是否一致 | 	// 验证实际文件信息与回调会话中是否一致 | ||||||
| 	if callbackSession.Size != info.Size || callbackSession.Key != info.CallbackKey { | 	if uploadSession.Size != info.Size || uploadSession.Key != info.CallbackKey { | ||||||
| 		return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err) | 		return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 HFO4
					HFO4