From e835dfb27d4a25e2057047692fd58af439b15acc Mon Sep 17 00:00:00 2001 From: David Date: Thu, 24 Dec 2020 10:31:51 +0100 Subject: [PATCH] Use sync string instead of file --- Emby.Server.Implementations/ApplicationHost.cs | 6 +++--- .../Channels/ChannelManager.cs | 5 +++-- .../LiveTv/EmbyTV/ItemDataProvider.cs | 4 ++-- .../ScheduledTasks/ScheduledTaskWorker.cs | 11 +++++++++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index 99cd3b6cc..6e360ed85 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -10,6 +10,7 @@ using System.Net; using System.Reflection; using System.Runtime.InteropServices; using System.Security.Cryptography.X509Certificates; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Emby.Dlna; @@ -100,7 +101,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Prometheus.DotNetRuntime; -using JsonSerializer = System.Text.Json.JsonSerializer; using OperatingSystem = MediaBrowser.Common.System.OperatingSystem; using WebSocketManager = Emby.Server.Implementations.HttpServer.WebSocketManager; @@ -1047,8 +1047,8 @@ namespace Emby.Server.Implementations var metafile = Path.Combine(dir, "meta.json"); if (File.Exists(metafile)) { - using FileStream jsonStream = File.OpenRead(metafile); - var manifest = JsonSerializer.DeserializeAsync(jsonStream, jsonOptions).GetAwaiter().GetResult(); + var jsonString = File.ReadAllText(metafile); + var manifest = JsonSerializer.Deserialize(jsonString, jsonOptions); if (!Version.TryParse(manifest.TargetAbi, out var targetAbi)) { diff --git a/Emby.Server.Implementations/Channels/ChannelManager.cs b/Emby.Server.Implementations/Channels/ChannelManager.cs index 3663e5094..f06ad4436 100644 --- a/Emby.Server.Implementations/Channels/ChannelManager.cs +++ b/Emby.Server.Implementations/Channels/ChannelManager.cs @@ -340,8 +340,8 @@ namespace Emby.Server.Implementations.Channels try { - using FileStream jsonStream = File.OpenRead(path); - return JsonSerializer.DeserializeAsync>(jsonStream, JsonDefaults.GetOptions()).GetAwaiter().GetResult() + var jsonString = File.ReadAllText(path); + return JsonSerializer.Deserialize>(jsonString, JsonDefaults.GetOptions()) ?? new List(); } catch @@ -368,6 +368,7 @@ namespace Emby.Server.Implementations.Channels } Directory.CreateDirectory(Path.GetDirectoryName(path)); + await using FileStream createStream = File.Create(path); await JsonSerializer.SerializeAsync(createStream, mediaSources, JsonDefaults.GetOptions()).ConfigureAwait(false); } diff --git a/Emby.Server.Implementations/LiveTv/EmbyTV/ItemDataProvider.cs b/Emby.Server.Implementations/LiveTv/EmbyTV/ItemDataProvider.cs index 1b9abaa78..bbe9b50dd 100644 --- a/Emby.Server.Implementations/LiveTv/EmbyTV/ItemDataProvider.cs +++ b/Emby.Server.Implementations/LiveTv/EmbyTV/ItemDataProvider.cs @@ -44,8 +44,8 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV try { - using FileStream jsonStream = File.OpenRead(_dataPath); - _items = JsonSerializer.DeserializeAsync(jsonStream, JsonDefaults.GetOptions()).GetAwaiter().GetResult(); + var jsonString = File.ReadAllText(_dataPath); + _items = JsonSerializer.Deserialize(jsonString, JsonDefaults.GetOptions()); return; } catch (Exception ex) diff --git a/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs b/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs index a9f459986..eee7aeea3 100644 --- a/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs +++ b/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs @@ -139,8 +139,15 @@ namespace Emby.Server.Implementations.ScheduledTasks { try { - using FileStream jsonStream = File.OpenRead(path); - _lastExecutionResult = JsonSerializer.DeserializeAsync(jsonStream, JsonDefaults.GetOptions()).GetAwaiter().GetResult(); + var jsonString = File.ReadAllText(path); + if (!string.IsNullOrWhiteSpace(jsonString)) + { + _lastExecutionResult = JsonSerializer.Deserialize(jsonString, JsonDefaults.GetOptions()); + } + else + { + _logger.LogDebug("Scheduled Task history file {path} is empty. Skipping deserialization.", path); + } } catch (Exception ex) {