Modify: change raw object ID to Hash ID in share service
This commit is contained in:
parent
9be1b4366f
commit
8f28a9c346
4 changed files with 45 additions and 26 deletions
|
@ -195,7 +195,7 @@ func (fs *FileSystem) doCompress(ctx context.Context, file *model.File, folder *
|
|||
|
||||
// Decompress 解压缩给定压缩文件到dst目录
|
||||
func (fs *FileSystem) Decompress(ctx context.Context, src, dst string) error {
|
||||
err := fs.resetFileIfNotExist(ctx, src)
|
||||
err := fs.ResetFileIfNotExist(ctx, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -285,8 +285,8 @@ func (fs *FileSystem) signURL(ctx context.Context, file *model.File, ttl int64,
|
|||
return source, nil
|
||||
}
|
||||
|
||||
// resetFileIfNotExist 重设当前目标文件为 path,如果当前目标为空
|
||||
func (fs *FileSystem) resetFileIfNotExist(ctx context.Context, path string) error {
|
||||
// ResetFileIfNotExist 重设当前目标文件为 path,如果当前目标为空
|
||||
func (fs *FileSystem) ResetFileIfNotExist(ctx context.Context, path string) error {
|
||||
// 找到文件
|
||||
if len(fs.FileTarget) == 0 {
|
||||
exist, file := fs.IsFileExist(path)
|
||||
|
@ -300,7 +300,7 @@ func (fs *FileSystem) resetFileIfNotExist(ctx context.Context, path string) erro
|
|||
return fs.resetPolicyToFirstFile(ctx)
|
||||
}
|
||||
|
||||
// resetFileIfNotExist 重设当前目标文件为 id,如果当前目标为空
|
||||
// ResetFileIfNotExist 重设当前目标文件为 id,如果当前目标为空
|
||||
func (fs *FileSystem) resetFileIDIfNotExist(ctx context.Context, id uint) error {
|
||||
// 找到文件
|
||||
if len(fs.FileTarget) == 0 {
|
||||
|
|
|
@ -131,19 +131,26 @@ func (service *FileIDService) CreateDocPreviewSession(ctx context.Context, c *gi
|
|||
}
|
||||
defer fs.Recycle()
|
||||
|
||||
// 获取对象id
|
||||
objectID, _ := c.Get("object_id")
|
||||
|
||||
// 如果上下文中已有File对象,则重设目标
|
||||
if file, ok := ctx.Value(fsctx.FileModelCtx).(*model.File); ok {
|
||||
fs.SetTargetFile(&[]model.File{*file})
|
||||
objectID = uint(0)
|
||||
}
|
||||
|
||||
// 重设根目录
|
||||
// 如果上下文中已有Folder对象,则重设根目录
|
||||
if folder, ok := ctx.Value(fsctx.FolderModelCtx).(*model.Folder); ok {
|
||||
fs.Root = folder
|
||||
path := ctx.Value(fsctx.PathCtx).(string)
|
||||
err := fs.ResetFileIfNotExist(ctx, path)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeNotFound, err.Error(), err)
|
||||
}
|
||||
objectID = uint(0)
|
||||
}
|
||||
|
||||
// 获取对象id
|
||||
objectID, _ := c.Get("object_id")
|
||||
|
||||
// 获取文件临时下载地址
|
||||
downloadURL, err := fs.GetDownloadURL(ctx, objectID.(uint), "doc_preview_timeout")
|
||||
if err != nil {
|
||||
|
@ -237,19 +244,26 @@ func (service *FileIDService) PreviewContent(ctx context.Context, c *gin.Context
|
|||
}
|
||||
defer fs.Recycle()
|
||||
|
||||
// 获取对象id
|
||||
objectID, _ := c.Get("object_id")
|
||||
|
||||
// 如果上下文中已有File对象,则重设目标
|
||||
if file, ok := ctx.Value(fsctx.FileModelCtx).(*model.File); ok {
|
||||
fs.SetTargetFile(&[]model.File{*file})
|
||||
objectID = uint(0)
|
||||
}
|
||||
|
||||
// 如果上下文中已有Folder对象,则重设根目录
|
||||
if folder, ok := ctx.Value(fsctx.FolderModelCtx).(*model.Folder); ok {
|
||||
fs.Root = folder
|
||||
path := ctx.Value(fsctx.PathCtx).(string)
|
||||
err := fs.ResetFileIfNotExist(ctx, path)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeNotFound, err.Error(), err)
|
||||
}
|
||||
objectID = uint(0)
|
||||
}
|
||||
|
||||
// 获取对象id
|
||||
objectID, _ := c.Get("object_id")
|
||||
|
||||
// 获取文件预览响应
|
||||
resp, err := fs.Preview(ctx, objectID.(uint), isText)
|
||||
if err != nil {
|
||||
|
|
|
@ -13,7 +13,6 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ShareGetService 获取分享服务
|
||||
|
@ -29,9 +28,9 @@ type Service struct {
|
|||
|
||||
// ArchiveService 分享归档下载服务
|
||||
type ArchiveService struct {
|
||||
Path string `json:"path" binding:"required,max=65535"`
|
||||
Items []uint `json:"items" binding:"exists"`
|
||||
Dirs []uint `json:"dirs" binding:"exists"`
|
||||
Path string `json:"path" binding:"required,max=65535"`
|
||||
Items []string `json:"items" binding:"exists"`
|
||||
Dirs []string `json:"dirs" binding:"exists"`
|
||||
}
|
||||
|
||||
// Get 获取分享内容
|
||||
|
@ -90,14 +89,21 @@ func (service *Service) CreateDownloadSession(c *gin.Context) serializer.Respons
|
|||
return serializer.Err(serializer.CodePolicyNotAllowed, "源文件不存在", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// 重设根目录
|
||||
if share.IsDir {
|
||||
fs.Root = &fs.DirTarget[0]
|
||||
|
||||
// 找到目标文件
|
||||
err = fs.ResetFileIfNotExist(ctx, service.Path)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
||||
}
|
||||
}
|
||||
|
||||
// 取得下载地址
|
||||
// TODO 改为真实ID
|
||||
downloadURL, err := fs.GetDownloadURL(context.Background(), 0, "download_timeout")
|
||||
downloadURL, err := fs.GetDownloadURL(ctx, 0, "download_timeout")
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
||||
}
|
||||
|
@ -117,6 +123,7 @@ func (service *Service) PreviewContent(ctx context.Context, c *gin.Context, isTe
|
|||
// 用于调下层service
|
||||
if share.IsDir {
|
||||
ctx = context.WithValue(ctx, fsctx.FolderModelCtx, share.Source())
|
||||
ctx = context.WithValue(ctx, fsctx.PathCtx, service.Path)
|
||||
} else {
|
||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, share.Source())
|
||||
}
|
||||
|
@ -134,6 +141,7 @@ func (service *Service) CreateDocPreviewSession(c *gin.Context) serializer.Respo
|
|||
ctx := context.Background()
|
||||
if share.IsDir {
|
||||
ctx = context.WithValue(ctx, fsctx.FolderModelCtx, share.Source())
|
||||
ctx = context.WithValue(ctx, fsctx.PathCtx, service.Path)
|
||||
} else {
|
||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, share.Source())
|
||||
}
|
||||
|
@ -212,15 +220,10 @@ func (service *Service) List(c *gin.Context) serializer.Response {
|
|||
return serializer.Err(serializer.CodeCreateFolderFailed, err.Error(), err)
|
||||
}
|
||||
|
||||
var parentID uint
|
||||
if len(fs.DirTarget) > 0 {
|
||||
parentID = fs.DirTarget[0].ID
|
||||
}
|
||||
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
Data: map[string]interface{}{
|
||||
"parent": parentID,
|
||||
"parent": "0000",
|
||||
"objects": objects,
|
||||
},
|
||||
}
|
||||
|
@ -254,7 +257,7 @@ func (service *Service) Thumb(c *gin.Context) serializer.Response {
|
|||
ctx := context.WithValue(context.Background(), fsctx.LimitParentCtx, parent)
|
||||
|
||||
// 获取文件ID
|
||||
fileID, err := strconv.ParseUint(c.Param("file"), 10, 32)
|
||||
fileID, err := hashid.DecodeHashID(c.Param("file"), hashid.FileID)
|
||||
if err != nil {
|
||||
return serializer.ParamErr("无法解析文件ID", err)
|
||||
}
|
||||
|
@ -318,8 +321,10 @@ func (service *ArchiveService) Archive(c *gin.Context) serializer.Response {
|
|||
tempUser.Group.OptionsSerialized.ArchiveDownload = true
|
||||
c.Set("user", tempUser)
|
||||
|
||||
// todo 改成真实
|
||||
subService := explorer.ItemIDService{}
|
||||
subService := explorer.ItemIDService{
|
||||
Dirs: service.Dirs,
|
||||
Items: service.Items,
|
||||
}
|
||||
|
||||
return subService.Archive(ctx, c)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue