Feat: download temporary archive file

This commit is contained in:
HFO4
2019-12-13 15:10:44 +08:00
parent afc0b647ca
commit 02c93be3bc
6 changed files with 103 additions and 15 deletions

View File

@@ -26,10 +26,17 @@ func (r lrs) Read(p []byte) (int, error) {
}
// withSpeedLimit 给原有的ReadSeeker加上限速
func withSpeedLimit(rs io.ReadSeeker, speed int) io.ReadSeeker {
bucket := ratelimit.NewBucketWithRate(float64(speed), int64(speed))
lrs := lrs{rs, ratelimit.Reader(rs, bucket)}
return lrs
func (fs *FileSystem) withSpeedLimit(rs io.ReadSeeker) io.ReadSeeker {
// 如果用户组有速度限制就返回限制流速的ReaderSeeker
if fs.User.Group.SpeedLimit != 0 {
speed := fs.User.Group.SpeedLimit
bucket := ratelimit.NewBucketWithRate(float64(speed), int64(speed))
lrs := lrs{rs, ratelimit.Reader(rs, bucket)}
return lrs
}
// 否则返回原始流
return rs
}
// AddFile 新增文件记录
@@ -54,6 +61,21 @@ func (fs *FileSystem) AddFile(ctx context.Context, parent *model.Folder) (*model
return &newFile, nil
}
// GetPhysicalFileContent 根据文件物理路径获取文件流
func (fs *FileSystem) GetPhysicalFileContent(ctx context.Context, path string) (io.ReadSeeker, error) {
// 重设上传策略
fs.Policy = &model.Policy{Type: "local"}
_ = fs.dispatchHandler()
// 获取文件流
rs, err := fs.Handler.Get(ctx, path)
if err != nil {
return nil, err
}
return fs.withSpeedLimit(rs), nil
}
// GetDownloadContent 获取用于下载的文件流
func (fs *FileSystem) GetDownloadContent(ctx context.Context, path string) (io.ReadSeeker, error) {
// 获取原始文件流
@@ -62,12 +84,8 @@ func (fs *FileSystem) GetDownloadContent(ctx context.Context, path string) (io.R
return nil, err
}
// 如果用户组有速度限制就返回限制流速的ReaderSeeker
if fs.User.Group.SpeedLimit != 0 {
return withSpeedLimit(rs, fs.User.Group.SpeedLimit), nil
}
// 否则返回原始流
return rs, nil
// 返回限速处理后的文件流
return fs.withSpeedLimit(rs), nil
}