FoundatioFx / Foundatio

Pluggable foundation blocks for building distributed apps.
Apache License 2.0
1.96k stars 243 forks source link

Writing InMemory stream results in NotSupportedException #296

Closed garcipat closed 8 months ago

garcipat commented 10 months ago

I discovered that if you use the new GetStream with StreamMode.Write on the InMemoryStorage, it cannot write since the MemoryStream is initialized with the byte[] and not expandable.

this means for Read and write, the memorystream has to be initialized differently here: https://github.com/FoundatioFx/Foundatio/blob/0cfb6f089db6eddd90cf7240f25b276d6f65139f/src/Foundatio/Storage/InMemoryFileStorage.cs#L57C20-L57C20

I will see how to handle the writing in this case since it has to persiste the bytes into the dictionary.

Edit: it also seems that in Write mode, you cannot create a new file in general.

garcipat commented 10 months ago

Here a unit test to cover that:

[Fact]
public async Task GetFileStream_ShouldBeAbleToWrite()
{
    await _uut.SaveFileAsync("stored.txt", "ABC");

    using var stream = await _uut.GetFileStreamAsync("stored.txt", StreamMode.Write);
    var newContent = "ABCABC";

    var bytes = Encoding.UTF8.GetBytes(newContent);
    stream!.Write(bytes, 0, bytes.Length);
    var content = await _uut.GetFileContentsAsync("stored.txt");
    content.Should().Be(newContent);
}
ejsmith commented 10 months ago

@garcipat thanks for reporting this. Any chance you can take a stab at a PR? I've been meaning to spend time on the getstream support as it's a newly added feature, but haven't had time yet.

garcipat commented 10 months ago

I intended to make a PR myself. Tried to solve it in our project, but have to think a bit more. The thing is that depending on how you Create the memory stream you can extend it or not. Does Not work with zip files either then as with azure sadly. You can expect a proposal soon.

garcipat commented 9 months ago

Sorry I just can't find time currently for this :( I know it makes a difference on how an inmemory stream is created. if you create it with a size you cannot change its size anymore. but if you create it without, you can expand it (see in the official documentation)

niemyjski commented 8 months ago

This has been fixed in the 10.7 release :)