Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base64 converter for chapter name in mediainfo #999

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Shoko.Server/Utilities/MediaInfoLib/MediaInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ private static MediaContainer GetMediaInfo_New(string filename)
Converters = new JsonConverter[]
{
new StreamJsonConverter(), new BooleanConverter(), new StringEnumConverter(),
new DateTimeConverter {DateTimeFormat = "yyyy-MM-dd HH:mm:ss"}, new MultiIntConverter()
new DateTimeConverter {DateTimeFormat = "yyyy-MM-dd HH:mm:ss"}, new MultiIntConverter(),
new MenuBase64Converter()
},
Error = (_, e) =>
{
Expand Down
57 changes: 57 additions & 0 deletions Shoko.Server/Utilities/MediaInfoLib/MenuBase64Converter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Shoko.Server.Utilities.MediaInfoLib
{
public class MenuBase64Converter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType.IsAssignableFrom(typeof(Dictionary<string,string>));
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;

var obj = JObject.Load(reader);

if (reader.Path != "extra") return obj.ToObject<Dictionary<string, string>>(); // Only continue if we are changing "extra" object in menu stream. Otherwise return as is.

var sanitizedDictionary = new Dictionary<string, string>();

foreach (var (key, value) in obj)
{
switch (value?.Type)
{
case JTokenType.String:
sanitizedDictionary[key] = value?.ToString();
break;
case JTokenType.Object:
{
var base64EncodedBytes = Convert.FromBase64String(value?["#value"]?.ToString() ?? "");
sanitizedDictionary[key] = Encoding.UTF8.GetString(base64EncodedBytes).Trim();
break;
}
default:
throw new NotImplementedException();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was not sure what to put for default case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw something more specific then a not implemented description, or at least provide a message

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's intentionally not implemented, I tend to go for a NotSupportedException

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to
throw new NotSupportedException("Unknown type encountered in MenuStream.extra");

}
}

return sanitizedDictionary;
}

public override bool CanWrite => false;

public override bool CanRead => true;

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}