Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnnqin committed Nov 29, 2021
1 parent 795deda commit 39dc55b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
13 changes: 11 additions & 2 deletions QCloudCSharpSDK/COSXML/Network/ResponseBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void HandleResponseBody(Stream inputStream)
{
if (File.Exists(filePath) && new FileInfo(filePath).Length > fileOffset)
{
// 写脏文件了,直接Truncate掉
// 写脏文件了,直接 Truncate
fileStream = new FileStream(filePath, FileMode.Truncate, FileAccess.Write);
}
else
Expand Down Expand Up @@ -190,7 +190,16 @@ public void StartHandleResponseBody(Stream inputStream, EndResponseBody endRespo

if (isDownload)
{
fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
if (File.Exists(filePath) && new FileInfo(filePath).Length > fileOffset)
{
// 写脏文件了,直接 Truncate
fileStream = new FileStream(filePath, FileMode.Truncate, FileAccess.Write);
}
else
{
// 正常文件或者追加写场景,直接写入
fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
}
fileStream.Seek(fileOffset, SeekOrigin.Begin);
responseBodyState.inputStream.BeginRead(responseBodyState.buffer, 0, responseBodyState.buffer.Length, AsyncStreamCallback, responseBodyState);
}
Expand Down
50 changes: 36 additions & 14 deletions QCloudCSharpSDK/COSXMLTests/ObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,21 +1507,43 @@ public void TestDownloadTaskCancelled()
[Test()]
public void TestDownloadTaskOverwriteSameFile()
{
try {
GetObjectRequest request = new GetObjectRequest(bucket, multiKey, localDir, localFileName);

COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(request);
var asyncTask = transferManager.DownloadAsync(downloadTask);
asyncTask.Wait();

//GetObjectResult result = cosXml.GetObject(request);
//Assert.AreEqual(200, result.httpCode);
long bigLength = new FileInfo(localFileName).Length;
// 再下载一个小文件
// 存在一致性问题,把小文件重新上传一下
PutObject();
request = new GetObjectRequest(bucket, commonKey, localDir, localFileName);

downloadTask = new COSXMLDownloadTask(request);
asyncTask = transferManager.DownloadAsync(downloadTask);
asyncTask.Wait();

//result = cosXml.GetObject(request);
//Assert.AreEqual(200, result.httpCode);
// 检查文件长度,是否覆盖写
long smallLength = new FileInfo(localFileName).Length;

Assert.True(smallLength < bigLength);
}
catch (COSXML.CosException.CosClientException clientEx)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
Assert.Fail();
}
catch (COSXML.CosException.CosServerException serverEx)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
Assert.Fail();
}
// 先下载一个大文件
GetObjectRequest request = new GetObjectRequest(bucket, multiKey, localDir, localFileName);
COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(request);
var asyncTask = transferManager.DownloadAsync(downloadTask);
asyncTask.Wait();
long bigLength = new FileInfo(localFileName).Length;
// 再下载一个小文件
request = new GetObjectRequest(bucket, commonKey, localDir, localFileName);
downloadTask = new COSXMLDownloadTask(request);
asyncTask = transferManager.DownloadAsync(downloadTask);
asyncTask.Wait();

// 检查文件长度,是否覆盖写
long smallLength = new FileInfo(localFileName).Length;
Assert.True(smallLength < bigLength);
}

[Test()]
Expand Down

0 comments on commit 39dc55b

Please sign in to comment.