Modify: Decide whether to redirect when previewing based on storage policy
This commit is contained in:
parent
eceee2fc76
commit
8a262a15d5
7 changed files with 144 additions and 34 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: "preview_timeout", Value: `60`, Type: "timeout"},
|
||||
{Name: "doc_preview_timeout", Value: `60`, Type: "timeout"},
|
||||
{Name: "upload_credential_timeout", Value: `1800`, Type: "timeout"},
|
||||
{Name: "upload_session_timeout", Value: `86400`, Type: "timeout"},
|
||||
|
|
|
@ -67,6 +67,12 @@ func GetPolicyByID(ID interface{}) (Policy, error) {
|
|||
return policy, result.Error
|
||||
}
|
||||
|
||||
// IsDirectlyPreview 返回此策略下文件是否可以直接预览(不需要重定向)
|
||||
// TODO 测试
|
||||
func (policy *Policy) IsDirectlyPreview() bool {
|
||||
return policy.Type == "local"
|
||||
}
|
||||
|
||||
// AfterFind 找到存储策略后的钩子
|
||||
func (policy *Policy) AfterFind() (err error) {
|
||||
// 解析存储策略设置到OptionsSerialized
|
||||
|
|
|
@ -87,6 +87,48 @@ func (fs *FileSystem) GetPhysicalFileContent(ctx context.Context, path string) (
|
|||
return fs.withSpeedLimit(rs), nil
|
||||
}
|
||||
|
||||
// Preview 预览文件
|
||||
// TODO 测试
|
||||
func (fs *FileSystem) Preview(ctx context.Context, path string) (*response.ContentResponse, error) {
|
||||
err := fs.resetFileIfNotExist(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 是否直接返回文件内容
|
||||
if fs.Policy.IsDirectlyPreview() {
|
||||
resp, err := fs.GetDownloadContent(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &response.ContentResponse{
|
||||
Redirect: false,
|
||||
Content: resp,
|
||||
}, nil
|
||||
}
|
||||
// 否则重定向到签名的预览URL
|
||||
ttl, err := strconv.ParseInt(model.GetSettingByName("preview_timeout"), 10, 64)
|
||||
if err != nil {
|
||||
return nil,
|
||||
serializer.NewError(
|
||||
serializer.CodeInternalSetting,
|
||||
"无法获取预览地址有效期设定",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
previewURL, err := fs.signURL(ctx, &fs.FileTarget[0], ttl, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &response.ContentResponse{
|
||||
Redirect: true,
|
||||
URL: previewURL,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
// GetDownloadContent 获取用于下载的文件流
|
||||
func (fs *FileSystem) GetDownloadContent(ctx context.Context, path string) (response.RSCloser, error) {
|
||||
// 获取原始文件流
|
||||
|
@ -109,22 +151,11 @@ func (fs *FileSystem) GetContent(ctx context.Context, path string) (response.RSC
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// 找到文件
|
||||
if len(fs.FileTarget) == 0 {
|
||||
exist, file := fs.IsFileExist(path)
|
||||
if !exist {
|
||||
return nil, ErrObjectNotExist
|
||||
}
|
||||
fs.FileTarget = []model.File{*file}
|
||||
}
|
||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, fs.FileTarget[0])
|
||||
|
||||
// 将当前存储策略重设为文件使用的
|
||||
fs.Policy = fs.FileTarget[0].GetPolicy()
|
||||
err = fs.dispatchHandler()
|
||||
err = fs.resetFileIfNotExist(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, fs.FileTarget[0])
|
||||
|
||||
// 获取文件流
|
||||
rs, err := fs.Handler.Get(ctx, fs.FileTarget[0].SourceName)
|
||||
|
@ -186,17 +217,11 @@ func (fs *FileSystem) GroupFileByPolicy(ctx context.Context, files []model.File)
|
|||
|
||||
// GetDownloadURL 创建文件下载链接, timeout 为数据库中存储过期时间的字段
|
||||
func (fs *FileSystem) GetDownloadURL(ctx context.Context, path string, timeout string) (string, error) {
|
||||
var fileTarget *model.File
|
||||
// 找到文件
|
||||
if len(fs.FileTarget) == 0 {
|
||||
exist, file := fs.IsFileExist(path)
|
||||
if !exist {
|
||||
return "", ErrObjectNotExist
|
||||
}
|
||||
fileTarget = file
|
||||
} else {
|
||||
fileTarget = &fs.FileTarget[0]
|
||||
err := fs.resetFileIfNotExist(ctx, path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
fileTarget := &fs.FileTarget[0]
|
||||
|
||||
// 生成下載地址
|
||||
ttl, err := strconv.ParseInt(model.GetSettingByName(timeout), 10, 64)
|
||||
|
@ -251,9 +276,7 @@ func (fs *FileSystem) signURL(ctx context.Context, file *model.File, ttl int64,
|
|||
fs.FileTarget = []model.File{*file}
|
||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, *file)
|
||||
|
||||
// 将当前存储策略重设为文件使用的
|
||||
fs.Policy = file.GetPolicy()
|
||||
err := fs.dispatchHandler()
|
||||
err := fs.resetPolicyToFirstFile(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -268,3 +291,31 @@ 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 {
|
||||
// 找到文件
|
||||
if len(fs.FileTarget) == 0 {
|
||||
exist, file := fs.IsFileExist(path)
|
||||
if !exist {
|
||||
return ErrObjectNotExist
|
||||
}
|
||||
fs.FileTarget = []model.File{*file}
|
||||
}
|
||||
|
||||
// 将当前存储策略重设为文件使用的
|
||||
return fs.resetPolicyToFirstFile(ctx)
|
||||
}
|
||||
|
||||
// resetPolicyToFirstFile 将当前存储策略重设为第一个目标文件文件使用的
|
||||
func (fs *FileSystem) resetPolicyToFirstFile(ctx context.Context) error {
|
||||
if len(fs.FileTarget) == 0 {
|
||||
return ErrObjectNotExist
|
||||
}
|
||||
fs.Policy = fs.FileTarget[0].GetPolicy()
|
||||
err := fs.dispatchHandler()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -3,9 +3,12 @@ package remote
|
|||
// TODO 测试
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/auth"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
||||
"github.com/HFO4/cloudreve/pkg/filesystem/response"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"io"
|
||||
|
@ -42,6 +45,7 @@ func (handler Handler) Thumb(ctx context.Context, path string) (*response.Conten
|
|||
}
|
||||
|
||||
// Source 获取外链URL
|
||||
// TODO 测试
|
||||
func (handler Handler) Source(
|
||||
ctx context.Context,
|
||||
path string,
|
||||
|
@ -49,11 +53,47 @@ func (handler Handler) Source(
|
|||
ttl int64,
|
||||
isDownload bool,
|
||||
) (string, error) {
|
||||
return "", errors.New("暂未实现")
|
||||
file, ok := ctx.Value(fsctx.FileModelCtx).(model.File)
|
||||
if !ok {
|
||||
return "", errors.New("无法获取文件记录上下文")
|
||||
}
|
||||
|
||||
serverURL, err := url.Parse(handler.Policy.Server)
|
||||
if err != nil {
|
||||
return "", errors.New("无法解析远程服务端地址")
|
||||
}
|
||||
|
||||
var (
|
||||
expires int64
|
||||
signedURI *url.URL
|
||||
controller = "/api/v3/slave/download"
|
||||
)
|
||||
if !isDownload {
|
||||
controller = "/api/v3/slave/source"
|
||||
}
|
||||
if ttl > 0 {
|
||||
expires = time.Now().Unix() + ttl
|
||||
}
|
||||
|
||||
// 签名下载地址
|
||||
sourcePath := base64.RawURLEncoding.EncodeToString([]byte(file.SourceName))
|
||||
authInstance := auth.HMACAuth{SecretKey: []byte(handler.Policy.SecretKey)}
|
||||
signedURI, err = auth.SignURI(
|
||||
authInstance,
|
||||
fmt.Sprintf("%s/%s", controller, sourcePath),
|
||||
expires,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return "", serializer.NewError(serializer.CodeEncryptError, "无法对URL进行签名", err)
|
||||
}
|
||||
|
||||
finalURL := serverURL.ResolveReference(signedURI).String()
|
||||
return finalURL, nil
|
||||
|
||||
}
|
||||
|
||||
// Token 获取上传策略和认证Token
|
||||
// TODO 测试
|
||||
func (handler Handler) Token(ctx context.Context, TTL int64, key string) (serializer.UploadCredential, error) {
|
||||
// 生成回调地址
|
||||
siteURL := model.GetSiteURL()
|
||||
|
|
|
@ -7,11 +7,11 @@ import "io"
|
|||
// 有些直接写文件数据到浏览器
|
||||
type ContentResponse struct {
|
||||
Redirect bool
|
||||
Content io.ReadSeeker
|
||||
Content RSCloser
|
||||
URL string
|
||||
}
|
||||
|
||||
// 存储策略适配器返回的文件流,有些策略需要带有Closer
|
||||
// RSCloser 存储策略适配器返回的文件流,有些策略需要带有Closer
|
||||
type RSCloser interface {
|
||||
io.ReadSeeker
|
||||
io.Closer
|
||||
|
|
|
@ -151,7 +151,9 @@ func Preview(c *gin.Context) {
|
|||
// 是否需要重定向
|
||||
if res.Code == -301 {
|
||||
c.Redirect(301, res.Data.(string))
|
||||
return
|
||||
}
|
||||
// 是否有错误发生
|
||||
if res.Code != 0 {
|
||||
c.JSON(200, res)
|
||||
}
|
||||
|
|
|
@ -196,14 +196,24 @@ func (service *SingleFileService) PreviewContent(ctx context.Context, c *gin.Con
|
|||
}
|
||||
defer fs.Recycle()
|
||||
|
||||
// 获取文件流
|
||||
rs, err := fs.GetDownloadContent(ctx, service.Path)
|
||||
// 获取文件预览响应
|
||||
resp, err := fs.Preview(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)
|
||||
// 重定向到文件源
|
||||
if resp.Redirect {
|
||||
return serializer.Response{
|
||||
Code: -301,
|
||||
Data: resp.URL,
|
||||
}
|
||||
}
|
||||
|
||||
// 直接返回文件内容
|
||||
defer resp.Content.Close()
|
||||
|
||||
http.ServeContent(c.Writer, c.Request, fs.FileTarget[0].Name, fs.FileTarget[0].UpdatedAt, resp.Content)
|
||||
|
||||
return serializer.Response{
|
||||
Code: 0,
|
||||
|
|
Loading…
Add table
Reference in a new issue