From 2c75c73886f09843881a1853ba74fdf04d18641a Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sat, 14 Dec 2019 15:06:30 +0800 Subject: [PATCH] Modify: preview should use login session to enable http-range --- routers/controllers/file.go | 16 +++++++++------- routers/router.go | 4 ++-- service/explorer/file.go | 24 +++++++++++++++++++++++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/routers/controllers/file.go b/routers/controllers/file.go index 3f6fcd4..79ae686 100644 --- a/routers/controllers/file.go +++ b/routers/controllers/file.go @@ -137,20 +137,22 @@ func Thumb(c *gin.Context) { } -// RedirectToDownload 创建下载会话并重定向至下载地址 -func RedirectToDownload(c *gin.Context) { +// Preview 预览文件 +func Preview(c *gin.Context) { // 创建上下文 ctx, cancel := context.WithCancel(context.Background()) defer cancel() var service explorer.FileDownloadCreateService if err := c.ShouldBindUri(&service); err == nil { - res := service.CreateDownloadSession(ctx, c) - if res.Code == 0 { + res := service.PreviewContent(ctx, c) + // 是否需要重定向 + if res.Code == -301 { c.Redirect(301, res.Data.(string)) - return } - c.JSON(200, res) + if res.Code != 0 { + c.JSON(200, res) + } } else { c.JSON(200, ErrorResponse(err)) } @@ -171,7 +173,7 @@ func CreateDownloadSession(c *gin.Context) { } } -// DownloadArchived 文件下载 +// Download 文件下载 func Download(c *gin.Context) { // 创建上下文 ctx, cancel := context.WithCancel(context.Background()) diff --git a/routers/router.go b/routers/router.go index 928fa68..ccea9cf 100644 --- a/routers/router.go +++ b/routers/router.go @@ -106,8 +106,8 @@ func InitRouter() *gin.Engine { file.POST("upload", controllers.FileUploadStream) // 创建文件下载会话 file.PUT("download/*path", controllers.CreateDownloadSession) - // 创建文件下载并重定向到下载地址 - file.GET("redirect/*path", controllers.RedirectToDownload) + // 预览文件 + file.GET("preview/*path", controllers.Preview) // 获取缩略图 file.GET("thumb/:id", controllers.Thumb) // 取得文件外链 diff --git a/service/explorer/file.go b/service/explorer/file.go index a51a6c0..f68654d 100644 --- a/service/explorer/file.go +++ b/service/explorer/file.go @@ -112,7 +112,7 @@ func (service *FileDownloadCreateService) CreateDownloadSession(ctx context.Cont } } -// Download 文件下载 +// Download 通过签名URL的文件下载,无需登录 func (service *DownloadService) Download(ctx context.Context, c *gin.Context) serializer.Response { // 创建文件系统 fs, err := filesystem.NewFileSystemFromContext(c) @@ -150,3 +150,25 @@ func (service *DownloadService) Download(ctx context.Context, c *gin.Context) se Code: 0, } } + +// PreviewContent 预览文件,需要登录会话 +func (service *FileDownloadCreateService) PreviewContent(ctx context.Context, c *gin.Context) serializer.Response { + // 创建文件系统 + fs, err := filesystem.NewFileSystemFromContext(c) + if err != nil { + return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err) + } + + // 获取文件流 + rs, err := fs.GetDownloadContent(ctx, service.Path) + if err != nil { + return serializer.Err(serializer.CodeNotSet, err.Error(), err) + } + defer rs.Close() + + http.ServeContent(c.Writer, c.Request, fs.FileTarget[0].Name, fs.FileTarget[0].UpdatedAt, rs) + + return serializer.Response{ + Code: 0, + } +}