From 39dc55bac314a50613c49deffd389e997764d54b Mon Sep 17 00:00:00 2001 From: shawnnqin Date: Mon, 29 Nov 2021 21:31:36 +0800 Subject: [PATCH] bug fix --- .../COSXML/Network/ResponseBody.cs | 13 ++++- QCloudCSharpSDK/COSXMLTests/ObjectTest.cs | 50 +++++++++++++------ 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/QCloudCSharpSDK/COSXML/Network/ResponseBody.cs b/QCloudCSharpSDK/COSXML/Network/ResponseBody.cs index b3b3281..4f5bf9e 100644 --- a/QCloudCSharpSDK/COSXML/Network/ResponseBody.cs +++ b/QCloudCSharpSDK/COSXML/Network/ResponseBody.cs @@ -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 @@ -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); } diff --git a/QCloudCSharpSDK/COSXMLTests/ObjectTest.cs b/QCloudCSharpSDK/COSXMLTests/ObjectTest.cs index d3f7876..bf668d3 100644 --- a/QCloudCSharpSDK/COSXMLTests/ObjectTest.cs +++ b/QCloudCSharpSDK/COSXMLTests/ObjectTest.cs @@ -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()]