|
|
@ -2,8 +2,10 @@ package filesystem
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"github.com/HFO4/cloudreve/models"
|
|
|
|
"github.com/HFO4/cloudreve/models"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -64,6 +66,10 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
上传处理相关
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// Upload 上传文件
|
|
|
|
// Upload 上传文件
|
|
|
|
func (fs *FileSystem) Upload(ctx context.Context, file FileData) (err error) {
|
|
|
|
func (fs *FileSystem) Upload(ctx context.Context, file FileData) (err error) {
|
|
|
|
// 上传前的钩子
|
|
|
|
// 上传前的钩子
|
|
|
@ -75,6 +81,9 @@ func (fs *FileSystem) Upload(ctx context.Context, file FileData) (err error) {
|
|
|
|
// 生成文件名和路径
|
|
|
|
// 生成文件名和路径
|
|
|
|
savePath := fs.GenerateSavePath(file)
|
|
|
|
savePath := fs.GenerateSavePath(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 处理客户端未完成上传时,关闭连接
|
|
|
|
|
|
|
|
go fs.CancelUpload(ctx, savePath, file)
|
|
|
|
|
|
|
|
|
|
|
|
// 保存文件
|
|
|
|
// 保存文件
|
|
|
|
err = fs.Handler.Put(ctx, file, savePath)
|
|
|
|
err = fs.Handler.Put(ctx, file, savePath)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -91,3 +100,16 @@ func (fs *FileSystem) GenerateSavePath(file FileData) string {
|
|
|
|
fs.User.Policy.GenerateFileName(fs.User.Model.ID, file.GetFileName()),
|
|
|
|
fs.User.Policy.GenerateFileName(fs.User.Model.ID, file.GetFileName()),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CancelUpload 监测客户端取消上传
|
|
|
|
|
|
|
|
func (fs *FileSystem) CancelUpload(ctx context.Context, path string, file FileData) {
|
|
|
|
|
|
|
|
ginCtx := ctx.Value("ginCtx").(*gin.Context)
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
|
|
// 客户端正常关闭,不执行操作
|
|
|
|
|
|
|
|
case <-ginCtx.Request.Context().Done():
|
|
|
|
|
|
|
|
// 客户端取消了上传,删除保存的文件
|
|
|
|
|
|
|
|
fmt.Println("取消上传")
|
|
|
|
|
|
|
|
// 归还空间
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|