Feat: get file source route / Fix: cache initialize at wrong time
This commit is contained in:
parent
e871f6e421
commit
1963a495fb
6 changed files with 83 additions and 7 deletions
2
main.go
2
main.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/HFO4/cloudreve/models"
|
"github.com/HFO4/cloudreve/models"
|
||||||
"github.com/HFO4/cloudreve/pkg/auth"
|
"github.com/HFO4/cloudreve/pkg/auth"
|
||||||
"github.com/HFO4/cloudreve/pkg/authn"
|
"github.com/HFO4/cloudreve/pkg/authn"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/cache"
|
||||||
"github.com/HFO4/cloudreve/pkg/conf"
|
"github.com/HFO4/cloudreve/pkg/conf"
|
||||||
"github.com/HFO4/cloudreve/routers"
|
"github.com/HFO4/cloudreve/routers"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -11,6 +12,7 @@ import (
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
conf.Init("conf/conf.ini")
|
conf.Init("conf/conf.ini")
|
||||||
|
cache.Init()
|
||||||
model.Init()
|
model.Init()
|
||||||
|
|
||||||
// Debug 关闭时,切换为生产模式
|
// Debug 关闭时,切换为生产模式
|
||||||
|
|
3
pkg/cache/driver.go
vendored
3
pkg/cache/driver.go
vendored
|
@ -8,7 +8,8 @@ import (
|
||||||
// Store 缓存存储器
|
// Store 缓存存储器
|
||||||
var Store Driver
|
var Store Driver
|
||||||
|
|
||||||
func init() {
|
// Init 初始化缓存
|
||||||
|
func Init() {
|
||||||
//Store = NewRedisStore(10, "tcp", "127.0.0.1:6379", "", "0")
|
//Store = NewRedisStore(10, "tcp", "127.0.0.1:6379", "", "0")
|
||||||
//return
|
//return
|
||||||
if conf.RedisConfig.Server == "" || gin.Mode() == gin.TestMode {
|
if conf.RedisConfig.Server == "" || gin.Mode() == gin.TestMode {
|
||||||
|
|
8
pkg/cache/driver_test.go
vendored
8
pkg/cache/driver_test.go
vendored
|
@ -42,3 +42,11 @@ func TestSetSettings(t *testing.T) {
|
||||||
asserts.Equal("3", value1)
|
asserts.Equal("3", value1)
|
||||||
asserts.Equal("4", value2)
|
asserts.Equal("4", value2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInit(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
|
||||||
|
asserts.NotPanics(func() {
|
||||||
|
Init()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package filesystem
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
model "github.com/HFO4/cloudreve/models"
|
model "github.com/HFO4/cloudreve/models"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||||
"github.com/HFO4/cloudreve/pkg/util"
|
"github.com/HFO4/cloudreve/pkg/util"
|
||||||
"github.com/juju/ratelimit"
|
"github.com/juju/ratelimit"
|
||||||
"io"
|
"io"
|
||||||
|
@ -150,3 +151,26 @@ func (fs *FileSystem) GroupFileByPolicy(ctx context.Context, files []model.File)
|
||||||
|
|
||||||
return policyGroup
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -15,6 +15,40 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetSource 获取文件的外链地址
|
||||||
|
func GetSource(c *gin.Context) {
|
||||||
|
// 创建上下文
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
fs, err := filesystem.NewFileSystemFromContext(c)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文件ID
|
||||||
|
fileID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(200, serializer.ParamErr("无法解析文件ID", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceURL, err := fs.GetSource(ctx, uint(fileID))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(200, serializer.Err(serializer.CodeNotSet, err.Error(), err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, serializer.Response{
|
||||||
|
Code: 0,
|
||||||
|
Data: struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
}{URL: sourceURL},
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Thumb 获取文件缩略图
|
// Thumb 获取文件缩略图
|
||||||
func Thumb(c *gin.Context) {
|
func Thumb(c *gin.Context) {
|
||||||
// 创建上下文
|
// 创建上下文
|
||||||
|
|
|
@ -41,12 +41,17 @@ func InitRouter() *gin.Engine {
|
||||||
{
|
{
|
||||||
// 测试用路由
|
// 测试用路由
|
||||||
v3.GET("site/ping", controllers.Ping)
|
v3.GET("site/ping", controllers.Ping)
|
||||||
// 用户登录
|
|
||||||
v3.POST("user/session", controllers.UserLogin)
|
// 不需要登录的用户相关路由
|
||||||
// WebAuthn登陆初始化
|
{
|
||||||
v3.GET("user/authn/:username", controllers.StartLoginAuthn)
|
// 用户登录
|
||||||
// WebAuthn登陆
|
v3.POST("user/session", controllers.UserLogin)
|
||||||
v3.POST("user/authn/finish/:username", controllers.FinishLoginAuthn)
|
// WebAuthn登陆初始化
|
||||||
|
v3.GET("user/authn/:username", controllers.StartLoginAuthn)
|
||||||
|
// WebAuthn登陆
|
||||||
|
v3.POST("user/authn/finish/:username", controllers.FinishLoginAuthn)
|
||||||
|
}
|
||||||
|
|
||||||
// 验证码
|
// 验证码
|
||||||
v3.GET("captcha", controllers.Captcha)
|
v3.GET("captcha", controllers.Captcha)
|
||||||
// 站点全局配置
|
// 站点全局配置
|
||||||
|
@ -80,6 +85,8 @@ func InitRouter() *gin.Engine {
|
||||||
file.GET("download/*path", controllers.Download)
|
file.GET("download/*path", controllers.Download)
|
||||||
// 下载文件
|
// 下载文件
|
||||||
file.GET("thumb/:id", controllers.Thumb)
|
file.GET("thumb/:id", controllers.Thumb)
|
||||||
|
// 取得文件外链
|
||||||
|
file.GET("source/:id", controllers.GetSource)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 目录
|
// 目录
|
||||||
|
|
Loading…
Add table
Reference in a new issue