Modify: preview should use login session to enable http-range

pull/247/head
HFO4 5 years ago
parent 8557ed8e2e
commit 2c75c73886

@ -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())

@ -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)
// 取得文件外链

@ -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,
}
}

Loading…
Cancel
Save