migrate from XMLSchema to DataContract

This commit is contained in:
Kilian von Pflugk 2024-03-29 15:32:23 +01:00
parent 5d743302e4
commit ef1e3f59c7
2 changed files with 98 additions and 11 deletions

View File

@ -7,7 +7,6 @@ using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Plugins; using MediaBrowser.Common.Plugins;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
@ -24,7 +23,6 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
{ {
private readonly object _serializationLock = new(); private readonly object _serializationLock = new();
private readonly object _introsLock = new(); private readonly object _introsLock = new();
private IXmlSerializer _xmlSerializer;
private ILibraryManager _libraryManager; private ILibraryManager _libraryManager;
private IItemRepository _itemRepository; private IItemRepository _itemRepository;
private ILogger<Plugin> _logger; private ILogger<Plugin> _logger;
@ -54,13 +52,14 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
{ {
Instance = this; Instance = this;
_xmlSerializer = xmlSerializer;
_libraryManager = libraryManager; _libraryManager = libraryManager;
_itemRepository = itemRepository; _itemRepository = itemRepository;
_logger = logger; _logger = logger;
FFmpegPath = serverConfiguration.GetEncodingOptions().EncoderAppPathDisplay; FFmpegPath = serverConfiguration.GetEncodingOptions().EncoderAppPathDisplay;
ArgumentNullException.ThrowIfNull(applicationPaths);
var introsDirectory = Path.Join(applicationPaths.CachePath, "introskipper"); var introsDirectory = Path.Join(applicationPaths.CachePath, "introskipper");
FingerprintCachePath = Path.Join(introsDirectory, "chromaprints"); FingerprintCachePath = Path.Join(introsDirectory, "chromaprints");
_introPath = Path.Join(applicationPaths.CachePath, "introskipper", "intros.xml"); _introPath = Path.Join(applicationPaths.CachePath, "introskipper", "intros.xml");
@ -95,6 +94,10 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
} }
} }
// migrate from XMLSchema to DataContract
XmlSerializationHelper.MigrateXML(_introPath);
XmlSerializationHelper.MigrateXML(_creditsPath);
ConfigurationChanged += OnConfigurationChanged; ConfigurationChanged += OnConfigurationChanged;
// TODO: remove when https://github.com/jellyfin/jellyfin-meta/discussions/30 is complete // TODO: remove when https://github.com/jellyfin/jellyfin-meta/discussions/30 is complete
@ -205,7 +208,14 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
introList.Add(intro.Value); introList.Add(intro.Value);
} }
_xmlSerializer.SerializeToFile(introList, _introPath); try
{
XmlSerializationHelper.SerializeToXml(introList, _introPath);
}
catch (Exception e)
{
_logger.LogError("SaveTimestamps intros {Message}", e.Message);
}
// Serialize credits // Serialize credits
introList.Clear(); introList.Clear();
@ -215,7 +225,14 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
introList.Add(intro.Value); introList.Add(intro.Value);
} }
_xmlSerializer.SerializeToFile(introList, _creditsPath); try
{
XmlSerializationHelper.SerializeToXml(introList, _creditsPath);
}
catch (Exception e)
{
_logger.LogError("SaveTimestamps credits {Message}", e.Message);
}
} }
} }
@ -227,9 +244,7 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
if (File.Exists(_introPath)) if (File.Exists(_introPath))
{ {
// Since dictionaries can't be easily serialized, analysis results are stored on disk as a list. // Since dictionaries can't be easily serialized, analysis results are stored on disk as a list.
var introList = (List<Intro>)_xmlSerializer.DeserializeFromFile( var introList = XmlSerializationHelper.DeserializeFromXml(_introPath);
typeof(List<Intro>),
_introPath);
foreach (var intro in introList) foreach (var intro in introList)
{ {
@ -239,9 +254,7 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
if (File.Exists(_creditsPath)) if (File.Exists(_creditsPath))
{ {
var creditList = (List<Intro>)_xmlSerializer.DeserializeFromFile( var creditList = XmlSerializationHelper.DeserializeFromXml(_creditsPath);
typeof(List<Intro>),
_creditsPath);
foreach (var credit in creditList) foreach (var credit in creditList)
{ {

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
namespace ConfusedPolarBear.Plugin.IntroSkipper
{
internal sealed class XmlSerializationHelper
{
public static void SerializeToXml<T>(T obj, string filePath)
{
// Create a FileStream to write the XML file
using FileStream fileStream = new FileStream(filePath, FileMode.Create);
// Create a DataContractSerializer for type T
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
// Serialize the object to the FileStream
serializer.WriteObject(fileStream, obj);
}
public static List<Intro> DeserializeFromXml(string filePath)
{
var result = new List<Intro>();
try
{
// Create a FileStream to read the XML file
using FileStream fileStream = new FileStream(filePath, FileMode.Open);
// Create an XmlDictionaryReader to read the XML
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fileStream, new XmlDictionaryReaderQuotas());
// Create a DataContractSerializer for type T
DataContractSerializer serializer = new DataContractSerializer(typeof(List<Intro>));
// Deserialize the object from the XML
result = serializer.ReadObject(reader) as List<Intro>;
// Close the reader
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine($"Error deserializing XML: {ex.Message}");
}
#pragma warning disable CS8603
// Return the deserialized object
return result;
#pragma warning restore CS8603
}
public static void MigrateXML(string filePath)
{
if (File.Exists(filePath))
{
string searchString = "<ArrayOfIntro xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
string replacementString = "<ArrayOfIntro xmlns=\"http://schemas.datacontract.org/2004/07/ConfusedPolarBear.Plugin.IntroSkipper\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
// Read the content of the file
string fileContent = File.ReadAllText(filePath, Encoding.UTF8);
// Check if the target string exists at the beginning
if (fileContent.Contains(searchString, StringComparison.OrdinalIgnoreCase))
{
// Replace the target string
fileContent = fileContent.Replace(searchString, replacementString, StringComparison.OrdinalIgnoreCase);
// Write the modified content back to the file
File.WriteAllText(filePath, fileContent, Encoding.UTF8);
}
}
}
}
}