Feat: truncate file if uploaded chunk is overlapped

This commit is contained in:
HFO4
2022-02-28 17:47:57 +08:00
parent 521c5c8dc4
commit 285611baf7
8 changed files with 69 additions and 34 deletions

View File

@ -136,8 +136,20 @@ func (handler Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
return err
}
if uint64(stat.Size()) != fileInfo.AppendStart {
if uint64(stat.Size()) < fileInfo.AppendStart {
return errors.New("未上传完成的文件分片与预期大小不一致")
} else if uint64(stat.Size()) > fileInfo.AppendStart {
out.Close()
if err := handler.Truncate(ctx, dst, fileInfo.AppendStart); err != nil {
return fmt.Errorf("覆盖分片时发生错误: %w", err)
}
out, err = os.OpenFile(dst, os.O_APPEND|os.O_CREATE|os.O_WRONLY, Perm)
defer out.Close()
if err != nil {
util.Log().Warning("无法打开或创建文件,%s", err)
return err
}
}
}