Feat: batch download in streamming paradism

Fix: add cache-controler header in API call responses
This commit is contained in:
HFO4
2022-04-13 17:53:46 +08:00
parent 32a655f84e
commit febbd0c5a0
11 changed files with 98 additions and 97 deletions

View File

@@ -12,7 +12,6 @@ import (
"path"
"strconv"
"strings"
"time"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
@@ -42,6 +41,11 @@ type DownloadService struct {
ID string `uri:"id" binding:"required"`
}
// ArchiveService 文件流式打包下載服务
type ArchiveService struct {
ID string `uri:"sessionID" binding:"required"`
}
// New 创建新文件
func (service *SingleFileService) Create(c *gin.Context) serializer.Response {
// 创建文件系统
@@ -93,8 +97,14 @@ func (service *SlaveListService) List(c *gin.Context) serializer.Response {
return serializer.Response{Data: string(res)}
}
// DownloadArchived 下載已打包的多文件
func (service *DownloadService) DownloadArchived(ctx context.Context, c *gin.Context) serializer.Response {
// DownloadArchived 通过预签名 URL 打包下载
func (service *ArchiveService) DownloadArchived(ctx context.Context, c *gin.Context) serializer.Response {
userRaw, exist := cache.Get("archive_user_" + service.ID)
if !exist {
return serializer.Err(404, "归档会话不存在", nil)
}
user := userRaw.(model.User)
// 创建文件系统
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
@@ -103,31 +113,28 @@ func (service *DownloadService) DownloadArchived(ctx context.Context, c *gin.Con
defer fs.Recycle()
// 查找打包的临时文件
zipPath, exist := cache.Get("archive_" + service.ID)
archiveSession, exist := cache.Get("archive_" + service.ID)
if !exist {
return serializer.Err(404, "归档文件不存在", nil)
return serializer.Err(404, "归档会话不存在", nil)
}
// 获取文件流
rs, err := fs.GetPhysicalFileContent(ctx, zipPath.(string))
defer rs.Close()
if err != nil {
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
}
if fs.User.Group.OptionsSerialized.OneTimeDownload {
// 清理资源,删除临时文件
_ = cache.Deletes([]string{service.ID}, "archive_")
}
// 清理打包会话
_ = cache.Deletes([]string{service.ID, "user_" + service.ID}, "archive_")
// 开始打包
c.Header("Content-Disposition", "attachment;")
c.Header("Content-Type", "application/zip")
http.ServeContent(c.Writer, c.Request, "", time.Now(), rs)
itemService := archiveSession.(ItemIDService)
items := itemService.Raw()
ctx = context.WithValue(ctx, fsctx.GinCtx, c)
err = fs.Compress(ctx, c.Writer, items.Dirs, items.Items, true)
if err != nil {
return serializer.Err(serializer.CodeNotSet, "无法创建压缩文件", err)
}
return serializer.Response{
Code: 0,
}
}
// Download 签名的匿名文件下载
@@ -261,7 +268,7 @@ func (service *FileIDService) CreateDownloadSession(ctx context.Context, c *gin.
// Download 通过签名URL的文件下载无需登录
func (service *DownloadService) Download(ctx context.Context, c *gin.Context) serializer.Response {
// 创建文件系统
fs, err := filesystem.NewFileSystemFromContext(c)
fs, err := filesystem.NewFileSystem(&user)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
}

View File

@@ -2,9 +2,9 @@ package explorer
import (
"context"
"encoding/gob"
"fmt"
"math"
"net/url"
"path"
"strings"
"time"
@@ -13,7 +13,6 @@ import (
"github.com/cloudreve/Cloudreve/v3/pkg/auth"
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
"github.com/cloudreve/Cloudreve/v3/pkg/hashid"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/pkg/task"
@@ -67,6 +66,10 @@ type ItemPropertyService struct {
IsFolder bool `form:"is_folder"`
}
func init() {
gob.Register(ItemIDService{})
}
// Raw 批量解码HashID获取原始ID
func (service *ItemIDService) Raw() *ItemService {
if service.Source != nil {
@@ -232,37 +235,20 @@ func (service *ItemIDService) Archive(ctx context.Context, c *gin.Context) seria
return serializer.Err(serializer.CodeGroupNotAllowed, "当前用户组无法进行此操作", nil)
}
// 开始压缩
ctx = context.WithValue(ctx, fsctx.GinCtx, c)
items := service.Raw()
zipFile, err := fs.Compress(ctx, items.Dirs, items.Items, true)
if err != nil {
return serializer.Err(serializer.CodeNotSet, "无法创建压缩文件", err)
}
// 生成一次性压缩文件下载地址
siteURL, err := url.Parse(model.GetSettingByName("siteURL"))
if err != nil {
return serializer.Err(serializer.CodeNotSet, "无法解析站点URL", err)
}
zipID := util.RandStringRunes(16)
// 创建打包下载会话
ttl := model.GetIntSetting("archive_timeout", 30)
signedURI, err := auth.SignURI(
downloadSessionID := util.RandStringRunes(16)
cache.Set("archive_"+downloadSessionID, *service, ttl)
cache.Set("archive_user_"+downloadSessionID, *fs.User, ttl)
signURL, err := auth.SignURI(
auth.General,
fmt.Sprintf("/api/v3/file/archive/%s/archive.zip", zipID),
time.Now().Unix()+int64(ttl),
fmt.Sprintf("/api/v3/file/archive/%s/archive.zip", downloadSessionID),
int64(ttl),
)
finalURL := siteURL.ResolveReference(signedURI).String()
// 将压缩文件记录存入缓存
err = cache.Set("archive_"+zipID, zipFile, ttl)
if err != nil {
return serializer.Err(serializer.CodeIOFailed, "无法写入缓存", err)
}
return serializer.Response{
Code: 0,
Data: finalURL,
Data: signURL.String(),
}
}