2020-08-07 08:38:01 -07:00
|
|
|
#nullable enable
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
using System;
|
2016-10-28 22:40:15 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-11-13 11:14:44 -07:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2016-10-28 22:40:15 -07:00
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
2017-02-20 13:50:58 -07:00
|
|
|
namespace Emby.Server.Implementations.AppBase
|
2016-10-28 22:40:15 -07:00
|
|
|
{
|
|
|
|
/// <summary>
|
2019-10-25 03:47:20 -07:00
|
|
|
/// Class ConfigurationHelper.
|
2016-10-28 22:40:15 -07:00
|
|
|
/// </summary>
|
|
|
|
public static class ConfigurationHelper
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Reads an xml configuration file from the file system
|
2019-10-25 03:47:20 -07:00
|
|
|
/// It will immediately re-serialize and save if new serialization data is available due to property changes.
|
2016-10-28 22:40:15 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <param name="xmlSerializer">The XML serializer.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
2019-02-06 12:38:42 -07:00
|
|
|
public static object GetXmlConfiguration(Type type, string path, IXmlSerializer xmlSerializer)
|
2016-10-28 22:40:15 -07:00
|
|
|
{
|
|
|
|
object configuration;
|
|
|
|
|
2020-08-07 08:38:01 -07:00
|
|
|
byte[]? buffer = null;
|
2016-10-28 22:40:15 -07:00
|
|
|
|
|
|
|
// Use try/catch to avoid the extra file system lookup using File.Exists
|
|
|
|
try
|
|
|
|
{
|
2019-01-26 15:09:07 -07:00
|
|
|
buffer = File.ReadAllBytes(path);
|
2016-10-28 22:40:15 -07:00
|
|
|
|
|
|
|
configuration = xmlSerializer.DeserializeFromBytes(type, buffer);
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
2020-11-13 18:04:06 -07:00
|
|
|
configuration = Activator.CreateInstance(type) ?? throw new ArgumentException($"Provided path ({type}) is not valid.", nameof(type));
|
2016-10-28 22:40:15 -07:00
|
|
|
}
|
|
|
|
|
2020-08-07 08:38:01 -07:00
|
|
|
using var stream = new MemoryStream(buffer?.Length ?? 0);
|
2020-04-14 12:11:21 -07:00
|
|
|
xmlSerializer.SerializeToStream(configuration, stream);
|
2016-10-28 22:40:15 -07:00
|
|
|
|
2020-04-14 12:11:21 -07:00
|
|
|
// Take the object we just got and serialize it back to bytes
|
2020-08-07 08:38:01 -07:00
|
|
|
byte[] newBytes = stream.GetBuffer();
|
|
|
|
int newBytesLen = (int)stream.Length;
|
2016-10-28 22:40:15 -07:00
|
|
|
|
2020-04-14 12:11:21 -07:00
|
|
|
// If the file didn't exist before, or if something has changed, re-save
|
2020-08-07 08:38:01 -07:00
|
|
|
if (buffer == null || !newBytes.AsSpan(0, newBytesLen).SequenceEqual(buffer))
|
2020-04-14 12:11:21 -07:00
|
|
|
{
|
2020-11-13 18:04:06 -07:00
|
|
|
var directory = Path.GetDirectoryName(path) ?? throw new ArgumentException($"Provided path ({path}) is not valid.", nameof(path));
|
2016-10-28 22:40:15 -07:00
|
|
|
|
2020-11-13 09:41:18 -07:00
|
|
|
Directory.CreateDirectory(directory);
|
2020-04-14 12:11:21 -07:00
|
|
|
// Save it after load in case we got new items
|
2021-03-07 06:43:28 -07:00
|
|
|
// use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
|
|
|
|
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
|
2020-08-07 08:38:01 -07:00
|
|
|
{
|
|
|
|
fs.Write(newBytes, 0, newBytesLen);
|
|
|
|
}
|
2016-10-28 22:40:15 -07:00
|
|
|
}
|
2020-04-14 12:11:21 -07:00
|
|
|
|
|
|
|
return configuration;
|
2016-10-28 22:40:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|