mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 03:36:44 +02:00
Added base64 decode function to the Value Generator (#27835)
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using Wox.Plugin.Logger;
|
||||
|
||||
namespace Community.PowerToys.Run.Plugin.ValueGenerator.Base64
|
||||
{
|
||||
public class Base64DecodeRequest : IComputeRequest
|
||||
{
|
||||
public byte[] Result { get; set; }
|
||||
|
||||
public string Description => "Base64 Decoding";
|
||||
|
||||
public bool IsSuccessful { get; set; }
|
||||
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
private string DataToDecode { get; set; }
|
||||
|
||||
public Base64DecodeRequest(string dataToDecode)
|
||||
{
|
||||
DataToDecode = dataToDecode ?? throw new ArgumentNullException(nameof(dataToDecode));
|
||||
}
|
||||
|
||||
public bool Compute()
|
||||
{
|
||||
IsSuccessful = true;
|
||||
try
|
||||
{
|
||||
Result = System.Convert.FromBase64String(DataToDecode);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception(e.Message, e, GetType());
|
||||
ErrorMessage = e.Message;
|
||||
IsSuccessful = false;
|
||||
}
|
||||
|
||||
return IsSuccessful;
|
||||
}
|
||||
|
||||
public string ResultToString()
|
||||
{
|
||||
if (Result != null)
|
||||
{
|
||||
return Encoding.UTF8.GetString(Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -121,6 +121,12 @@ namespace Community.PowerToys.Run.Plugin.ValueGenerator
|
||||
string content = query.RawUserQuery.Substring(commandIndex + command.Length).Trim();
|
||||
request = new Base64Request(Encoding.UTF8.GetBytes(content));
|
||||
}
|
||||
else if (command.ToLower(null) == "base64d")
|
||||
{
|
||||
int commandIndex = query.RawUserQuery.IndexOf(command, StringComparison.InvariantCultureIgnoreCase);
|
||||
string content = query.RawUserQuery.Substring(commandIndex + command.Length).Trim();
|
||||
request = new Base64DecodeRequest(content);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FormatException($"Invalid Query: {query.RawUserQuery}");
|
||||
|
||||
Reference in New Issue
Block a user