put StreamToString to extensions

This commit is contained in:
Alex Bezdieniezhnykh
2025-01-20 10:04:33 +02:00
parent 49de0351c1
commit bfb7b89f32
4 changed files with 18 additions and 12 deletions
@@ -0,0 +1,15 @@
using System.Text;
namespace Azaion.Common.Extensions;
public static class StreamExtensions
{
public static string ConvertToString(this Stream stream)
{
stream.Position = 0;
using var reader = new StreamReader(stream, Encoding.UTF8);
var result = reader.ReadToEnd();
stream.Position = 0;
return result;
}
}
-3
View File
@@ -1,8 +1,5 @@
using Azaion.Common;
using Azaion.Common.Configs;
using Azaion.Common.Database;
using Azaion.Common.Entities;
using LinqToDB;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
+1
View File
@@ -52,5 +52,6 @@ public static class Security
int bytesRead;
while ((bytesRead = await cryptoStream.ReadAsync(buffer, cancellationToken)) > 0)
await toStream.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken);
toStream.Seek(0, SeekOrigin.Begin);
}
}
+2 -9
View File
@@ -1,5 +1,6 @@
using System.Security.Cryptography;
using System.Text;
using Azaion.Common.Extensions;
using Azaion.Services;
using FluentAssertions;
using Newtonsoft.Json;
@@ -27,7 +28,7 @@ public class SecurityTest
await encryptedStream.DecryptTo(decryptedStream, key);
encryptedStream.Close();
var str = StreamToString(decryptedStream);
var str = decryptedStream.ConvertToString();
str.Should().Be(testString);
}
@@ -91,17 +92,9 @@ public class SecurityTest
return stream;
}
private static string StreamToString(Stream stream)
{
stream.Position = 0;
using var reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
private static Stream StringToStream(string src)
{
var byteArray = Encoding.UTF8.GetBytes(src);
return new MemoryStream(byteArray);
}
}