mirror of
				https://github.com/cloudreve/cloudreve.git
				synced 2025-11-04 04:47:24 +08:00 
			
		
		
		
	Feat: generate upload server url from policy
This commit is contained in:
		@ -6,6 +6,7 @@ import (
 | 
			
		||||
	"github.com/HFO4/cloudreve/pkg/cache"
 | 
			
		||||
	"github.com/HFO4/cloudreve/pkg/util"
 | 
			
		||||
	"github.com/jinzhu/gorm"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"path"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
@ -147,3 +148,22 @@ func (policy *Policy) GenerateFileName(uid uint, origin string) string {
 | 
			
		||||
func (policy *Policy) IsDirectlyPreview() bool {
 | 
			
		||||
	return policy.Type == "local"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetUploadURL 获取文件上传服务API地址
 | 
			
		||||
func (policy *Policy) GetUploadURL() string {
 | 
			
		||||
	server, err := url.Parse(policy.Server)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return policy.Server
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var controller *url.URL
 | 
			
		||||
	switch policy.Type {
 | 
			
		||||
	case "local":
 | 
			
		||||
		controller, _ = url.Parse("/api/v3/file/upload")
 | 
			
		||||
	case "remote":
 | 
			
		||||
		controller, _ = url.Parse("/api/v3/slave/upload")
 | 
			
		||||
	default:
 | 
			
		||||
		controller, _ = url.Parse("")
 | 
			
		||||
	}
 | 
			
		||||
	return server.ResolveReference(controller).String()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -141,3 +141,26 @@ func TestPolicy_IsDirectlyPreview(t *testing.T) {
 | 
			
		||||
	policy.Type = "remote"
 | 
			
		||||
	asserts.False(policy.IsDirectlyPreview())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestPolicy_GetUploadURL(t *testing.T) {
 | 
			
		||||
	asserts := assert.New(t)
 | 
			
		||||
 | 
			
		||||
	// 本地
 | 
			
		||||
	{
 | 
			
		||||
		policy := Policy{Type: "local", Server: "http://127.0.0.1"}
 | 
			
		||||
		asserts.Equal("http://127.0.0.1/api/v3/file/upload", policy.GetUploadURL())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 远程
 | 
			
		||||
	{
 | 
			
		||||
		policy := Policy{Type: "remote", Server: "http://127.0.0.1"}
 | 
			
		||||
		asserts.Equal("http://127.0.0.1/api/v3/slave/upload", policy.GetUploadURL())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 未知
 | 
			
		||||
	{
 | 
			
		||||
		policy := Policy{Type: "unknown", Server: "http://127.0.0.1"}
 | 
			
		||||
		asserts.Equal("http://127.0.0.1", policy.GetUploadURL())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -64,7 +64,7 @@ func BuildUser(user model.User) User {
 | 
			
		||||
			SaveType:       user.Policy.Type,
 | 
			
		||||
			MaxSize:        fmt.Sprintf("%.2fmb", float64(user.Policy.MaxSize)/(1024*1024)),
 | 
			
		||||
			AllowedType:    user.Policy.OptionsSerialized.FileType,
 | 
			
		||||
			UploadURL:      user.Policy.Server,
 | 
			
		||||
			UploadURL:      user.Policy.GetUploadURL(),
 | 
			
		||||
			AllowGetSource: user.Policy.IsOriginLinkEnable,
 | 
			
		||||
		},
 | 
			
		||||
		Group: group{
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user