-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertToBase64Command.cs
More file actions
133 lines (119 loc) · 4.61 KB
/
ConvertToBase64Command.cs
File metadata and controls
133 lines (119 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System.Management.Automation;
using System.Text;
namespace PSBinaryModule.Commands
{
/// <summary>
/// Encodes a string or file contents to Base64.
/// </summary>
[Cmdlet(VerbsData.ConvertTo, "Base64")]
[OutputType(typeof(Base64ConversionResult))]
public sealed class ConvertToBase64Command : PSCmdlet
{
/// <summary>
/// Gets or sets the string to encode.
/// </summary>
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipeline = true,
ParameterSetName = "InputString",
HelpMessage = "String to encode to Base64")]
[ValidateNotNullOrEmpty]
public string InputString { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the file path to encode.
/// </summary>
[Parameter(
Mandatory = true,
Position = 0,
ParameterSetName = "Path",
HelpMessage = "Path to file to encode to Base64")]
[ValidateNotNullOrEmpty]
public string Path { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the encoding to use.
/// </summary>
[Parameter(
Mandatory = false,
HelpMessage = "Text encoding to use (UTF8, ASCII, Unicode). Default is UTF8")]
[ValidateSet("UTF8", "ASCII", "Unicode")]
public string Encoding { get; set; } = "UTF8";
protected override void ProcessRecord()
{
try
{
// Get the data to encode based on parameter set
string dataToEncode;
if (ParameterSetName == "InputString")
{
dataToEncode = InputString;
}
else
{
// Resolve the file path
var resolvedPaths = SessionState.Path.GetResolvedPSPathFromPSPath(Path);
if (resolvedPaths.Count == 0)
{
WriteError(new ErrorRecord(
new FileNotFoundException($"Cannot find path '{Path}' because it does not exist."),
"PathNotFound",
ErrorCategory.ObjectNotFound,
Path));
return;
}
if (resolvedPaths.Count > 1)
{
WriteError(new ErrorRecord(
new ArgumentException($"Path '{Path}' resolves to multiple items. Please specify a single file."),
"MultiplePathsResolved",
ErrorCategory.InvalidArgument,
Path));
return;
}
var resolvedPath = resolvedPaths[0].Path;
if (!File.Exists(resolvedPath))
{
WriteError(new ErrorRecord(
new FileNotFoundException($"Cannot find file '{resolvedPath}'."),
"FileNotFound",
ErrorCategory.ObjectNotFound,
resolvedPath));
return;
}
dataToEncode = File.ReadAllText(resolvedPath);
}
// Get encoding
var enc = GetEncoding(Encoding);
// Encode to Base64
var bytes = enc.GetBytes(dataToEncode);
var encoded = Convert.ToBase64String(bytes);
// Create result
var inputPreview = dataToEncode.Length > 100
? dataToEncode.AsSpan(0, 100).ToString() + "..."
: dataToEncode;
var result = Base64ConversionResult.FromEncoding(
inputPreview,
encoded,
Encoding);
WriteObject(result);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
WriteError(new ErrorRecord(
ex,
"FileAccessError",
ErrorCategory.ReadError,
ParameterSetName == "Path" ? Path : null));
}
}
private static Encoding GetEncoding(string encodingName)
{
return encodingName switch
{
"ASCII" => System.Text.Encoding.ASCII,
"Unicode" => System.Text.Encoding.Unicode,
_ => System.Text.Encoding.UTF8,
};
}
}
}