Feat: thumb for shared folder

pull/247/head
HFO4 5 years ago
parent 9fe451ad4b
commit 437bdf46d4

@ -311,6 +311,13 @@ func (fs *FileSystem) resetFileIDIfNotExist(ctx context.Context, id uint) error
fs.FileTarget = []model.File{file[0]} fs.FileTarget = []model.File{file[0]}
} }
// 如果上下文限制了父目录,则进行检查
if parent, ok := ctx.Value(fsctx.LimitParentCtx).(*model.Folder); ok {
if parent.ID != fs.FileTarget[0].FolderID {
return ErrObjectNotExist
}
}
// 将当前存储策略重设为文件使用的 // 将当前存储策略重设为文件使用的
return fs.resetPolicyToFirstFile(ctx) return fs.resetPolicyToFirstFile(ctx)
} }

@ -160,3 +160,16 @@ func ArchiveShare(c *gin.Context) {
c.JSON(200, ErrorResponse(err)) c.JSON(200, ErrorResponse(err))
} }
} }
// ShareThumb 获取分享目录下文件的缩略图
func ShareThumb(c *gin.Context) {
var service share.Service
if err := c.ShouldBindQuery(&service); err == nil {
res := service.Thumb(c)
if res.Code >= 0 {
c.JSON(200, res)
}
} else {
c.JSON(200, ErrorResponse(err))
}
}

@ -214,6 +214,12 @@ func InitMasterRouter() *gin.Engine {
middleware.CheckShareUnlocked(), middleware.CheckShareUnlocked(),
controllers.PreviewShareReadme, controllers.PreviewShareReadme,
) )
// 获取缩略图
share.GET("thumb/:id/:file",
middleware.CheckShareUnlocked(),
middleware.ShareCanPreview(),
controllers.ShareThumb,
)
} }
// 需要登录保护的 // 需要登录保护的

@ -11,7 +11,9 @@ import (
"github.com/HFO4/cloudreve/pkg/util" "github.com/HFO4/cloudreve/pkg/util"
"github.com/HFO4/cloudreve/service/explorer" "github.com/HFO4/cloudreve/service/explorer"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http"
"path" "path"
"strconv"
) )
// ShareGetService 获取分享服务 // ShareGetService 获取分享服务
@ -219,6 +221,58 @@ func (service *Service) List(c *gin.Context) serializer.Response {
} }
} }
// Thumb 获取被分享文件的缩略图
func (service *Service) Thumb(c *gin.Context) serializer.Response {
shareCtx, _ := c.Get("share")
share := shareCtx.(*model.Share)
if !share.IsDir {
return serializer.ParamErr("此分享无缩略图", nil)
}
// 创建文件系统
fs, err := filesystem.NewFileSystem(share.GetCreator())
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
}
defer fs.Recycle()
// 重设根目录
fs.Root = share.GetSource().(*model.Folder)
// 找到缩略图的父目录
exist, parent := fs.IsPathExist(service.Path)
if !exist {
return serializer.Err(serializer.CodeNotFound, "路径不存在", nil)
}
ctx := context.WithValue(context.Background(), fsctx.LimitParentCtx, parent)
// 获取文件ID
fileID, err := strconv.ParseUint(c.Param("file"), 10, 32)
if err != nil {
return serializer.ParamErr("无法解析文件ID", err)
}
// 获取缩略图
resp, err := fs.GetThumb(ctx, uint(fileID))
if err != nil {
return serializer.Err(serializer.CodeNotSet, "无法获取缩略图", err)
}
if resp.Redirect {
c.Header("Cache-Control", fmt.Sprintf("max-age=%d", resp.MaxAge))
c.Redirect(http.StatusMovedPermanently, resp.URL)
return serializer.Response{Code: -1}
}
defer resp.Content.Close()
http.ServeContent(c.Writer, c.Request, "thumb.png", fs.FileTarget[0].UpdatedAt, resp.Content)
return serializer.Response{Code: -1}
}
// Archive 创建批量下载归档 // Archive 创建批量下载归档
func (service *ArchiveService) Archive(c *gin.Context) serializer.Response { func (service *ArchiveService) Archive(c *gin.Context) serializer.Response {
shareCtx, _ := c.Get("share") shareCtx, _ := c.Get("share")
@ -251,6 +305,7 @@ func (service *ArchiveService) Archive(c *gin.Context) serializer.Response {
return serializer.Err(serializer.CodeNotFound, "路径不存在", nil) return serializer.Err(serializer.CodeNotFound, "路径不存在", nil)
} }
// 限制操作范围为父目录下
ctx := context.WithValue(context.Background(), fsctx.LimitParentCtx, parent) ctx := context.WithValue(context.Background(), fsctx.LimitParentCtx, parent)
// 用于调下层service // 用于调下层service

Loading…
Cancel
Save