Skip to content

Commit e9ff9ef

Browse files
committed
Fix #98
1 parent edd429b commit e9ff9ef

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

Storages/ManagedCode.Storage.FileSystem/FileSystemStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected override async Task<Result<BlobMetadata>> UploadInternalAsync(Stream s
9292

9393
var filePath = GetPathFromOptions(options);
9494

95-
using (var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
95+
using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
9696
{
9797
stream.Seek(0, SeekOrigin.Begin);
9898
await stream.CopyToAsync(fs, 81920, cancellationToken);
@@ -222,4 +222,4 @@ private void EnsureDirectoryExist(string directory)
222222
if (!Directory.Exists(path))
223223
Directory.CreateDirectory(path);
224224
}
225-
}
225+
}

Tests/ManagedCode.Storage.Tests/Storages/FileSystem/FileSystemUploadTests.cs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using FluentAssertions;
16
using ManagedCode.Storage.Tests.Common;
27
using Microsoft.Extensions.DependencyInjection;
8+
using Xunit;
39

410
namespace ManagedCode.Storage.Tests.Storages.FileSystem;
511

@@ -14,4 +20,66 @@ protected override ServiceProvider ConfigureServices()
1420
{
1521
return FileSystemConfigurator.ConfigureServices("managed-code-blob");
1622
}
17-
}
23+
24+
[Fact]
25+
public async Task UploadAsync_AsStream_CorrectlyOverwritesFiles()
26+
{
27+
// Arrange
28+
29+
var uploadStream1 = new MemoryStream(90*1024);
30+
var buffer = new byte[90 * 1024];
31+
var random = new Random();
32+
random.NextBytes(buffer);
33+
uploadStream1.Write(buffer, 0, buffer.Length);
34+
35+
var uploadStream2 = new MemoryStream(512);
36+
var zeroByteBuffer = new byte[512];
37+
uploadStream2.Write(zeroByteBuffer);
38+
var filenameToUse = "UploadAsync_AsStream_CorrectlyOverwritesFiles.bin";
39+
40+
var temporaryDirectory = Path.GetTempPath();
41+
42+
// Act
43+
var firstResult = await Storage.UploadAsync(uploadStream1, options =>
44+
{
45+
options.FileName = filenameToUse;
46+
options.Directory = temporaryDirectory;
47+
});
48+
49+
firstResult.IsSuccess.Should().BeTrue();
50+
51+
// let's download it
52+
var downloadedResult = await Storage.DownloadAsync(options =>
53+
{
54+
options.FileName = filenameToUse;
55+
options.Directory = temporaryDirectory;
56+
});
57+
downloadedResult.IsSuccess.Should().BeTrue();
58+
// size
59+
downloadedResult.Value!.FileInfo.Length.Should().Be(90*1024);
60+
61+
62+
var secondResult = await Storage.UploadAsync(uploadStream2, options =>
63+
{
64+
options.FileName = filenameToUse;
65+
options.Directory = temporaryDirectory;
66+
});
67+
68+
secondResult.IsSuccess.Should().BeTrue();
69+
70+
// let's download it
71+
downloadedResult = await Storage.DownloadAsync(options =>
72+
{
73+
options.FileName = filenameToUse;
74+
options.Directory = temporaryDirectory;
75+
});
76+
downloadedResult.IsSuccess.Should().BeTrue();
77+
// size
78+
downloadedResult.Value!.FileInfo.Length.Should().Be(512);
79+
80+
// content
81+
using var ms = new MemoryStream();
82+
await downloadedResult.Value!.FileStream.CopyToAsync(ms);
83+
ms.ToArray().Should().BeEquivalentTo(zeroByteBuffer);
84+
}
85+
}

0 commit comments

Comments
 (0)