shoko/Protocols/DataProtoHandler.cs

46 lines
1.1 KiB
C#
Raw Normal View History

2023-10-02 18:49:24 +00:00
using System.Text;
namespace Shoko;
[Protocol("data")]
class DataProtoHandler : ProtoHandler
{
public DataProtoHandler(Uri url)
{
URL = url;
}
2023-10-20 04:59:28 +00:00
public override Task Load()
2023-10-02 18:49:24 +00:00
{
var data = new UriBuilder(URL).Path;
var parts = data.Split(",");
var type = parts[0].Split(";", StringSplitOptions.TrimEntries).ToList();
var dict = new Dictionary<string, string>();
MediaType = type.Count > 0 && type[0].Length > 0 ? type[0] : "text/plain";
type.RemoveAt(0);
foreach (var item in type)
{
var val = item.Split("=", 2);
dict[val[0]] = val.Length > 1 ? val[1] : "";
}
if(dict.ContainsKey("base64"))
{
Content = new MemoryStream(Convert.FromBase64String(parts[1]));
}
else
{
Content = new MemoryStream(Encoding.UTF8.GetBytes(parts[1]));
}
MediaTypeParams = dict;
Status = "OK";
2023-10-20 04:59:28 +00:00
OnLoaded();
return Task.CompletedTask;
2023-10-02 18:49:24 +00:00
}
public override void Render()
{
}
}