Skip to content

Commit

Permalink
feature(#59): Added TempFileStream
Browse files Browse the repository at this point in the history
  • Loading branch information
jhartmann123 authored and davidroth committed Sep 5, 2024
1 parent d32f8f2 commit a23d8e4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
14 changes: 9 additions & 5 deletions docs/Common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
- [Common](#common)
- [PropertyUtil](#propertyutil)
- [PathUtil](#pathutil)
- [Transactions](#transactions)

This project contains general, framework independent utilities and abstractions.

Expand Down Expand Up @@ -33,10 +32,15 @@ Utility for paths. Does not try to replicate `System.IO.Path`, but adds some hel

Currently this only has methods for removing invalid chars from paths and filenames, as `Path.GetInvalidFileNameChars()` returns different values based on the OS, while `PathUtil` uses a fixed set. This may be for example required when generating a filename for a download.

## Transactions
## TempFileStream

If you need to use trasactions, use the transaction scope handler to run your code in one. You normally don't need to care about transactions, as the MediatR-Pipeline and Hangfire-Jobs have handlers for that (`TransactionalDecorator`, `TransactionalJobHandler`). See the [Hangfire](../Hangfire/README.md) or [MediatR](#mediatr) docs for details.
This creates a temporary file that gets automatically deleted when it is closed. Example:

```cs
Container.RegisterSingleton<ITransactionScopeHandler, TransactionScopeHandler>();
```
await using (var fs = new TempFileStream())
{
// Write content to file
// Upload file
}
// File was deleted at this point.
```
15 changes: 15 additions & 0 deletions src/Common/src/IO/TempFileStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Fusonic GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

namespace Fusonic.Extensions.Common.IO;

/// <summary>
/// A file stream which points to a temporary file. The file automatically gets deleted when the stream is closed.
/// </summary>
public class TempFileStream(int bufferSize = 4096) : FileStream(
Path.GetTempFileName(),
FileMode.Create,
FileAccess.ReadWrite,
FileShare.Read,
bufferSize,
FileOptions.DeleteOnClose);
47 changes: 47 additions & 0 deletions src/Common/test/IO/TempFileStreamTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Fusonic GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

using System.Text;
using FluentAssertions;
using Fusonic.Extensions.Common.IO;
using Xunit;

namespace Fusonic.Extensions.Common.Tests.IO;

public class TempFileStreamTests
{
[Fact]
public async Task TempFileStream_CreatesFile()
{
await using var stream = new TempFileStream();
File.Exists(stream.Name).Should().BeTrue();
}

[Fact]
public async Task TempFileStream_CanWriteToFile()
{
await using var stream = new TempFileStream();
await stream.WriteAsync("Test"u8.ToArray());
await stream.FlushAsync();

stream.Seek(0, SeekOrigin.Begin);

var buffer = new byte[4];
_ = stream.Read(buffer);

Encoding.UTF8.GetString(buffer).Should().Be("Test");
}

[Fact]
public async Task TempFileStream_DeletesFileOnDispose()
{
string path;
await using (var stream = new TempFileStream())
{
path = stream.Name;
File.Exists(path).Should().BeTrue();
}

File.Exists(path).Should().BeFalse();
}
}

0 comments on commit a23d8e4

Please sign in to comment.