Feat: get file source route / Fix: cache initialize at wrong time

This commit is contained in:
HFO4
2019-12-10 11:55:28 +08:00
parent e871f6e421
commit 1963a495fb
6 changed files with 83 additions and 7 deletions

3
pkg/cache/driver.go vendored
View File

@@ -8,7 +8,8 @@ import (
// Store 缓存存储器
var Store Driver
func init() {
// Init 初始化缓存
func Init() {
//Store = NewRedisStore(10, "tcp", "127.0.0.1:6379", "", "0")
//return
if conf.RedisConfig.Server == "" || gin.Mode() == gin.TestMode {

View File

@@ -42,3 +42,11 @@ func TestSetSettings(t *testing.T) {
asserts.Equal("3", value1)
asserts.Equal("4", value2)
}
func TestInit(t *testing.T) {
asserts := assert.New(t)
asserts.NotPanics(func() {
Init()
})
}

View File

@@ -3,6 +3,7 @@ package filesystem
import (
"context"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/HFO4/cloudreve/pkg/util"
"github.com/juju/ratelimit"
"io"
@@ -150,3 +151,26 @@ func (fs *FileSystem) GroupFileByPolicy(ctx context.Context, files []model.File)
return policyGroup
}
// GetSource 获取可直接访问文件的外链地址
func (fs *FileSystem) GetSource(ctx context.Context, fileID uint) (string, error) {
// 查找文件记录
fileObject, err := model.GetFilesByIDs([]uint{fileID}, fs.User.ID)
if err != nil || len(fileObject) == 0 {
return "", ErrObjectNotExist.WithError(err)
}
fs.FileTarget = []model.File{fileObject[0]}
// 将当前存储策略重设为文件使用的
fs.Policy = fileObject[0].GetPolicy()
err = fs.dispatchHandler()
if err != nil {
return "", err
}
// 检查存储策略是否可以获得外链
if !fs.Policy.IsOriginLinkEnable {
return "", serializer.NewError(serializer.CodePolicyNotAllowed, "当前存储策略无法获得外链", nil)
}
return "", nil
}