Feat: download / preview files for shared folder

pull/247/head
HFO4 5 years ago
parent 0edbbfc9ea
commit b1a9943b0c

@ -13,6 +13,8 @@ const (
PathCtx
// FileModelCtx 文件数据库模型
FileModelCtx
// FolderModelCtx 目录数据库模型
FolderModelCtx
// HTTPCtx HTTP请求的上下文
HTTPCtx
// UploadPolicyCtx 上传策略一般为slave模式下使用
@ -25,4 +27,6 @@ const (
OriginSourceNameCtx
// FileSizeCtx 文件大小
FileSizeCtx
// ShareKeyCtx 分享文件的 HashID
ShareKeyCtx
)

@ -4,6 +4,7 @@ import (
"context"
"fmt"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/HFO4/cloudreve/pkg/util"
"path"
@ -24,6 +25,7 @@ type Object struct {
Size uint64 `json:"size"`
Type string `json:"type"`
Date string `json:"date"`
Key string `json:"key,omitempty"`
}
// Rename 重命名对象
@ -260,6 +262,12 @@ 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()
@ -302,7 +310,7 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu
}
}
objects = append(objects, Object{
newFile := Object{
ID: file.ID,
Name: file.Name,
Path: processedPath,
@ -310,7 +318,11 @@ func (fs *FileSystem) List(ctx context.Context, dirPath string, pathProcessor fu
Size: file.Size,
Type: "file",
Date: file.CreatedAt.Format("2006-01-02 15:04:05"),
})
}
if shareKey != "" {
newFile.Key = shareKey
}
objects = append(objects, newFile)
}
return objects, nil

@ -173,7 +173,7 @@ func InitMasterRouter() *gin.Engine {
// 获取分享
share.GET("info/:id", controllers.GetShare)
// 创建文件下载会话
share.POST("download/:id",
share.PUT("download/:id",
middleware.CheckShareUnlocked(),
middleware.BeforeShareDownload(),
controllers.GetShareDownload,

@ -133,6 +133,11 @@ func (service *SingleFileService) CreateDocPreviewSession(ctx context.Context, c
fs.SetTargetFile(&[]model.File{*file})
}
// 如果上下文中已有Folder对象则重设目标
if folder, ok := ctx.Value(fsctx.FolderModelCtx).(*model.Folder); ok {
fs.SetTargetDir(&[]model.Folder{*folder})
}
// 获取文件临时下载地址
downloadURL, err := fs.GetDownloadURL(ctx, service.Path, "doc_preview_timeout")
if err != nil {
@ -228,6 +233,11 @@ func (service *SingleFileService) PreviewContent(ctx context.Context, c *gin.Con
fs.SetTargetFile(&[]model.File{*file})
}
// 如果上下文中已有Folder对象则重设目标
if folder, ok := ctx.Value(fsctx.FolderModelCtx).(*model.Folder); ok {
fs.SetTargetDir(&[]model.Folder{*folder})
}
// 获取文件预览响应
resp, err := fs.Preview(ctx, service.Path, isText)
if err != nil {

@ -6,6 +6,7 @@ import (
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem"
"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"
"github.com/HFO4/cloudreve/service/explorer"
@ -81,7 +82,7 @@ func (service *ShareService) CreateDownloadSession(c *gin.Context) serializer.Re
}
// 取得下载地址
downloadURL, err := fs.GetDownloadURL(context.Background(), "", "download_timeout")
downloadURL, err := fs.GetDownloadURL(context.Background(), service.Path, "download_timeout")
if err != nil {
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
}
@ -99,9 +100,13 @@ func (service *ShareService) PreviewContent(ctx context.Context, c *gin.Context,
share := shareCtx.(*model.Share)
// 用于调下层service
ctx = context.WithValue(ctx, fsctx.FileModelCtx, share.GetSource())
if share.IsDir {
ctx = context.WithValue(ctx, fsctx.FolderModelCtx, share.GetSource())
} else {
ctx = context.WithValue(ctx, fsctx.FileModelCtx, share.GetSource())
}
subService := explorer.SingleFileService{
Path: "",
Path: service.Path,
}
return subService.PreviewContent(ctx, c, isText)
@ -113,9 +118,14 @@ func (service *ShareService) CreateDocPreviewSession(c *gin.Context) serializer.
share := shareCtx.(*model.Share)
// 用于调下层service
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, share.GetSource())
ctx := context.Background()
if share.IsDir {
ctx = context.WithValue(ctx, fsctx.FolderModelCtx, share.GetSource())
} else {
ctx = context.WithValue(ctx, fsctx.FileModelCtx, share.GetSource())
}
subService := explorer.SingleFileService{
Path: "",
Path: service.Path,
}
return subService.CreateDocPreviewSession(ctx, c)
@ -182,6 +192,9 @@ func (service *ShareService) List(c *gin.Context) serializer.Response {
fs.SetTargetDir(&[]model.Folder{*share.GetSource().(*model.Folder)})
fs.DirTarget[0].Name = "/"
// 分享Key上下文
ctx = context.WithValue(ctx, fsctx.ShareKeyCtx, hashid.HashID(share.ID, hashid.ShareID))
// 获取子项目
objects, err := fs.List(ctx, service.Path, nil)
if err != nil {

Loading…
Cancel
Save