The Free Software Media System - Server Backend & API
Use streams instead of strings
David2a574914eaca
diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.csindex fcc156e9c5..99cd3b6cc1 100644--- a/Emby.Server.Implementations/ApplicationHost.cs+++ b/Emby.Server.Implementations/ApplicationHost.cs@@ -1038,6 +1038,7 @@ namespace Emby.Server.Implementations}var directories = Directory.EnumerateDirectories(path, "*.*", SearchOption.TopDirectoryOnly);+ var jsonOptions = JsonDefaults.GetOptions();foreach (var dir in directories){@@ -1046,8 +1047,8 @@ namespace Emby.Server.Implementationsvar metafile = Path.Combine(dir, "meta.json");if (File.Exists(metafile)){- var jsonString = File.ReadAllText(metafile);- var manifest = JsonSerializer.Deserialize<PluginManifest>(jsonString, JsonDefaults.GetOptions());+ using FileStream jsonStream = File.OpenRead(metafile);+ var manifest = JsonSerializer.DeserializeAsync<PluginManifest>(jsonStream, jsonOptions).GetAwaiter().GetResult();if (!Version.TryParse(manifest.TargetAbi, out var targetAbi)){diff --git a/Emby.Server.Implementations/Channels/ChannelManager.cs b/Emby.Server.Implementations/Channels/ChannelManager.csindex 4e4fbf82c6..b462d7bdcd 100644--- a/Emby.Server.Implementations/Channels/ChannelManager.cs+++ b/Emby.Server.Implementations/Channels/ChannelManager.cs@@ -340,8 +340,8 @@ namespace Emby.Server.Implementations.Channelstry{- var jsonString = File.ReadAllText(path);- return JsonSerializer.Deserialize<List<MediaSourceInfo>>(jsonString, JsonDefaults.GetOptions()) ?? new List<MediaSourceInfo>();+ using FileStream jsonStream = File.OpenRead(path);+ return JsonSerializer.DeserializeAsync<List<MediaSourceInfo>>(jsonStream, JsonDefaults.GetOptions()).GetAwaiter().GetResult();}catch{diff --git a/Emby.Server.Implementations/Library/LiveStreamHelper.cs b/Emby.Server.Implementations/Library/LiveStreamHelper.csindex ad7988fb15..3ceba0fe94 100644--- a/Emby.Server.Implementations/Library/LiveStreamHelper.cs+++ b/Emby.Server.Implementations/Library/LiveStreamHelper.cs@@ -46,8 +46,8 @@ namespace Emby.Server.Implementations.Library{try{- var jsonString = await File.ReadAllTextAsync(cacheFilePath, cancellationToken).ConfigureAwait(false);- JsonSerializer.Deserialize<MediaInfo>(jsonString, JsonDefaults.GetOptions());+ await using FileStream jsonStream = File.OpenRead(cacheFilePath);+ await JsonSerializer.DeserializeAsync<MediaInfo>(jsonStream, JsonDefaults.GetOptions(), cancellationToken).ConfigureAwait(false);// _logger.LogDebug("Found cached media info");}diff --git a/Emby.Server.Implementations/Library/MediaSourceManager.cs b/Emby.Server.Implementations/Library/MediaSourceManager.csindex 264390dd33..b3900e4aa1 100644--- a/Emby.Server.Implementations/Library/MediaSourceManager.cs+++ b/Emby.Server.Implementations/Library/MediaSourceManager.cs@@ -641,8 +641,8 @@ namespace Emby.Server.Implementations.Library{try{- var json = await File.ReadAllTextAsync(cacheFilePath, cancellationToken).ConfigureAwait(false);- mediaInfo = JsonSerializer.Deserialize<MediaInfo>(json, JsonDefaults.GetOptions());+ await using FileStream jsonStream = File.OpenRead(cacheFilePath);+ mediaInfo = await JsonSerializer.DeserializeAsync<MediaInfo>(jsonStream, JsonDefaults.GetOptions(), cancellationToken).ConfigureAwait(false);// _logger.LogDebug("Found cached media info");}diff --git a/Emby.Server.Implementations/LiveTv/EmbyTV/ItemDataProvider.cs b/Emby.Server.Implementations/LiveTv/EmbyTV/ItemDataProvider.csindex 5256a0ee72..1b9abaa78e 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.EmbyTVtry{- var json = File.ReadAllText(_dataPath);- _items = JsonSerializer.Deserialize<T[]>(json, JsonDefaults.GetOptions());+ using FileStream jsonStream = File.OpenRead(_dataPath);+ _items = JsonSerializer.DeserializeAsync<T[]>(jsonStream, JsonDefaults.GetOptions()).GetAwaiter().GetResult();return;}catch (Exception ex)diff --git a/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs b/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.csindex eee7aeea30..a9f4599864 100644--- a/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs+++ b/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs@@ -139,15 +139,8 @@ namespace Emby.Server.Implementations.ScheduledTasks{try{- var jsonString = File.ReadAllText(path);- if (!string.IsNullOrWhiteSpace(jsonString))- {- _lastExecutionResult = JsonSerializer.Deserialize<TaskResult>(jsonString, JsonDefaults.GetOptions());- }- else- {- _logger.LogDebug("Scheduled Task history file {path} is empty. Skipping deserialization.", path);- }+ using FileStream jsonStream = File.OpenRead(path);+ _lastExecutionResult = JsonSerializer.DeserializeAsync<TaskResult>(jsonStream, JsonDefaults.GetOptions()).GetAwaiter().GetResult();}catch (Exception ex){diff --git a/MediaBrowser.Providers/Plugins/AudioDb/AlbumImageProvider.cs b/MediaBrowser.Providers/Plugins/AudioDb/AlbumImageProvider.csindex 605b93788b..a5aeba7d2d 100644--- a/MediaBrowser.Providers/Plugins/AudioDb/AlbumImageProvider.cs+++ b/MediaBrowser.Providers/Plugins/AudioDb/AlbumImageProvider.cs@@ -57,8 +57,8 @@ namespace MediaBrowser.Providers.Plugins.AudioDbvar path = AudioDbAlbumProvider.GetAlbumInfoPath(_config.ApplicationPaths, id);- var jsonString = await File.ReadAllTextAsync(path, cancellationToken).ConfigureAwait(false);- var obj = JsonSerializer.Deserialize<AudioDbAlbumProvider.RootObject>(jsonString, JsonDefaults.GetOptions());+ await using FileStream jsonStream = File.OpenRead(path);+ var obj = await JsonSerializer.DeserializeAsync<AudioDbAlbumProvider.RootObject>(jsonStream, JsonDefaults.GetOptions(), cancellationToken).ConfigureAwait(false);if (obj != null && obj.album != null && obj.album.Count > 0){diff --git a/MediaBrowser.Providers/Plugins/AudioDb/AlbumProvider.cs b/MediaBrowser.Providers/Plugins/AudioDb/AlbumProvider.csindex 32ef135475..9a61abc1ac 100644--- a/MediaBrowser.Providers/Plugins/AudioDb/AlbumProvider.cs+++ b/MediaBrowser.Providers/Plugins/AudioDb/AlbumProvider.cs@@ -64,8 +64,8 @@ namespace MediaBrowser.Providers.Plugins.AudioDbvar path = GetAlbumInfoPath(_config.ApplicationPaths, id);- var jsonString = await File.ReadAllTextAsync(path, cancellationToken).ConfigureAwait(false);- var obj = JsonSerializer.Deserialize<RootObject>(jsonString, JsonDefaults.GetOptions());+ await using FileStream jsonStream = File.OpenRead(path);+ var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, JsonDefaults.GetOptions(), cancellationToken).ConfigureAwait(false);if (obj != null && obj.album != null && obj.album.Count > 0){diff --git a/MediaBrowser.Providers/Plugins/AudioDb/ArtistImageProvider.cs b/MediaBrowser.Providers/Plugins/AudioDb/ArtistImageProvider.csindex f0b8e00f3f..639bbe8595 100644--- a/MediaBrowser.Providers/Plugins/AudioDb/ArtistImageProvider.cs+++ b/MediaBrowser.Providers/Plugins/AudioDb/ArtistImageProvider.cs@@ -59,8 +59,8 @@ namespace MediaBrowser.Providers.Plugins.AudioDbvar path = AudioDbArtistProvider.GetArtistInfoPath(_config.ApplicationPaths, id);- var jsonString = await File.ReadAllTextAsync(path, cancellationToken).ConfigureAwait(false);- var obj = JsonSerializer.Deserialize<AudioDbArtistProvider.RootObject>(jsonString, JsonDefaults.GetOptions());+ await using FileStream jsonStream = File.OpenRead(path);+ var obj = await JsonSerializer.DeserializeAsync<AudioDbArtistProvider.RootObject>(jsonStream, JsonDefaults.GetOptions(), cancellationToken).ConfigureAwait(false);if (obj != null && obj.artists != null && obj.artists.Count > 0){diff --git a/MediaBrowser.Providers/Plugins/AudioDb/ArtistProvider.cs b/MediaBrowser.Providers/Plugins/AudioDb/ArtistProvider.csindex 2476c86715..71efd194e3 100644--- a/MediaBrowser.Providers/Plugins/AudioDb/ArtistProvider.cs+++ b/MediaBrowser.Providers/Plugins/AudioDb/ArtistProvider.cs@@ -65,8 +65,8 @@ namespace MediaBrowser.Providers.Plugins.AudioDbvar path = GetArtistInfoPath(_config.ApplicationPaths, id);- var jsonString = await File.ReadAllTextAsync(path, cancellationToken).ConfigureAwait(false);- var obj = JsonSerializer.Deserialize<RootObject>(jsonString, JsonDefaults.GetOptions());+ await using FileStream jsonStream = File.OpenRead(path);+ var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, JsonDefaults.GetOptions(), cancellationToken).ConfigureAwait(false);if (obj != null && obj.artists != null && obj.artists.Count > 0){diff --git a/MediaBrowser.Providers/Plugins/Omdb/OmdbProvider.cs b/MediaBrowser.Providers/Plugins/Omdb/OmdbProvider.csindex bbedffbc7d..fa75e4ec49 100644--- a/MediaBrowser.Providers/Plugins/Omdb/OmdbProvider.cs+++ b/MediaBrowser.Providers/Plugins/Omdb/OmdbProvider.cs@@ -299,7 +299,7 @@ namespace MediaBrowser.Providers.Plugins.Omdbvar jsonOptions = JsonDefaults.GetOptions();var rootObject = await GetDeserializedOmdbResponse<RootObject>(_httpClientFactory.CreateClient(NamedClient.Default), url, jsonOptions, cancellationToken).ConfigureAwait(false);Directory.CreateDirectory(Path.GetDirectoryName(path));- await using FileStream jsonFileStream = File.Create(path);+ await using FileStream jsonFileStream = File.OpenWrite(path);await JsonSerializer.SerializeAsync(jsonFileStream, rootObject, jsonOptions, cancellationToken).ConfigureAwait(false);return path;@@ -337,7 +337,7 @@ namespace MediaBrowser.Providers.Plugins.Omdbvar jsonOptions = JsonDefaults.GetOptions();var rootObject = await GetDeserializedOmdbResponse<SeasonRootObject>(_httpClientFactory.CreateClient(NamedClient.Default), url, jsonOptions, cancellationToken).ConfigureAwait(false);Directory.CreateDirectory(Path.GetDirectoryName(path));- await using FileStream jsonFileStream = File.Create(path);+ await using FileStream jsonFileStream = File.OpenWrite(path);await JsonSerializer.SerializeAsync(jsonFileStream, rootObject, jsonOptions, cancellationToken).ConfigureAwait(false);return path;@@ -349,7 +349,7 @@ namespace MediaBrowser.Providers.Plugins.Omdbvar content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);// OMDb is sending "N/A" for no empty number fields- content = content.Replace("\"N/A\"", "\"0\"", StringComparison.InvariantCulture);+ content = content.Replace("\"N/A\"", "\"\"", StringComparison.InvariantCulture);return JsonSerializer.Deserialize<T>(content, jsonOptions);}