mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: get file content via anonymous url
This commit is contained in:
@@ -82,21 +82,24 @@ func (fs *FileSystem) GetContent(ctx context.Context, path string) (io.ReadSeeke
|
||||
}
|
||||
|
||||
// 找到文件
|
||||
exist, file := fs.IsFileExist(path)
|
||||
if !exist {
|
||||
return nil, ErrObjectNotExist
|
||||
if len(fs.FileTarget) == 0 {
|
||||
exist, file := fs.IsFileExist(path)
|
||||
if !exist {
|
||||
return nil, ErrObjectNotExist
|
||||
}
|
||||
fs.FileTarget = []model.File{*file}
|
||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, file)
|
||||
}
|
||||
fs.FileTarget = []model.File{*file}
|
||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, file)
|
||||
|
||||
// 将当前存储策略重设为文件使用的
|
||||
fs.Policy = file.GetPolicy()
|
||||
fs.Policy = fs.FileTarget[0].GetPolicy()
|
||||
err = fs.dispatchHandler()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取文件流
|
||||
rs, err := fs.Handler.Get(ctx, file.SourceName)
|
||||
rs, err := fs.Handler.Get(ctx, fs.FileTarget[0].SourceName)
|
||||
if err != nil {
|
||||
return nil, ErrIO.WithError(err)
|
||||
}
|
||||
|
||||
@@ -79,6 +79,22 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
|
||||
return fs, err
|
||||
}
|
||||
|
||||
// NewAnonymousFileSystem 初始化匿名文件系统
|
||||
// TODO 测试
|
||||
func NewAnonymousFileSystem() (*FileSystem, error) {
|
||||
fs := &FileSystem{
|
||||
User: &model.User{},
|
||||
}
|
||||
|
||||
anonymousGroup, err := model.GetGroupByID(3)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fs.User.Group = anonymousGroup
|
||||
return fs, nil
|
||||
}
|
||||
|
||||
// dispatchHandler 根据存储策略分配文件适配器
|
||||
func (fs *FileSystem) dispatchHandler() error {
|
||||
var policyType string
|
||||
@@ -134,3 +150,13 @@ func (fs *FileSystem) SetTargetDir(dirs *[]model.Folder) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// SetTargetFileByIDs 根据文件ID设置目标文件,忽略用户ID
|
||||
func (fs *FileSystem) SetTargetFileByIDs(ids []uint) error {
|
||||
files, err := model.GetFilesByIDs(ids, 0)
|
||||
if err != nil || len(files) == 0 {
|
||||
return ErrFileExisted.WithError(err)
|
||||
}
|
||||
fs.SetTargetFile(&files)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -64,6 +64,8 @@ const (
|
||||
CodePolicyNotAllowed = 50003
|
||||
// CodeIOFailed IO操作失败
|
||||
CodeIOFailed = 50004
|
||||
// CodeGroupNotAllowed 当前用户组不允许
|
||||
CodeGroupNotAllowed = 50005
|
||||
//CodeParamErr 各种奇奇怪怪的参数错误
|
||||
CodeParamErr = 40001
|
||||
// CodeNotSet 未定错误,后续尝试从error中获取
|
||||
|
||||
Reference in New Issue
Block a user