Feat: preview doc file / Fix: chinese character encoding in attachment header
This commit is contained in:
parent
9a942f8b48
commit
e6d2a94809
4 changed files with 47 additions and 2 deletions
|
@ -102,6 +102,7 @@ solid #e9e9e9;"bgcolor="#fff"><tbody><tr style="font-family: 'Helvetica Neue',He
|
|||
{Name: "oss_timeout", Value: `3600`, Type: "timeout"},
|
||||
{Name: "archive_timeout", Value: `30`, Type: "timeout"},
|
||||
{Name: "download_timeout", Value: `30`, Type: "timeout"},
|
||||
{Name: "doc_preview_timeout", Value: `60`, Type: "timeout"},
|
||||
{Name: "allowdVisitorDownload", Value: `false`, Type: "share"},
|
||||
{Name: "login_captcha", Value: `0`, Type: "login"},
|
||||
{Name: "qq_login", Value: `0`, Type: "login"},
|
||||
|
|
|
@ -158,6 +158,21 @@ func Preview(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
// GetDocPreview 获取DOC文件预览地址
|
||||
func GetDocPreview(c *gin.Context) {
|
||||
// 创建上下文
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service explorer.SingleFileService
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
res := service.CreateDocPreviewSession(ctx, c)
|
||||
c.JSON(200, res)
|
||||
} else {
|
||||
c.JSON(200, ErrorResponse(err))
|
||||
}
|
||||
}
|
||||
|
||||
// CreateDownloadSession 创建文件下载会话
|
||||
func CreateDownloadSession(c *gin.Context) {
|
||||
// 创建上下文
|
||||
|
|
|
@ -110,6 +110,8 @@ func InitRouter() *gin.Engine {
|
|||
file.PUT("download/*path", controllers.CreateDownloadSession)
|
||||
// 预览文件
|
||||
file.GET("preview/*path", controllers.Preview)
|
||||
// 取得Office文档预览地址
|
||||
file.GET("doc/*path", controllers.GetDocPreview)
|
||||
// 获取缩略图
|
||||
file.GET("thumb/:id", controllers.Thumb)
|
||||
// 取得文件外链
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
@ -95,6 +96,32 @@ func (service *FileAnonymousGetService) Download(ctx context.Context, c *gin.Con
|
|||
}
|
||||
}
|
||||
|
||||
// CreateDocPreviewSession 创建DOC文件预览会话,返回预览地址
|
||||
func (service *SingleFileService) CreateDocPreviewSession(ctx context.Context, c *gin.Context) serializer.Response {
|
||||
// 创建文件系统
|
||||
fs, err := filesystem.NewFileSystemFromContext(c)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
|
||||
}
|
||||
|
||||
// 获取文件临时下载地址
|
||||
downloadURL, err := fs.GetDownloadURL(ctx, service.Path, "doc_preview_timeout")
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
||||
}
|
||||
|
||||
// 生成最终的预览器地址
|
||||
viewerBase, _ := url.Parse("https://view.officeapps.live.com/op/view.aspx")
|
||||
params := viewerBase.Query()
|
||||
params.Set("src", downloadURL)
|
||||
viewerBase.RawQuery = params.Encode()
|
||||
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
Data: viewerBase.String(),
|
||||
}
|
||||
}
|
||||
|
||||
// CreateDownloadSession 创建下载会话,获取下载URL
|
||||
func (service *SingleFileService) CreateDownloadSession(ctx context.Context, c *gin.Context) serializer.Response {
|
||||
// 创建文件系统
|
||||
|
@ -139,7 +166,7 @@ func (service *DownloadService) Download(ctx context.Context, c *gin.Context) se
|
|||
}
|
||||
|
||||
// 设置文件名
|
||||
c.Header("Content-Disposition", "attachment; filename=\""+fs.FileTarget[0].Name+"\"")
|
||||
c.Header("Content-Disposition", "attachment; filename=\""+url.PathEscape(fs.FileTarget[0].Name)+"\"")
|
||||
|
||||
if fs.User.Group.OptionsSerialized.OneTimeDownloadEnabled {
|
||||
// 清理资源,删除临时文件
|
||||
|
@ -147,7 +174,7 @@ func (service *DownloadService) Download(ctx context.Context, c *gin.Context) se
|
|||
}
|
||||
|
||||
// 发送文件
|
||||
http.ServeContent(c.Writer, c.Request, "", fs.FileTarget[0].UpdatedAt, rs)
|
||||
http.ServeContent(c.Writer, c.Request, fs.FileTarget[0].Name, fs.FileTarget[0].UpdatedAt, rs)
|
||||
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
|
|
Loading…
Add table
Reference in a new issue