|
|
@ -2,12 +2,16 @@ package explorer
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/HFO4/cloudreve/pkg/cache"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
|
|
|
|
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// FileDownloadService 文件下载服务,path为文件完整路径
|
|
|
|
// FileDownloadService 文件下载服务,path为文件完整路径
|
|
|
@ -20,6 +24,51 @@ type FileAnonymousGetService struct {
|
|
|
|
Name string `uri:"name" binding:"required"`
|
|
|
|
Name string `uri:"name" binding:"required"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ArchiveDownloadService struct {
|
|
|
|
|
|
|
|
ID string `uri:"id" binding:"required"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Download 下載已打包的多文件
|
|
|
|
|
|
|
|
func (service *ArchiveDownloadService) Download(ctx context.Context, c *gin.Context) serializer.Response {
|
|
|
|
|
|
|
|
// 创建文件系统
|
|
|
|
|
|
|
|
fs, err := filesystem.NewFileSystemFromContext(c)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 查找打包的临时文件
|
|
|
|
|
|
|
|
zipPath, exist := cache.Get("archive_" + service.ID)
|
|
|
|
|
|
|
|
if !exist {
|
|
|
|
|
|
|
|
return serializer.Err(404, "归档文件不存在", nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取文件流
|
|
|
|
|
|
|
|
rs, err := fs.GetPhysicalFileContent(ctx, zipPath.(string))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.Header("Content-Type", "application/zip")
|
|
|
|
|
|
|
|
http.ServeContent(c.Writer, c.Request, "archive.zip", time.Now(), rs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否需要关闭文件
|
|
|
|
|
|
|
|
if fc, ok := rs.(io.Closer); ok {
|
|
|
|
|
|
|
|
err = fc.Close()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 清理资源,删除临时文件
|
|
|
|
|
|
|
|
_ = cache.Deletes([]string{service.ID}, "archive_")
|
|
|
|
|
|
|
|
err = os.Remove(zipPath.(string))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
util.Log().Warning("无法删除临时文件 %s :%s", zipPath, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return serializer.Response{
|
|
|
|
|
|
|
|
Code: 0,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Download 签名的匿名文件下载
|
|
|
|
// Download 签名的匿名文件下载
|
|
|
|
func (service *FileAnonymousGetService) Download(ctx context.Context, c *gin.Context) serializer.Response {
|
|
|
|
func (service *FileAnonymousGetService) Download(ctx context.Context, c *gin.Context) serializer.Response {
|
|
|
|
fs, err := filesystem.NewAnonymousFileSystem()
|
|
|
|
fs, err := filesystem.NewAnonymousFileSystem()
|
|
|
|