mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Modify: change raw object ID to Hash ID in file service
This commit is contained in:
@@ -94,8 +94,8 @@ func (fs *FileSystem) GetPhysicalFileContent(ctx context.Context, path string) (
|
||||
// path - 文件虚拟路径
|
||||
// isText - 是否为文本文件,文本文件会忽略重定向,直接由
|
||||
// 服务端拉取中转给用户,故会对文件大小进行限制
|
||||
func (fs *FileSystem) Preview(ctx context.Context, path string, isText bool) (*response.ContentResponse, error) {
|
||||
err := fs.resetFileIfNotExist(ctx, path)
|
||||
func (fs *FileSystem) Preview(ctx context.Context, id uint, isText bool) (*response.ContentResponse, error) {
|
||||
err := fs.resetFileIDIfNotExist(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -108,7 +108,7 @@ func (fs *FileSystem) Preview(ctx context.Context, path string, isText bool) (*r
|
||||
|
||||
// 是否直接返回文件内容
|
||||
if isText || fs.Policy.IsDirectlyPreview() {
|
||||
resp, err := fs.GetDownloadContent(ctx, path)
|
||||
resp, err := fs.GetDownloadContent(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -132,9 +132,9 @@ func (fs *FileSystem) Preview(ctx context.Context, path string, isText bool) (*r
|
||||
}
|
||||
|
||||
// GetDownloadContent 获取用于下载的文件流
|
||||
func (fs *FileSystem) GetDownloadContent(ctx context.Context, path string) (response.RSCloser, error) {
|
||||
func (fs *FileSystem) GetDownloadContent(ctx context.Context, id uint) (response.RSCloser, error) {
|
||||
// 获取原始文件流
|
||||
rs, err := fs.GetContent(ctx, path)
|
||||
rs, err := fs.GetContent(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -145,7 +145,7 @@ func (fs *FileSystem) GetDownloadContent(ctx context.Context, path string) (resp
|
||||
}
|
||||
|
||||
// GetContent 获取文件内容,path为虚拟路径
|
||||
func (fs *FileSystem) GetContent(ctx context.Context, path string) (response.RSCloser, error) {
|
||||
func (fs *FileSystem) GetContent(ctx context.Context, id uint) (response.RSCloser, error) {
|
||||
// 触发`下载前`钩子
|
||||
err := fs.Trigger(ctx, "BeforeFileDownload")
|
||||
if err != nil {
|
||||
@@ -153,7 +153,7 @@ func (fs *FileSystem) GetContent(ctx context.Context, path string) (response.RSC
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = fs.resetFileIfNotExist(ctx, path)
|
||||
err = fs.resetFileIDIfNotExist(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -218,8 +218,8 @@ func (fs *FileSystem) GroupFileByPolicy(ctx context.Context, files []model.File)
|
||||
}
|
||||
|
||||
// GetDownloadURL 创建文件下载链接, timeout 为数据库中存储过期时间的字段
|
||||
func (fs *FileSystem) GetDownloadURL(ctx context.Context, path string, timeout string) (string, error) {
|
||||
err := fs.resetFileIfNotExist(ctx, path)
|
||||
func (fs *FileSystem) GetDownloadURL(ctx context.Context, id uint, timeout string) (string, error) {
|
||||
err := fs.resetFileIDIfNotExist(ctx, id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -240,7 +240,7 @@ func (fs *FileSystem) GetDownloadURL(ctx context.Context, path string, timeout s
|
||||
return source, nil
|
||||
}
|
||||
|
||||
// Source 获取可直接访问文件的外链地址
|
||||
// GetSource 获取可直接访问文件的外链地址
|
||||
func (fs *FileSystem) GetSource(ctx context.Context, fileID uint) (string, error) {
|
||||
// 查找文件记录
|
||||
err := fs.resetFileIDIfNotExist(ctx, fileID)
|
||||
@@ -340,3 +340,11 @@ func (fs *FileSystem) resetPolicyToFirstFile(ctx context.Context) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Search 搜索文件
|
||||
func (fs *FileSystem) Search(ctx context.Context, keywords string) ([]Object, error) {
|
||||
files, _ := model.GetFilesByKeywords(keywords, fs.User.ID)
|
||||
fs.SetTargetFile(&files)
|
||||
|
||||
return fs.listObjects(ctx, "/", files, nil, nil), nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
||||
"github.com/HFO4/cloudreve/pkg/hashid"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"path"
|
||||
@@ -18,7 +19,7 @@ import (
|
||||
|
||||
// Object 文件或者目录
|
||||
type Object struct {
|
||||
ID uint `json:"id"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Pic string `json:"pic"`
|
||||
@@ -262,36 +263,41 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu
|
||||
var childFolders []model.Folder
|
||||
var childFiles []model.File
|
||||
|
||||
// 分享文件的ID
|
||||
shareKey := ""
|
||||
if key, ok := ctx.Value(fsctx.ShareKeyCtx).(string); ok {
|
||||
shareKey = key
|
||||
}
|
||||
|
||||
// 获取子目录
|
||||
childFolders, _ = folder.GetChildFolder()
|
||||
|
||||
// 获取子文件
|
||||
childFiles, _ = folder.GetChildFiles()
|
||||
|
||||
return fs.listObjects(ctx, parentPath, childFiles, childFolders, pathProcessor), nil
|
||||
}
|
||||
|
||||
func (fs *FileSystem) listObjects(ctx context.Context, parent string, files []model.File, folders []model.Folder, pathProcessor func(string) string) []Object {
|
||||
// 分享文件的ID
|
||||
shareKey := ""
|
||||
if key, ok := ctx.Value(fsctx.ShareKeyCtx).(string); ok {
|
||||
shareKey = key
|
||||
}
|
||||
|
||||
// 汇总处理结果
|
||||
objects := make([]Object, 0, len(childFiles)+len(childFolders))
|
||||
objects := make([]Object, 0, len(files)+len(folders))
|
||||
|
||||
// 所有对象的父目录
|
||||
var processedPath string
|
||||
|
||||
for _, subFolder := range childFolders {
|
||||
for _, subFolder := range folders {
|
||||
// 路径处理钩子,
|
||||
// 所有对象父目录都是一样的,所以只处理一次
|
||||
if processedPath == "" {
|
||||
if pathProcessor != nil {
|
||||
processedPath = pathProcessor(parentPath)
|
||||
processedPath = pathProcessor(parent)
|
||||
} else {
|
||||
processedPath = parentPath
|
||||
processedPath = parent
|
||||
}
|
||||
}
|
||||
|
||||
objects = append(objects, Object{
|
||||
ID: subFolder.ID,
|
||||
ID: hashid.HashID(subFolder.ID, hashid.FolderID),
|
||||
Name: subFolder.Name,
|
||||
Path: processedPath,
|
||||
Pic: "",
|
||||
@@ -301,17 +307,17 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu
|
||||
})
|
||||
}
|
||||
|
||||
for _, file := range childFiles {
|
||||
for _, file := range files {
|
||||
if processedPath == "" {
|
||||
if pathProcessor != nil {
|
||||
processedPath = pathProcessor(parentPath)
|
||||
processedPath = pathProcessor(parent)
|
||||
} else {
|
||||
processedPath = parentPath
|
||||
processedPath = parent
|
||||
}
|
||||
}
|
||||
|
||||
newFile := Object{
|
||||
ID: file.ID,
|
||||
ID: hashid.HashID(file.ID, hashid.FileID),
|
||||
Name: file.Name,
|
||||
Path: processedPath,
|
||||
Pic: file.PicInfo,
|
||||
@@ -325,7 +331,7 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu
|
||||
objects = append(objects, newFile)
|
||||
}
|
||||
|
||||
return objects, nil
|
||||
return objects
|
||||
}
|
||||
|
||||
// CreateDirectory 根据给定的完整创建目录,支持递归创建
|
||||
|
||||
Reference in New Issue
Block a user