2019-11-26 19:51:54 +08:00
|
|
|
|
package explorer
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2019-12-13 15:10:44 +08:00
|
|
|
|
"github.com/HFO4/cloudreve/pkg/cache"
|
2019-11-26 19:51:54 +08:00
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem"
|
2019-12-10 17:10:34 +08:00
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
2019-11-26 19:51:54 +08:00
|
|
|
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
2019-12-13 15:10:44 +08:00
|
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
2019-11-26 19:51:54 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2019-12-11 18:52:41 +08:00
|
|
|
|
"io"
|
2019-11-27 12:49:31 +08:00
|
|
|
|
"net/http"
|
2019-12-13 15:10:44 +08:00
|
|
|
|
"os"
|
|
|
|
|
"time"
|
2019-11-26 19:51:54 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// FileDownloadService 文件下载服务,path为文件完整路径
|
|
|
|
|
type FileDownloadService struct {
|
2019-11-29 15:24:23 +08:00
|
|
|
|
Path string `uri:"path" binding:"required,min=1,max=65535"`
|
2019-11-26 19:51:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 20:17:21 +08:00
|
|
|
|
type FileAnonymousGetService struct {
|
|
|
|
|
ID uint `uri:"id" binding:"required,min=1"`
|
|
|
|
|
Name string `uri:"name" binding:"required"`
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 15:10:44 +08:00
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 20:17:21 +08:00
|
|
|
|
// Download 签名的匿名文件下载
|
|
|
|
|
func (service *FileAnonymousGetService) Download(ctx context.Context, c *gin.Context) serializer.Response {
|
2019-12-10 20:42:40 +08:00
|
|
|
|
fs, err := filesystem.NewAnonymousFileSystem()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serializer.Err(serializer.CodeGroupNotAllowed, err.Error(), err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找文件
|
|
|
|
|
err = fs.SetTargetFileByIDs([]uint{service.ID})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取文件流
|
|
|
|
|
rs, err := fs.GetDownloadContent(ctx, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送文件
|
|
|
|
|
http.ServeContent(c.Writer, c.Request, service.Name, fs.FileTarget[0].UpdatedAt, rs)
|
2019-12-10 20:17:21 +08:00
|
|
|
|
|
2019-12-11 18:52:41 +08:00
|
|
|
|
// 检查是否需要关闭文件
|
|
|
|
|
if fc, ok := rs.(io.Closer); ok {
|
|
|
|
|
defer fc.Close()
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 20:17:21 +08:00
|
|
|
|
return serializer.Response{
|
|
|
|
|
Code: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-26 19:51:54 +08:00
|
|
|
|
// Download 文件下载
|
|
|
|
|
func (service *FileDownloadService) 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)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-26 20:59:57 +08:00
|
|
|
|
// 开始处理下载
|
2019-12-10 17:10:34 +08:00
|
|
|
|
ctx = context.WithValue(ctx, fsctx.GinCtx, c)
|
2019-11-27 13:10:19 +08:00
|
|
|
|
rs, err := fs.GetDownloadContent(ctx, service.Path)
|
2019-11-26 19:51:54 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 12:49:31 +08:00
|
|
|
|
// 设置文件名
|
2019-11-30 15:09:56 +08:00
|
|
|
|
c.Header("Content-Disposition", "attachment; filename=\""+fs.FileTarget[0].Name+"\"")
|
2019-11-27 12:49:31 +08:00
|
|
|
|
// 发送文件
|
2019-12-08 13:03:08 +08:00
|
|
|
|
http.ServeContent(c.Writer, c.Request, "", fs.FileTarget[0].UpdatedAt, rs)
|
2019-11-27 12:49:31 +08:00
|
|
|
|
|
2019-12-11 18:52:41 +08:00
|
|
|
|
// 检查是否需要关闭文件
|
|
|
|
|
if fc, ok := rs.(io.Closer); ok {
|
|
|
|
|
defer fc.Close()
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-26 19:51:54 +08:00
|
|
|
|
return serializer.Response{
|
|
|
|
|
Code: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|