Feat: list files for shared folder
This commit is contained in:
parent
a0c320b964
commit
0edbbfc9ea
7 changed files with 115 additions and 26 deletions
|
@ -49,6 +49,30 @@ func ShareCanPreview() gin.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// CheckShareUnlocked 检查分享是否已解锁
|
||||
func CheckShareUnlocked() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if shareCtx, ok := c.Get("share"); ok {
|
||||
share := shareCtx.(*model.Share)
|
||||
// 分享是否已解锁
|
||||
if share.Password != "" {
|
||||
sessionKey := fmt.Sprintf("share_unlock_%d", share.ID)
|
||||
unlocked := util.GetSession(c, sessionKey) != nil
|
||||
if !unlocked {
|
||||
c.JSON(200, serializer.Err(serializer.CodeNoPermissionErr,
|
||||
"无权访问此分享", nil))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
|
||||
// BeforeShareDownload 分享被下载前的检查
|
||||
func BeforeShareDownload() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
@ -66,18 +90,6 @@ func BeforeShareDownload() gin.HandlerFunc {
|
|||
return
|
||||
}
|
||||
|
||||
// 分享是否已解锁
|
||||
if share.Password != "" {
|
||||
sessionKey := fmt.Sprintf("share_unlock_%d", share.ID)
|
||||
unlocked := util.GetSession(c, sessionKey) != nil
|
||||
if !unlocked {
|
||||
c.JSON(200, serializer.Err(serializer.CodeNoPermissionErr,
|
||||
"无权访问此分享", nil))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 对积分、下载次数进行更新
|
||||
err = share.DownloadBy(user, c)
|
||||
if err != nil {
|
||||
|
|
|
@ -20,13 +20,22 @@ func (fs *FileSystem) IsPathExist(path string) (bool, *model.Folder) {
|
|||
}
|
||||
|
||||
// 递归步入目录
|
||||
// TODO:装入树形缓存
|
||||
// TODO:测试新增
|
||||
var currentFolder *model.Folder
|
||||
|
||||
// 如果已设定目录对象,则从给定目录向下遍历
|
||||
if len(fs.DirTarget) > 0 {
|
||||
currentFolder = &fs.DirTarget[0]
|
||||
}
|
||||
|
||||
for _, folderName := range pathList {
|
||||
var err error
|
||||
|
||||
// 根目录
|
||||
if folderName == "/" {
|
||||
if currentFolder != nil {
|
||||
continue
|
||||
}
|
||||
currentFolder, err = fs.User.Root()
|
||||
if err != nil {
|
||||
return false, nil
|
||||
|
|
|
@ -31,6 +31,8 @@ func BuildSiteConfig(settings map[string]string, user *model.User) Response {
|
|||
var userRes User
|
||||
if user != nil {
|
||||
userRes = BuildUser(*user)
|
||||
} else {
|
||||
userRes = BuildUser(*model.NewAnonymousUser())
|
||||
}
|
||||
return Response{
|
||||
Data: SiteConfig{
|
||||
|
|
|
@ -43,6 +43,7 @@ type group struct {
|
|||
AllowTorrentDownload bool `json:"allowTorrentDownload"`
|
||||
AllowArchiveDownload bool `json:"allowArchiveDownload"`
|
||||
ShareFreeEnabled bool `json:"shareFree"`
|
||||
ShareDownload bool `json:"shareDownload"`
|
||||
}
|
||||
|
||||
type storage struct {
|
||||
|
@ -78,6 +79,7 @@ func BuildUser(user model.User) User {
|
|||
AllowTorrentDownload: aria2Option[2],
|
||||
AllowArchiveDownload: user.Group.OptionsSerialized.ArchiveDownloadEnabled,
|
||||
ShareFreeEnabled: user.Group.OptionsSerialized.ShareFreeEnabled,
|
||||
ShareDownload: user.Group.OptionsSerialized.ShareDownloadEnabled,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ func GetShare(c *gin.Context) {
|
|||
|
||||
// GetShareDownload 创建分享下载会话
|
||||
func GetShareDownload(c *gin.Context) {
|
||||
var service share.SingleFileService
|
||||
var service share.ShareService
|
||||
if err := c.ShouldBindQuery(&service); err == nil {
|
||||
res := service.CreateDownloadSession(c)
|
||||
c.JSON(200, res)
|
||||
|
@ -45,7 +45,7 @@ func PreviewShare(c *gin.Context) {
|
|||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service share.SingleFileService
|
||||
var service share.ShareService
|
||||
if err := c.ShouldBindQuery(&service); err == nil {
|
||||
res := service.PreviewContent(ctx, c, false)
|
||||
// 是否需要重定向
|
||||
|
@ -68,7 +68,7 @@ func PreviewShareText(c *gin.Context) {
|
|||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service share.SingleFileService
|
||||
var service share.ShareService
|
||||
if err := c.ShouldBindQuery(&service); err == nil {
|
||||
res := service.PreviewContent(ctx, c, true)
|
||||
// 是否有错误发生
|
||||
|
@ -82,7 +82,7 @@ func PreviewShareText(c *gin.Context) {
|
|||
|
||||
// GetShareDocPreview 创建分享Office文档预览地址
|
||||
func GetShareDocPreview(c *gin.Context) {
|
||||
var service share.SingleFileService
|
||||
var service share.ShareService
|
||||
if err := c.ShouldBindQuery(&service); err == nil {
|
||||
res := service.CreateDocPreviewSession(c)
|
||||
c.JSON(200, res)
|
||||
|
@ -93,7 +93,7 @@ func GetShareDocPreview(c *gin.Context) {
|
|||
|
||||
// SaveShare 转存他人分享
|
||||
func SaveShare(c *gin.Context) {
|
||||
var service share.SingleFileService
|
||||
var service share.ShareService
|
||||
if err := c.ShouldBindJSON(&service); err == nil {
|
||||
res := service.SaveToMyFile(c)
|
||||
c.JSON(200, res)
|
||||
|
@ -101,3 +101,14 @@ func SaveShare(c *gin.Context) {
|
|||
c.JSON(200, ErrorResponse(err))
|
||||
}
|
||||
}
|
||||
|
||||
// ListSharedFolder 列出分享的目录下的对象
|
||||
func ListSharedFolder(c *gin.Context) {
|
||||
var service share.ShareService
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
res := service.List(c)
|
||||
c.JSON(200, res)
|
||||
} else {
|
||||
c.JSON(200, ErrorResponse(err))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -174,25 +174,35 @@ func InitMasterRouter() *gin.Engine {
|
|||
share.GET("info/:id", controllers.GetShare)
|
||||
// 创建文件下载会话
|
||||
share.POST("download/:id",
|
||||
middleware.CheckShareUnlocked(),
|
||||
middleware.BeforeShareDownload(),
|
||||
controllers.GetShareDownload,
|
||||
)
|
||||
// 预览分享文件
|
||||
share.GET("preview/:id",
|
||||
middleware.CheckShareUnlocked(),
|
||||
middleware.ShareCanPreview(),
|
||||
middleware.BeforeShareDownload(),
|
||||
controllers.PreviewShare,
|
||||
)
|
||||
// 取得Office文档预览地址
|
||||
share.GET("doc/:id", middleware.ShareCanPreview(),
|
||||
share.GET("doc/:id",
|
||||
middleware.CheckShareUnlocked(),
|
||||
middleware.ShareCanPreview(),
|
||||
middleware.BeforeShareDownload(),
|
||||
controllers.GetShareDocPreview,
|
||||
)
|
||||
// 获取文本文件内容
|
||||
share.GET("content/:id",
|
||||
middleware.CheckShareUnlocked(),
|
||||
middleware.BeforeShareDownload(),
|
||||
controllers.PreviewShareText,
|
||||
)
|
||||
// 分享目录列文件
|
||||
share.GET("list/:id/*path",
|
||||
middleware.CheckShareUnlocked(),
|
||||
controllers.ListSharedFolder,
|
||||
)
|
||||
}
|
||||
|
||||
// 需要登录保护的
|
||||
|
@ -272,6 +282,7 @@ func InitMasterRouter() *gin.Engine {
|
|||
// 转存他人分享
|
||||
share.POST("save/:id",
|
||||
middleware.ShareAvailable(),
|
||||
middleware.CheckShareUnlocked(),
|
||||
middleware.BeforeShareDownload(),
|
||||
controllers.SaveShare,
|
||||
)
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"github.com/HFO4/cloudreve/service/explorer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"path"
|
||||
)
|
||||
|
||||
// ShareGetService 获取分享服务
|
||||
|
@ -17,9 +18,10 @@ type ShareGetService struct {
|
|||
Password string `form:"password" binding:"max=255"`
|
||||
}
|
||||
|
||||
// SingleFileService 对单文件进行操作的服务,path为可选文件完整路径
|
||||
type SingleFileService struct {
|
||||
Path string `form:"path" binding:"max=65535"`
|
||||
// ShareService 对分享进行操作的服务,
|
||||
// path 为可选文件完整路径,在目录分享下有效
|
||||
type ShareService struct {
|
||||
Path string `form:"path" uri:"path" binding:"max=65535"`
|
||||
}
|
||||
|
||||
// Get 获取分享内容
|
||||
|
@ -59,7 +61,7 @@ func (service *ShareGetService) Get(c *gin.Context) serializer.Response {
|
|||
}
|
||||
|
||||
// CreateDownloadSession 创建下载会话
|
||||
func (service *SingleFileService) CreateDownloadSession(c *gin.Context) serializer.Response {
|
||||
func (service *ShareService) CreateDownloadSession(c *gin.Context) serializer.Response {
|
||||
shareCtx, _ := c.Get("share")
|
||||
share := shareCtx.(*model.Share)
|
||||
userCtx, _ := c.Get("user")
|
||||
|
@ -92,7 +94,7 @@ func (service *SingleFileService) CreateDownloadSession(c *gin.Context) serializ
|
|||
|
||||
// PreviewContent 预览文件,需要登录会话, isText - 是否为文本文件,文本文件会
|
||||
// 强制经由服务端中转
|
||||
func (service *SingleFileService) PreviewContent(ctx context.Context, c *gin.Context, isText bool) serializer.Response {
|
||||
func (service *ShareService) PreviewContent(ctx context.Context, c *gin.Context, isText bool) serializer.Response {
|
||||
shareCtx, _ := c.Get("share")
|
||||
share := shareCtx.(*model.Share)
|
||||
|
||||
|
@ -106,7 +108,7 @@ func (service *SingleFileService) PreviewContent(ctx context.Context, c *gin.Con
|
|||
}
|
||||
|
||||
// CreateDocPreviewSession 创建Office预览会话,返回预览地址
|
||||
func (service *SingleFileService) CreateDocPreviewSession(c *gin.Context) serializer.Response {
|
||||
func (service *ShareService) CreateDocPreviewSession(c *gin.Context) serializer.Response {
|
||||
shareCtx, _ := c.Get("share")
|
||||
share := shareCtx.(*model.Share)
|
||||
|
||||
|
@ -120,7 +122,7 @@ func (service *SingleFileService) CreateDocPreviewSession(c *gin.Context) serial
|
|||
}
|
||||
|
||||
// SaveToMyFile 将此分享转存到自己的网盘
|
||||
func (service *SingleFileService) SaveToMyFile(c *gin.Context) serializer.Response {
|
||||
func (service *ShareService) SaveToMyFile(c *gin.Context) serializer.Response {
|
||||
shareCtx, _ := c.Get("share")
|
||||
share := shareCtx.(*model.Share)
|
||||
userCtx, _ := c.Get("user")
|
||||
|
@ -151,3 +153,43 @@ func (service *SingleFileService) SaveToMyFile(c *gin.Context) serializer.Respon
|
|||
|
||||
return serializer.Response{}
|
||||
}
|
||||
|
||||
// List 列出分享的目录下的对象
|
||||
func (service *ShareService) List(c *gin.Context) serializer.Response {
|
||||
shareCtx, _ := c.Get("share")
|
||||
share := shareCtx.(*model.Share)
|
||||
|
||||
if !share.IsDir {
|
||||
return serializer.ParamErr("此分享无法列目录", nil)
|
||||
}
|
||||
|
||||
if !path.IsAbs(service.Path) {
|
||||
return serializer.ParamErr("路径无效", nil)
|
||||
}
|
||||
|
||||
// 创建文件系统
|
||||
fs, err := filesystem.NewFileSystem(share.GetCreator())
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
|
||||
}
|
||||
defer fs.Recycle()
|
||||
|
||||
// 上下文
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// 重设根目录
|
||||
fs.SetTargetDir(&[]model.Folder{*share.GetSource().(*model.Folder)})
|
||||
fs.DirTarget[0].Name = "/"
|
||||
|
||||
// 获取子项目
|
||||
objects, err := fs.List(ctx, service.Path, nil)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeCreateFolderFailed, err.Error(), err)
|
||||
}
|
||||
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
Data: objects,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue