Feat: get policy from directory props / Feat: return source enabled flag in file list

pull/1146/head
HFO4 2 years ago
parent c84d0114ae
commit 855c9d92c4

@ -1 +1 @@
Subproject commit 88c1133306e2e9692b015db9aac57be20b269a53 Subproject commit b90a49ba5d809e3042bce801ac28fb7cde74c878

@ -62,10 +62,10 @@ func (fs *FileSystem) AddFile(ctx context.Context, parent *model.Folder) (*model
UserID: fs.User.ID, UserID: fs.User.ID,
Size: file.GetSize(), Size: file.GetSize(),
FolderID: parent.ID, 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" newFile.PicInfo = "1,1"
} }
@ -350,7 +350,7 @@ func (fs *FileSystem) resetPolicyToFirstFile(ctx context.Context) error {
} }
// Search 搜索文件 // 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...) files, _ := model.GetFilesByKeywords(fs.User.ID, keywords...)
fs.SetTargetFile(&files) fs.SetTargetFile(&files)

@ -104,6 +104,8 @@ func (fs *FileSystem) reset() {
func NewFileSystem(user *model.User) (*FileSystem, error) { func NewFileSystem(user *model.User) (*FileSystem, error) {
fs := getEmptyFS() fs := getEmptyFS()
fs.User = user fs.User = user
fs.Policy = &fs.User.Policy
// 分配存储策略适配器 // 分配存储策略适配器
err := fs.DispatchHandler() err := fs.DispatchHandler()
@ -132,16 +134,11 @@ func NewAnonymousFileSystem() (*FileSystem, error) {
// DispatchHandler 根据存储策略分配文件适配器 // DispatchHandler 根据存储策略分配文件适配器
func (fs *FileSystem) DispatchHandler() error { func (fs *FileSystem) DispatchHandler() error {
var policyType string currentPolicy := fs.Policy
var currentPolicy *model.Policy policyType := currentPolicy.Type
if fs.Policy == nil { if currentPolicy == nil {
// 如果没有具体指定,就是用用户当前存储策略 return ErrUnknownPolicyType
policyType = fs.User.Policy.Type
currentPolicy = &fs.User.Policy
} else {
policyType = fs.Policy.Type
currentPolicy = fs.Policy
} }
switch policyType { switch policyType {
@ -241,7 +238,7 @@ func NewFileSystemFromCallback(c *gin.Context) (*FileSystem, error) {
// SwitchToSlaveHandler 将负责上传的 Handler 切换为从机节点 // SwitchToSlaveHandler 将负责上传的 Handler 切换为从机节点
func (fs *FileSystem) SwitchToSlaveHandler(node cluster.Node) { 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 切换为从机节点转存使用的影子处理器 // SwitchToShadowHandler 将负责上传的 Handler 切换为从机节点转存使用的影子处理器

@ -119,7 +119,6 @@ func HookResetPolicy(ctx context.Context, fs *FileSystem) error {
} }
fs.Policy = originFile.GetPolicy() fs.Policy = originFile.GetPolicy()
fs.User.Policy = *fs.Policy
return fs.DispatchHandler() return fs.DispatchHandler()
} }
@ -317,7 +316,7 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error {
fs.SetTargetFile(&[]model.File{*file}) fs.SetTargetFile(&[]model.File{*file})
// 异步尝试生成缩略图 // 异步尝试生成缩略图
if fs.User.Policy.IsThumbGenerateNeeded() { if fs.Policy.IsThumbGenerateNeeded() {
fs.recycleLock.Lock() fs.recycleLock.Lock()
go func() { go func() {
defer fs.recycleLock.Unlock() defer fs.recycleLock.Unlock()

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"path" "path"
"strings" "strings"
"time"
model "github.com/cloudreve/Cloudreve/v3/models" model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "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 重命名对象 // Rename 重命名对象
func (fs *FileSystem) Rename(ctx context.Context, dir, file []uint, new string) (err error) { 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为最终对象路径的处理钩子。 // 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) isExist, folder := fs.IsPathExist(dirPath)
if !isExist { if !isExist {
@ -289,7 +276,7 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu
// ListPhysical 列出存储策略中的外部目录 // ListPhysical 列出存储策略中的外部目录
// TODO:测试 // 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 { if err := fs.DispatchHandler(); fs.Policy == nil || err != nil {
return nil, ErrUnknownPolicyType 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 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 // 分享文件的ID
shareKey := "" shareKey := ""
if key, ok := ctx.Value(fsctx.ShareKeyCtx).(string); ok { 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 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), ID: hashid.HashID(subFolder.ID, hashid.FolderID),
Name: subFolder.Name, Name: subFolder.Name,
Path: processedPath, Path: processedPath,
@ -363,14 +350,15 @@ func (fs *FileSystem) listObjects(ctx context.Context, parent string, files []mo
} }
} }
newFile := Object{ newFile := serializer.Object{
ID: hashid.HashID(file.ID, hashid.FileID), ID: hashid.HashID(file.ID, hashid.FileID),
Name: file.Name, Name: file.Name,
Path: processedPath, Path: processedPath,
Pic: file.PicInfo, Pic: file.PicInfo,
Size: file.Size, Size: file.Size,
Type: "file", Type: "file",
Date: file.CreatedAt, Date: file.CreatedAt,
SourceEnabled: file.GetPolicy().IsOriginLinkEnable,
} }
if shareKey != "" { if shareKey != "" {
newFile.Key = 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 { func (fs *FileSystem) GenerateSavePath(ctx context.Context, file FileHeader) string {
if fs.User.Model.ID != 0 { if fs.User.Model.ID != 0 {
return path.Join( return path.Join(
fs.User.Policy.GeneratePath( fs.Policy.GeneratePath(
fs.User.Model.ID, fs.User.Model.ID,
file.GetVirtualPath(), file.GetVirtualPath(),
), ),
fs.User.Policy.GenerateFileName( fs.Policy.GenerateFileName(
fs.User.Model.ID, fs.User.Model.ID,
file.GetFileName(), file.GetFileName(),
), ),
@ -155,15 +155,15 @@ func (fs *FileSystem) GetUploadToken(ctx context.Context, path string, size uint
var err error var err error
// 检查文件大小 // 检查文件大小
if fs.User.Policy.MaxSize != 0 { if fs.Policy.MaxSize != 0 {
if size > fs.User.Policy.MaxSize { if size > fs.Policy.MaxSize {
return nil, ErrFileSizeTooBig return nil, ErrFileSizeTooBig
} }
} }
// 是否需要预先生成存储路径 // 是否需要预先生成存储路径
var savePath string var savePath string
if fs.User.Policy.IsPathGenerateNeeded() { if fs.Policy.IsPathGenerateNeeded() {
savePath = fs.GenerateSavePath(ctx, local.FileStream{Name: name, VirtualPath: path}) savePath = fs.GenerateSavePath(ctx, local.FileStream{Name: name, VirtualPath: path})
ctx = context.WithValue(ctx, fsctx.SavePathCtx, savePath) ctx = context.WithValue(ctx, fsctx.SavePathCtx, savePath)
} }
@ -182,7 +182,6 @@ func (fs *FileSystem) GetUploadToken(ctx context.Context, path string, size uint
serializer.UploadSession{ serializer.UploadSession{
Key: callbackKey, Key: callbackKey,
UID: fs.User.ID, UID: fs.User.ID,
PolicyID: fs.User.GetPolicyID(0),
VirtualPath: path, VirtualPath: path,
Name: name, Name: name,
Size: size, Size: size,

@ -45,10 +45,10 @@ func (fs *FileSystem) ValidateLegalName(ctx context.Context, name string) bool {
// ValidateFileSize 验证上传的文件大小是否超出限制 // ValidateFileSize 验证上传的文件大小是否超出限制
func (fs *FileSystem) ValidateFileSize(ctx context.Context, size uint64) bool { func (fs *FileSystem) ValidateFileSize(ctx context.Context, size uint64) bool {
if fs.User.Policy.MaxSize == 0 { if fs.Policy.MaxSize == 0 {
return true return true
} }
return size <= fs.User.Policy.MaxSize return size <= fs.Policy.MaxSize
} }
// ValidateCapacity 验证并扣除用户容量 // ValidateCapacity 验证并扣除用户容量
@ -59,11 +59,11 @@ func (fs *FileSystem) ValidateCapacity(ctx context.Context, size uint64) bool {
// ValidateExtension 验证文件扩展名 // ValidateExtension 验证文件扩展名
func (fs *FileSystem) ValidateExtension(ctx context.Context, fileName string) bool { 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 true
} }
return IsInExtensionList(fs.User.Policy.OptionsSerialized.FileType, fileName) return IsInExtensionList(fs.Policy.OptionsSerialized.FileType, fileName)
} }
// IsInExtensionList 返回文件的扩展名是否在给定的列表范围内 // IsInExtensionList 返回文件的扩展名是否在给定的列表范围内

@ -2,6 +2,8 @@ package serializer
import ( import (
"encoding/gob" "encoding/gob"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/hashid"
"time" "time"
) )
@ -21,3 +23,55 @@ type ObjectProps struct {
QueryDate time.Time `json:"query_date"` QueryDate time.Time `json:"query_date"`
} }
// ObjectList 文件、目录列表
type ObjectList struct {
Parent string `json:"parent,omitempty"`
Objects []Object `json:"objects"`
Policy *PolicySummary `json:"policy,omitempty"`
}
// 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"`
SourceEnabled bool `json:"source_enabled"`
}
// PolicySummary 用于前端组件使用的存储策略概况
type PolicySummary struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
MaxSize uint64 `json:"max_size"`
FileType []string `json:"file_type"`
}
// BuildObjectList 构建列目录响应
func BuildObjectList(parent uint, objects []Object, policy *model.Policy) ObjectList {
res := ObjectList{
Objects: objects,
}
if parent > 0 {
res.Parent = hashid.HashID(parent, hashid.FolderID)
}
if policy != nil {
res.Policy = &PolicySummary{
ID: hashid.HashID(policy.ID, hashid.PolicyID),
Name: policy.Name,
Type: policy.Type,
MaxSize: policy.MaxSize,
FileType: policy.OptionsSerialized.FileType,
}
}
return res
}

@ -27,7 +27,6 @@ type User struct {
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
PreferredTheme string `json:"preferred_theme"` PreferredTheme string `json:"preferred_theme"`
Anonymous bool `json:"anonymous"` Anonymous bool `json:"anonymous"`
Policy policy `json:"policy"`
Group group `json:"group"` Group group `json:"group"`
Tags []tag `json:"tags"` Tags []tag `json:"tags"`
} }
@ -98,13 +97,6 @@ func BuildUser(user model.User) User {
CreatedAt: user.CreatedAt, CreatedAt: user.CreatedAt,
PreferredTheme: user.OptionsSerialized.PreferredTheme, PreferredTheme: user.OptionsSerialized.PreferredTheme,
Anonymous: user.IsAnonymous(), Anonymous: user.IsAnonymous(),
Policy: policy{
SaveType: user.Policy.Type,
MaxSize: fmt.Sprintf("%.2fmb", float64(user.Policy.MaxSize)/(1024*1024)),
AllowedType: user.Policy.OptionsSerialized.FileType,
UploadURL: user.Policy.GetUploadURL(),
AllowGetSource: user.Policy.IsOriginLinkEnable,
},
Group: group{ Group: group{
ID: user.GroupID, ID: user.GroupID,
Name: user.Group.Name, Name: user.Group.Name,

@ -3,17 +3,16 @@ package controllers
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/cloudreve/Cloudreve/v3/pkg/request"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"sync" "sync"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/conf" "github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/driver/local"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/request"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/service/explorer" "github.com/cloudreve/Cloudreve/v3/service/explorer"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -300,13 +299,6 @@ func FileUploadStream(c *gin.Context) {
return return
} }
// 非可用策略时拒绝上传
if user, ok := c.Get("user"); ok && !user.(*model.User).Policy.IsTransitUpload(fileSize) {
request.BlackHole(c.Request.Body)
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, "当前存储策略无法使用", nil))
return
}
// 解码文件名和路径 // 解码文件名和路径
fileName, err := url.QueryUnescape(c.Request.Header.Get("X-Cr-FileName")) fileName, err := url.QueryUnescape(c.Request.Header.Get("X-Cr-FileName"))
filePath, err := url.QueryUnescape(c.Request.Header.Get("X-Cr-Path")) filePath, err := url.QueryUnescape(c.Request.Header.Get("X-Cr-Path"))
@ -330,6 +322,13 @@ func FileUploadStream(c *gin.Context) {
return return
} }
// 非可用策略时拒绝上传
if !fs.Policy.IsTransitUpload(fileSize) {
request.BlackHole(c.Request.Body)
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, "当前存储策略无法使用", nil))
return
}
// 给文件系统分配钩子 // 给文件系统分配钩子
fs.Use("BeforeUpload", filesystem.HookValidateFile) fs.Use("BeforeUpload", filesystem.HookValidateFile)
fs.Use("BeforeUpload", filesystem.HookValidateCapacity) fs.Use("BeforeUpload", filesystem.HookValidateCapacity)

@ -54,9 +54,7 @@ func (service *ListFolderService) List(c *gin.Context) serializer.Response {
} }
return serializer.Response{ return serializer.Response{
Data: map[string]interface{}{ Data: serializer.BuildObjectList(0, res, nil),
"objects": res,
},
} }
} }
@ -82,9 +80,7 @@ func (service *ListFolderService) List(c *gin.Context) serializer.Response {
} }
return serializer.Response{ return serializer.Response{
Data: map[string]interface{}{ Data: serializer.BuildObjectList(0, res, nil),
"objects": res,
},
} }
} }

@ -4,7 +4,6 @@ import (
"context" "context"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem" "github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/hashid"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" "github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -40,10 +39,7 @@ func (service *DirectoryService) ListDirectory(c *gin.Context) serializer.Respon
return serializer.Response{ return serializer.Response{
Code: 0, Code: 0,
Data: map[string]interface{}{ Data: serializer.BuildObjectList(parentID, objects, fs.Policy),
"parent": hashid.HashID(parentID, hashid.FolderID),
"objects": objects,
},
} }
} }

@ -25,13 +25,6 @@ func (service *UploadCredentialService) Get(ctx context.Context, c *gin.Context)
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
} }
// 存储策略是否一致
if service.Type != "" {
if service.Type != fs.User.Policy.Type {
return serializer.Err(serializer.CodePolicyNotAllowed, "存储策略已变更,请刷新页面", nil)
}
}
ctx = context.WithValue(ctx, fsctx.GinCtx, c) ctx = context.WithValue(ctx, fsctx.GinCtx, c)
credential, err := fs.GetUploadToken(ctx, service.Path, service.Size, service.Name) credential, err := fs.GetUploadToken(ctx, service.Path, service.Size, service.Name)
if err != nil { if err != nil {

@ -263,10 +263,7 @@ func (service *Service) List(c *gin.Context) serializer.Response {
return serializer.Response{ return serializer.Response{
Code: 0, Code: 0,
Data: map[string]interface{}{ Data: serializer.BuildObjectList(0, objects, nil),
"parent": "0000",
"objects": objects,
},
} }
} }

Loading…
Cancel
Save