Skip to content

Commit f07a006

Browse files
authored
Merge pull request #11 from feO2x/further-readme-improvements
docs: improve code formatting and readability in readme examples
2 parents e6dc738 + f310fed commit f07a006

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

readme.md

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,40 @@ Then, inject the `ITemporaryStreamService` into any class that needs to convert
4040
temporary streams:
4141

4242
```csharp
43-
using Light.TemporaryStreams;
44-
using System.IO;
45-
using System.Threading.Tasks;
46-
4743
public class SomeService
4844
{
4945
private readonly ITemporaryStreamService _temporaryStreamService;
5046
private readonly IS3UploadClient _s3UploadClient;
5147

52-
public SomeService(ITemporaryStreamService temporaryStreamService, IS3UploadClient s3UploadClient)
48+
public SomeService(
49+
ITemporaryStreamService temporaryStreamService,
50+
IS3UploadClient s3UploadClient
51+
)
5352
{
5453
_temporaryStreamService = temporaryStreamService;
5554
_s3UploadClient = s3UploadClient;
5655
}
5756

58-
public async Task ProcessStreamAsync(Stream nonSeekableStream, CancellationToken cancellationToken = default)
57+
public async Task ProcessStreamAsync(
58+
Stream nonSeekableStream,
59+
CancellationToken cancellationToken = default
60+
)
5961
{
60-
// A temporary stream is either backed by a memory stream or a file stream and thus seekable.
62+
// A temporary stream is either backed by a memory stream or a file stream and
63+
// thus seekable.
6164
await using TemporaryStream temporaryStream =
62-
await _temporaryStreamService.CopyToTemporaryStreamAsync(nonSeekableStream, cancellationToken);
65+
await _temporaryStreamService.CopyToTemporaryStreamAsync(
66+
nonSeekableStream,
67+
cancellationToken: cancellationToken
68+
);
6369

6470
// Do something here with the temporary stream (analysis, processing, etc.).
65-
// For example, your code base might have a PdfProcessor that requires a seekable stream.
71+
// For example, your code base might have a PdfProcessor that requires
72+
// a seekable stream.
6673
using (var pdf = new PdfProcessor(temporaryStream, leaveOpen: true))
6774
{
68-
var emptyOrIrrelevantPages = await pdf.DetermineEmptyOrIrrelevantPagesAsync(cancellationToken);
75+
var emptyOrIrrelevantPages =
76+
await pdf.DetermineEmptyOrIrrelevantPagesAsync(cancellationToken);
6977
pdf.RemovePages(emptyOrIrrelevantPages);
7078
}
7179

@@ -136,10 +144,15 @@ by implementing the `ICopyToTemporaryStreamPlugin` interface.
136144

137145
```csharp
138146
// You can simply pass any instance of System.Security.Cryptography.HashAlgorithm
139-
// to the hashing plugin constructor. They will be disposed of when the hashingPlugin is disposed of.
147+
// to the hashing plugin constructor. They will be disposed of when the
148+
// hashingPlugin is disposed of.
140149
await using var hashingPlugin = new HashingPlugin([SHA1.Create(), MD5.Create()]);
141-
await using var temporaryStream = await _temporaryStreamService
142-
.CopyToTemporaryStreamAsync(stream, [hashingPlugin], cancellationToken: cancellationToken);
150+
await using var temporaryStream =
151+
await _temporaryStreamService.CopyToTemporaryStreamAsync(
152+
stream,
153+
[hashingPlugin],
154+
cancellationToken: cancellationToken
155+
);
143156

144157
// After copying is done, you can call GetHash to obtain the hash as a base64 string
145158
// or GetHashArray to obtain the hash in its raw byte array form.
@@ -149,20 +162,35 @@ string sha1Base64Hash = hashingPlugin.GetHash(nameof(SHA1));
149162
byte[] md5HashArray = hashingPlugin.GetHashArray(nameof(MD5));
150163
```
151164

152-
### Hexadecimal Hashes via CopyToHashCalculator
165+
### More Control via CopyToHashCalculator
153166

154167
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.
155168

156169
```csharp
157-
var sha1Calculator = new CopyToHashCalculator(SHA1.Create(), HashConversionMethod.UpperHexadecimal, "SHA1");
158-
var md5Calculator = new CopyToHashCalculator(MD5.Create(), HashConversionMethod.None, "MD5");
170+
// You can explicitly create instances of CopyToHashCalculator to have
171+
// more control over the conversion method and the name that identifies
172+
// the hash calculator within the HashingPlugin.
173+
var sha1Calculator = new CopyToHashCalculator(
174+
SHA1.Create(),
175+
HashConversionMethod.UpperHexadecimal,
176+
"SHA1"
177+
);
178+
var md5Calculator = new CopyToHashCalculator(
179+
MD5.Create(),
180+
HashConversionMethod.None,
181+
"MD5"
182+
);
159183
await using var hashingPlugin = new HashingPlugin([sha1Calculator, md5Calculator]);
160184

161-
await using var temporaryStream = await _temporaryStreamService
162-
.CopyToTemporaryStreamAsync(stream, [hashingPlugin], cancellationToken: cancellationToken);
185+
await using var temporaryStream =
186+
await _temporaryStreamService.CopyToTemporaryStreamAsync(
187+
stream,
188+
[hashingPlugin],
189+
cancellationToken: cancellationToken
190+
);
163191

164-
string sha1HexadecimalHash = hashingPlugin.GetHash(nameof(SHA1));
165-
byte[] md5HashArray = hashingPlugin.GetHashArray(nameof(MD5));
192+
string sha1HexadecimalHash = hashingPlugin.GetHash("SHA1");
193+
byte[] md5HashArray = hashingPlugin.GetHashArray("MD5");
166194
```
167195

168196
## When To Use Light.TemporaryStreams 🤔

0 commit comments

Comments
 (0)