mirror of https://github.com/cloudreve/Cloudreve
Feat: generate upload server url from policy
parent
6b6bfb4c6b
commit
37e78cb39b
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/HFO4/cloudreve/pkg/cache"
|
"github.com/HFO4/cloudreve/pkg/cache"
|
||||||
"github.com/HFO4/cloudreve/pkg/util"
|
"github.com/HFO4/cloudreve/pkg/util"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -147,3 +148,22 @@ func (policy *Policy) GenerateFileName(uid uint, origin string) string {
|
||||||
func (policy *Policy) IsDirectlyPreview() bool {
|
func (policy *Policy) IsDirectlyPreview() bool {
|
||||||
return policy.Type == "local"
|
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"
|
policy.Type = "remote"
|
||||||
asserts.False(policy.IsDirectlyPreview())
|
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,
|
SaveType: user.Policy.Type,
|
||||||
MaxSize: fmt.Sprintf("%.2fmb", float64(user.Policy.MaxSize)/(1024*1024)),
|
MaxSize: fmt.Sprintf("%.2fmb", float64(user.Policy.MaxSize)/(1024*1024)),
|
||||||
AllowedType: user.Policy.OptionsSerialized.FileType,
|
AllowedType: user.Policy.OptionsSerialized.FileType,
|
||||||
UploadURL: user.Policy.Server,
|
UploadURL: user.Policy.GetUploadURL(),
|
||||||
AllowGetSource: user.Policy.IsOriginLinkEnable,
|
AllowGetSource: user.Policy.IsOriginLinkEnable,
|
||||||
},
|
},
|
||||||
Group: group{
|
Group: group{
|
||||||
|
|
Loading…
Reference in New Issue