Skip to content

Commit e6dc738

Browse files
authored
Merge pull request #10 from feO2x/readme-updates
Readme updates
2 parents 0da9e08 + e6d7021 commit e6dc738

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ obj/
66
*.suo
77
*.user
88
Light.TemporaryStreams.snk
9+
.DS_Store

readme.md

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@
66

77
## Overview 🔍
88

9-
Light.TemporaryStreams is a lightweight .NET library that helps you convert non-seekable streams into seekable temporary
10-
streams. A temporary stream is either backed by a memory stream (for input smaller than 80 KB) or a file stream to a
11-
temporary file. This is particularly useful for backend services that receive streams from HTTP requests (e.g.,
12-
`application/octet-stream`, custom-parsed `multipart/form-data`) or download files from storage systems for further
13-
processing.
9+
Light.TemporaryStreams is a lightweight .NET library that helps you convert non-seekable streams to seekable temporary streams. A temporary stream is either backed by a memory stream (for input smaller than 80 KB) or a file stream. This is particularly useful for backend services that receive streams from HTTP requests or download files from storage systems for further processing.
1410

1511
## Key Features ✨
1612

1713
- 🚀 Easy conversion of non-seekable streams to seekable temporary streams
1814
- 💾 Automatic management of temporary files (creation and deletion)
19-
- 🔄 Smart switching between memory-based and file-based streams based on size (similar behavior to ASP.NET Core's
20-
`IFormFile`)
15+
- 🔄 Smart switching between memory-based and file-based streams depending on size (similar behavior to ASP.NET Core's `IFormFile`)
2116
- 🧩 Plugin system for extending functionality (e.g., calculating hashes during stream copying)
2217
- 🔌 Integration with Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.Logging
2318

@@ -62,29 +57,29 @@ public class SomeService
6257

6358
public async Task ProcessStreamAsync(Stream nonSeekableStream, CancellationToken cancellationToken = default)
6459
{
65-
// A temporary stream is either backed by a memory stream or a file stream
66-
// and thus seekable.
60+
// A temporary stream is either backed by a memory stream or a file stream and thus seekable.
6761
await using TemporaryStream temporaryStream =
6862
await _temporaryStreamService.CopyToTemporaryStreamAsync(nonSeekableStream, cancellationToken);
6963

7064
// Do something here with the temporary stream (analysis, processing, etc.).
71-
// For example, your code base has a PdfProcessor that requires a seekable stream.
65+
// For example, your code base might have a PdfProcessor that requires a seekable stream.
7266
using (var pdf = new PdfProcessor(temporaryStream, leaveOpen: true))
7367
{
74-
var emptyOrIrrelevantPages = pdf.DetermineEmptyOrIrrelevantPages();
68+
var emptyOrIrrelevantPages = await pdf.DetermineEmptyOrIrrelevantPagesAsync(cancellationToken);
7569
pdf.RemovePages(emptyOrIrrelevantPages);
7670
}
7771

7872
// Once you are done with processing, you can easily reset the stream to Position 0.
7973
// You can also use resilience patterns here and always reset the stream
8074
// for each upload attempt.
8175
temporaryStream.ResetStreamPosition();
82-
await _s3UploadClient.UploadAsync(temporaryStream);
76+
await _s3UploadClient.UploadAsync(temporaryStream, cancellationToken);
8377

84-
// When the temporary stream is disposed, it will automatically delete the
78+
// When the temporary stream is disposed of (because of the await using at
79+
// the beginning of the method), it will automatically delete the
8580
// underlying file if necessary. No need to worry about manual cleanup.
86-
// This is also great when a temporary stream is returned in an
87-
// MVC Controller action or in Minimal API endpoint.
81+
// This also works when a temporary stream is returned in an
82+
// MVC Controller action or in a Minimal API endpoint.
8883
}
8984
}
9085
```
@@ -156,7 +151,7 @@ byte[] md5HashArray = hashingPlugin.GetHashArray(nameof(MD5));
156151

157152
### Hexadecimal Hashes via CopyToHashCalculator
158153

159-
The `HashAlgorithm` instances passed to the `HashingPlugin` constructor in the previous example are actually converted into instances of `CopyToHashCalculator` via an implicit conversion operator. You can instantiate this class yourself to have more control over the conversion method that converts a hash byte array into a string as well as the name used to identify the hash calculator.
154+
The `HashAlgorithm` instances passed to the `HashingPlugin` constructor in the previous example are actually converted to instances of `CopyToHashCalculator` via an implicit conversion operator. You can instantiate this class yourself to have more control over the conversion method that converts a hash byte array into a string as well as the name used to identify the hash calculator.
160155

161156
```csharp
162157
var sha1Calculator = new CopyToHashCalculator(SHA1.Create(), HashConversionMethod.UpperHexadecimal, "SHA1");
@@ -172,9 +167,10 @@ byte[] md5HashArray = hashingPlugin.GetHashArray(nameof(MD5));
172167

173168
## When To Use Light.TemporaryStreams 🤔
174169

175-
- Your service implements endpoints that receives `application/octet-stream` that you need to process further.
176-
- Your service implements endpoints that receives `multipart/form-data` and you cannot use `IFormFile`, for example because the request has both JSON and binary data. See [this blog post by Andrew Lock](https://andrewlock.net/reading-json-and-binary-data-from-multipart-form-data-sections-in-aspnetcore/) for an example.
177-
- Your service downloads files from storage systems like S3 and processes them further.
170+
- Your service implements endpoints that receive `application/octet-stream` requests and you need to process the incoming stream in a seekable way.
171+
- Your service implements endpoints that receive `multipart/form-data` requests and you cannot use `IFormFile`, for example because the request has both JSON and binary data. See [this blog post by Andrew Lock](https://andrewlock.net/reading-json-and-binary-data-from-multipart-form-data-sections-in-aspnetcore/) for an example.
172+
- Your service downloads files from storage systems like Amazon S3 or Azure Storage Accounts and processes them further.
173+
- Your endpoint wants to return a stream to the caller and the file should be gone after the request finishes.
178174

179175
## Light.TemporaryStreams.Core vs. Light.TemporaryStreams 🧰
180176

@@ -198,11 +194,11 @@ This package builds on Core and adds integration with:
198194

199195
Use Light.TemporaryStreams.Core if you're working in a non-DI environment or have your own DI container.
200196
Use Light.TemporaryStreams if you're working in an ASP.NET Core application or any other application supporting
201-
Microsoft.Extensions.DependencyInjection.
197+
Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.Logging.
202198

203199
## Contributing 🤝
204200

205-
Contributions are welcome! First, create an issue to discuss your idea. After that, you can submit pull requests.
201+
Contributions are welcome! First, create an issue to discuss your idea. After that, you can submit a pull request.
206202

207203
## License 📜
208204

0 commit comments

Comments
 (0)