Skip to content

Commit de4feec

Browse files
committed
Added Base64 string value converter
1 parent 4b02f86 commit de4feec

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/MADE.Data.Converters/Extensions/StringExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
namespace MADE.Data.Converters.Extensions
55
{
66
using System;
7+
using System.IO;
78
using System.Text;
9+
using System.Threading.Tasks;
810

911
/// <summary>
1012
/// Defines a collection of extensions for string values.
@@ -112,6 +114,21 @@ public static string FromBase64(this string base64Value, Encoding encoding = def
112114
return encoding.GetString(Convert.FromBase64String(base64Value));
113115
}
114116

117+
/// <summary>
118+
/// Converts a string value to a <see cref="MemoryStream"/>.
119+
/// </summary>
120+
/// <param name="value">The value to convert.</param>
121+
/// <returns>A <see cref="MemoryStream"/> representing the string value.</returns>
122+
public static async Task<MemoryStream> ToMemoryStreamAsync(this string value)
123+
{
124+
var stream = new MemoryStream();
125+
var writer = new StreamWriter(stream);
126+
await writer.WriteAsync(value);
127+
await writer.FlushAsync();
128+
stream.Position = 0;
129+
return stream;
130+
}
131+
115132
/// <summary>
116133
/// Converts a string value to an integer.
117134
/// </summary>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// MADE Apps licenses this file to you under the MIT license.
2+
// See the LICENSE file in the project root for more information.
3+
4+
namespace MADE.Data.Converters
5+
{
6+
using System.Text;
7+
using MADE.Data.Converters.Extensions;
8+
9+
/// <summary>
10+
/// Defines a value converter from <see cref="string"/> to Base64 <see cref="string"/> with an optional Encoding parameter.
11+
/// </summary>
12+
public partial class StringToBase64StringValueConverter : IValueConverter<string, string>
13+
{
14+
/// <summary>
15+
/// Converts the <paramref name="value">value</paramref> to the Base64 <see cref="string"/>.
16+
/// </summary>
17+
/// <param name="value">
18+
/// The value to convert.
19+
/// </param>
20+
/// <param name="parameter">
21+
/// The optional <see cref="Encoding"/> parameter used to help with conversion.
22+
/// </param>
23+
/// <returns>
24+
/// The converted Base64 <see cref="string"/> object.
25+
/// </returns>
26+
public string Convert(string value, object parameter = default)
27+
{
28+
return value.ToBase64(parameter as Encoding ?? Encoding.UTF8);
29+
}
30+
31+
/// <summary>
32+
/// Converts the Base64 <paramref name="value">value</paramref> back to the original <see cref="string"/> value.
33+
/// </summary>
34+
/// <param name="value">
35+
/// The value to convert.
36+
/// </param>
37+
/// <param name="parameter">
38+
/// The optional <see cref="Encoding"/> parameter used to help with conversion.
39+
/// </param>
40+
/// <returns>
41+
/// The converted <see cref="string"/> object.
42+
/// </returns>
43+
public string ConvertBack(string value, object parameter = default)
44+
{
45+
return value.FromBase64(parameter as Encoding ?? Encoding.UTF8);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)