mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: get policy from directory props / Feat: return source enabled flag in file list
This commit is contained in:
@@ -62,10 +62,10 @@ func (fs *FileSystem) AddFile(ctx context.Context, parent *model.Folder) (*model
|
||||
UserID: fs.User.ID,
|
||||
Size: file.GetSize(),
|
||||
FolderID: parent.ID,
|
||||
PolicyID: fs.User.Policy.ID,
|
||||
PolicyID: fs.Policy.ID,
|
||||
}
|
||||
|
||||
if fs.User.Policy.IsThumbExist(file.GetFileName()) {
|
||||
if fs.Policy.IsThumbExist(file.GetFileName()) {
|
||||
newFile.PicInfo = "1,1"
|
||||
}
|
||||
|
||||
@@ -350,7 +350,7 @@ func (fs *FileSystem) resetPolicyToFirstFile(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// Search 搜索文件
|
||||
func (fs *FileSystem) Search(ctx context.Context, keywords ...interface{}) ([]Object, error) {
|
||||
func (fs *FileSystem) Search(ctx context.Context, keywords ...interface{}) ([]serializer.Object, error) {
|
||||
files, _ := model.GetFilesByKeywords(fs.User.ID, keywords...)
|
||||
fs.SetTargetFile(&files)
|
||||
|
||||
|
||||
@@ -104,6 +104,8 @@ func (fs *FileSystem) reset() {
|
||||
func NewFileSystem(user *model.User) (*FileSystem, error) {
|
||||
fs := getEmptyFS()
|
||||
fs.User = user
|
||||
fs.Policy = &fs.User.Policy
|
||||
|
||||
// 分配存储策略适配器
|
||||
err := fs.DispatchHandler()
|
||||
|
||||
@@ -132,16 +134,11 @@ func NewAnonymousFileSystem() (*FileSystem, error) {
|
||||
|
||||
// DispatchHandler 根据存储策略分配文件适配器
|
||||
func (fs *FileSystem) DispatchHandler() error {
|
||||
var policyType string
|
||||
var currentPolicy *model.Policy
|
||||
currentPolicy := fs.Policy
|
||||
policyType := currentPolicy.Type
|
||||
|
||||
if fs.Policy == nil {
|
||||
// 如果没有具体指定,就是用用户当前存储策略
|
||||
policyType = fs.User.Policy.Type
|
||||
currentPolicy = &fs.User.Policy
|
||||
} else {
|
||||
policyType = fs.Policy.Type
|
||||
currentPolicy = fs.Policy
|
||||
if currentPolicy == nil {
|
||||
return ErrUnknownPolicyType
|
||||
}
|
||||
|
||||
switch policyType {
|
||||
@@ -241,7 +238,7 @@ func NewFileSystemFromCallback(c *gin.Context) (*FileSystem, error) {
|
||||
|
||||
// SwitchToSlaveHandler 将负责上传的 Handler 切换为从机节点
|
||||
func (fs *FileSystem) SwitchToSlaveHandler(node cluster.Node) {
|
||||
fs.Handler = slaveinmaster.NewDriver(node, fs.Handler, &fs.User.Policy)
|
||||
fs.Handler = slaveinmaster.NewDriver(node, fs.Handler, fs.Policy)
|
||||
}
|
||||
|
||||
// SwitchToShadowHandler 将负责上传的 Handler 切换为从机节点转存使用的影子处理器
|
||||
|
||||
@@ -119,7 +119,6 @@ func HookResetPolicy(ctx context.Context, fs *FileSystem) error {
|
||||
}
|
||||
|
||||
fs.Policy = originFile.GetPolicy()
|
||||
fs.User.Policy = *fs.Policy
|
||||
return fs.DispatchHandler()
|
||||
}
|
||||
|
||||
@@ -317,7 +316,7 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
|
||||
fs.SetTargetFile(&[]model.File{*file})
|
||||
|
||||
// 异步尝试生成缩略图
|
||||
if fs.User.Policy.IsThumbGenerateNeeded() {
|
||||
if fs.Policy.IsThumbGenerateNeeded() {
|
||||
fs.recycleLock.Lock()
|
||||
go func() {
|
||||
defer fs.recycleLock.Unlock()
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
||||
@@ -19,18 +18,6 @@ import (
|
||||
=================
|
||||
*/
|
||||
|
||||
// Object 文件或者目录
|
||||
type Object struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Pic string `json:"pic"`
|
||||
Size uint64 `json:"size"`
|
||||
Type string `json:"type"`
|
||||
Date time.Time `json:"date"`
|
||||
Key string `json:"key,omitempty"`
|
||||
}
|
||||
|
||||
// Rename 重命名对象
|
||||
func (fs *FileSystem) Rename(ctx context.Context, dir, file []uint, new string) (err error) {
|
||||
// 验证新名字
|
||||
@@ -266,7 +253,7 @@ func (fs *FileSystem) ListDeleteFiles(ctx context.Context, ids []uint) error {
|
||||
// pathProcessor为最终对象路径的处理钩子。
|
||||
// 有些情况下(如在分享页面列对象)时,
|
||||
// 路径需要截取掉被分享目录路径之前的部分。
|
||||
func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor func(string) string) ([]Object, error) {
|
||||
func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor func(string) string) ([]serializer.Object, error) {
|
||||
// 获取父目录
|
||||
isExist, folder := fs.IsPathExist(dirPath)
|
||||
if !isExist {
|
||||
@@ -289,7 +276,7 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu
|
||||
|
||||
// ListPhysical 列出存储策略中的外部目录
|
||||
// TODO:测试
|
||||
func (fs *FileSystem) ListPhysical(ctx context.Context, dirPath string) ([]Object, error) {
|
||||
func (fs *FileSystem) ListPhysical(ctx context.Context, dirPath string) ([]serializer.Object, error) {
|
||||
if err := fs.DispatchHandler(); fs.Policy == nil || err != nil {
|
||||
return nil, ErrUnknownPolicyType
|
||||
}
|
||||
@@ -319,7 +306,7 @@ func (fs *FileSystem) ListPhysical(ctx context.Context, dirPath string) ([]Objec
|
||||
return fs.listObjects(ctx, dirPath, nil, folders, nil), nil
|
||||
}
|
||||
|
||||
func (fs *FileSystem) listObjects(ctx context.Context, parent string, files []model.File, folders []model.Folder, pathProcessor func(string) string) []Object {
|
||||
func (fs *FileSystem) listObjects(ctx context.Context, parent string, files []model.File, folders []model.Folder, pathProcessor func(string) string) []serializer.Object {
|
||||
// 分享文件的ID
|
||||
shareKey := ""
|
||||
if key, ok := ctx.Value(fsctx.ShareKeyCtx).(string); ok {
|
||||
@@ -327,7 +314,7 @@ func (fs *FileSystem) listObjects(ctx context.Context, parent string, files []mo
|
||||
}
|
||||
|
||||
// 汇总处理结果
|
||||
objects := make([]Object, 0, len(files)+len(folders))
|
||||
objects := make([]serializer.Object, 0, len(files)+len(folders))
|
||||
|
||||
// 所有对象的父目录
|
||||
var processedPath string
|
||||
@@ -343,7 +330,7 @@ func (fs *FileSystem) listObjects(ctx context.Context, parent string, files []mo
|
||||
}
|
||||
}
|
||||
|
||||
objects = append(objects, Object{
|
||||
objects = append(objects, serializer.Object{
|
||||
ID: hashid.HashID(subFolder.ID, hashid.FolderID),
|
||||
Name: subFolder.Name,
|
||||
Path: processedPath,
|
||||
@@ -363,14 +350,15 @@ func (fs *FileSystem) listObjects(ctx context.Context, parent string, files []mo
|
||||
}
|
||||
}
|
||||
|
||||
newFile := Object{
|
||||
ID: hashid.HashID(file.ID, hashid.FileID),
|
||||
Name: file.Name,
|
||||
Path: processedPath,
|
||||
Pic: file.PicInfo,
|
||||
Size: file.Size,
|
||||
Type: "file",
|
||||
Date: file.CreatedAt,
|
||||
newFile := serializer.Object{
|
||||
ID: hashid.HashID(file.ID, hashid.FileID),
|
||||
Name: file.Name,
|
||||
Path: processedPath,
|
||||
Pic: file.PicInfo,
|
||||
Size: file.Size,
|
||||
Type: "file",
|
||||
Date: file.CreatedAt,
|
||||
SourceEnabled: file.GetPolicy().IsOriginLinkEnable,
|
||||
}
|
||||
if shareKey != "" {
|
||||
newFile.Key = shareKey
|
||||
|
||||
@@ -81,11 +81,11 @@ func (fs *FileSystem) Upload(ctx context.Context, file FileHeader) (err error) {
|
||||
func (fs *FileSystem) GenerateSavePath(ctx context.Context, file FileHeader) string {
|
||||
if fs.User.Model.ID != 0 {
|
||||
return path.Join(
|
||||
fs.User.Policy.GeneratePath(
|
||||
fs.Policy.GeneratePath(
|
||||
fs.User.Model.ID,
|
||||
file.GetVirtualPath(),
|
||||
),
|
||||
fs.User.Policy.GenerateFileName(
|
||||
fs.Policy.GenerateFileName(
|
||||
fs.User.Model.ID,
|
||||
file.GetFileName(),
|
||||
),
|
||||
@@ -155,15 +155,15 @@ func (fs *FileSystem) GetUploadToken(ctx context.Context, path string, size uint
|
||||
var err error
|
||||
|
||||
// 检查文件大小
|
||||
if fs.User.Policy.MaxSize != 0 {
|
||||
if size > fs.User.Policy.MaxSize {
|
||||
if fs.Policy.MaxSize != 0 {
|
||||
if size > fs.Policy.MaxSize {
|
||||
return nil, ErrFileSizeTooBig
|
||||
}
|
||||
}
|
||||
|
||||
// 是否需要预先生成存储路径
|
||||
var savePath string
|
||||
if fs.User.Policy.IsPathGenerateNeeded() {
|
||||
if fs.Policy.IsPathGenerateNeeded() {
|
||||
savePath = fs.GenerateSavePath(ctx, local.FileStream{Name: name, VirtualPath: path})
|
||||
ctx = context.WithValue(ctx, fsctx.SavePathCtx, savePath)
|
||||
}
|
||||
@@ -182,7 +182,6 @@ func (fs *FileSystem) GetUploadToken(ctx context.Context, path string, size uint
|
||||
serializer.UploadSession{
|
||||
Key: callbackKey,
|
||||
UID: fs.User.ID,
|
||||
PolicyID: fs.User.GetPolicyID(0),
|
||||
VirtualPath: path,
|
||||
Name: name,
|
||||
Size: size,
|
||||
|
||||
@@ -45,10 +45,10 @@ func (fs *FileSystem) ValidateLegalName(ctx context.Context, name string) bool {
|
||||
|
||||
// ValidateFileSize 验证上传的文件大小是否超出限制
|
||||
func (fs *FileSystem) ValidateFileSize(ctx context.Context, size uint64) bool {
|
||||
if fs.User.Policy.MaxSize == 0 {
|
||||
if fs.Policy.MaxSize == 0 {
|
||||
return true
|
||||
}
|
||||
return size <= fs.User.Policy.MaxSize
|
||||
return size <= fs.Policy.MaxSize
|
||||
}
|
||||
|
||||
// ValidateCapacity 验证并扣除用户容量
|
||||
@@ -59,11 +59,11 @@ func (fs *FileSystem) ValidateCapacity(ctx context.Context, size uint64) bool {
|
||||
// ValidateExtension 验证文件扩展名
|
||||
func (fs *FileSystem) ValidateExtension(ctx context.Context, fileName string) bool {
|
||||
// 不需要验证
|
||||
if len(fs.User.Policy.OptionsSerialized.FileType) == 0 {
|
||||
if len(fs.Policy.OptionsSerialized.FileType) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
return IsInExtensionList(fs.User.Policy.OptionsSerialized.FileType, fileName)
|
||||
return IsInExtensionList(fs.Policy.OptionsSerialized.FileType, fileName)
|
||||
}
|
||||
|
||||
// IsInExtensionList 返回文件的扩展名是否在给定的列表范围内
|
||||
|
||||
Reference in New Issue
Block a user