浏览代码

Use streams instead of strings

David 4 年之前
父节点
当前提交
2a574914ea

+ 3 - 2
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.Implementations
                     var 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))
                         {

+ 2 - 2
Emby.Server.Implementations/Channels/ChannelManager.cs

@@ -340,8 +340,8 @@ namespace Emby.Server.Implementations.Channels
 
             try
             {
-                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
             {

+ 2 - 2
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");
                 }

+ 2 - 2
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");
                 }

+ 2 - 2
Emby.Server.Implementations/LiveTv/EmbyTV/ItemDataProvider.cs

@@ -44,8 +44,8 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
 
                 try
                 {
-                    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)

+ 2 - 9
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)
                             {

+ 2 - 2
MediaBrowser.Providers/Plugins/AudioDb/AlbumImageProvider.cs

@@ -57,8 +57,8 @@ namespace MediaBrowser.Providers.Plugins.AudioDb
 
                 var 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)
                 {

+ 2 - 2
MediaBrowser.Providers/Plugins/AudioDb/AlbumProvider.cs

@@ -64,8 +64,8 @@ namespace MediaBrowser.Providers.Plugins.AudioDb
 
                 var 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)
                 {

+ 2 - 2
MediaBrowser.Providers/Plugins/AudioDb/ArtistImageProvider.cs

@@ -59,8 +59,8 @@ namespace MediaBrowser.Providers.Plugins.AudioDb
 
                 var 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)
                 {

+ 2 - 2
MediaBrowser.Providers/Plugins/AudioDb/ArtistProvider.cs

@@ -65,8 +65,8 @@ namespace MediaBrowser.Providers.Plugins.AudioDb
 
                 var 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)
                 {

+ 3 - 3
MediaBrowser.Providers/Plugins/Omdb/OmdbProvider.cs

@@ -299,7 +299,7 @@ namespace MediaBrowser.Providers.Plugins.Omdb
             var 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.Omdb
             var 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.Omdb
             var 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);
         }