@@ -40,32 +40,38 @@ Then, inject the `ITemporaryStreamService` into any class that needs to convert
4040temporary streams:
4141
4242``` csharp
43- using Light .TemporaryStreams ;
44- using System .IO ;
45- using System .Threading .Tasks ;
46-
4743public 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 {
6062 // A temporary stream is either backed by a memory stream or a file stream and thus seekable.
6163 await using TemporaryStream temporaryStream =
62- await _temporaryStreamService .CopyToTemporaryStreamAsync (nonSeekableStream , cancellationToken );
64+ await _temporaryStreamService .CopyToTemporaryStreamAsync (
65+ nonSeekableStream ,
66+ cancellationToken : cancellationToken
67+ );
6368
6469 // Do something here with the temporary stream (analysis, processing, etc.).
6570 // For example, your code base might have a PdfProcessor that requires a seekable stream.
6671 using (var pdf = new PdfProcessor (temporaryStream , leaveOpen : true ))
6772 {
68- var emptyOrIrrelevantPages = await pdf .DetermineEmptyOrIrrelevantPagesAsync (cancellationToken );
73+ var emptyOrIrrelevantPages =
74+ await pdf .DetermineEmptyOrIrrelevantPagesAsync (cancellationToken );
6975 pdf .RemovePages (emptyOrIrrelevantPages );
7076 }
7177
@@ -136,10 +142,15 @@ by implementing the `ICopyToTemporaryStreamPlugin` interface.
136142
137143``` csharp
138144// 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.
145+ // to the hashing plugin constructor. They will be disposed of when the
146+ // hashingPlugin is disposed of.
140147await using var hashingPlugin = new HashingPlugin ([SHA1 .Create (), MD5 .Create ()]);
141- await using var temporaryStream = await _temporaryStreamService
142- .CopyToTemporaryStreamAsync (stream , [hashingPlugin ], cancellationToken : cancellationToken );
148+ await using var temporaryStream =
149+ await _temporaryStreamService .CopyToTemporaryStreamAsync (
150+ stream ,
151+ [hashingPlugin ],
152+ cancellationToken : cancellationToken
153+ );
143154
144155// After copying is done, you can call GetHash to obtain the hash as a base64 string
145156// or GetHashArray to obtain the hash in its raw byte array form.
@@ -149,20 +160,34 @@ string sha1Base64Hash = hashingPlugin.GetHash(nameof(SHA1));
149160byte [] md5HashArray = hashingPlugin .GetHashArray (nameof (MD5 ));
150161```
151162
152- ### Hexadecimal Hashes via CopyToHashCalculator
163+ ### More Control via CopyToHashCalculator
153164
154165The ` 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.
155166
156167``` csharp
157- var sha1Calculator = new CopyToHashCalculator (SHA1 .Create (), HashConversionMethod .UpperHexadecimal , " SHA1" );
158- var md5Calculator = new CopyToHashCalculator (MD5 .Create (), HashConversionMethod .None , " MD5" );
168+ // You can explicitly create instances of CopyToHashCalculator to have more control over the
169+ // conversion method and the name that identifies the hash calculator within the HashingPlugin.
170+ var sha1Calculator = new CopyToHashCalculator (
171+ SHA1 .Create (),
172+ HashConversionMethod .UpperHexadecimal ,
173+ " SHA1"
174+ );
175+ var md5Calculator = new CopyToHashCalculator (
176+ MD5 .Create (),
177+ HashConversionMethod .None ,
178+ " MD5"
179+ );
159180await using var hashingPlugin = new HashingPlugin ([sha1Calculator , md5Calculator ]);
160181
161- await using var temporaryStream = await _temporaryStreamService
162- .CopyToTemporaryStreamAsync (stream , [hashingPlugin ], cancellationToken : cancellationToken );
182+ await using var temporaryStream =
183+ await _temporaryStreamService .CopyToTemporaryStreamAsync (
184+ stream ,
185+ [hashingPlugin ],
186+ cancellationToken : cancellationToken
187+ );
163188
164- string sha1HexadecimalHash = hashingPlugin .GetHash (nameof ( SHA1 ) );
165- byte [] md5HashArray = hashingPlugin .GetHashArray (nameof ( MD5 ) );
189+ string sha1HexadecimalHash = hashingPlugin .GetHash (" SHA1" );
190+ byte [] md5HashArray = hashingPlugin .GetHashArray (" MD5" );
166191```
167192
168193## When To Use Light.TemporaryStreams 🤔
0 commit comments