mirror of https://github.com/jellyfin/jellyfin.git
Removed nesting levels through block-scoped `using` statement (#10025)
Co-authored-by: John Doe <john@doe> Co-authored-by: Lehonti Ramos <lehonti@ramos>
This commit is contained in:
parent
c79c59a32b
commit
bc959270b7
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
@ -59,21 +59,17 @@ public class MigrateMusicBrainzTimeout : IMigrationRoutine
|
|||
|
||||
private OldMusicBrainzConfiguration? ReadOld(string path)
|
||||
{
|
||||
using (var xmlReader = XmlReader.Create(path))
|
||||
{
|
||||
var serverConfigSerializer = new XmlSerializer(typeof(OldMusicBrainzConfiguration), new XmlRootAttribute("PluginConfiguration"));
|
||||
return serverConfigSerializer.Deserialize(xmlReader) as OldMusicBrainzConfiguration;
|
||||
}
|
||||
using var xmlReader = XmlReader.Create(path);
|
||||
var serverConfigSerializer = new XmlSerializer(typeof(OldMusicBrainzConfiguration), new XmlRootAttribute("PluginConfiguration"));
|
||||
return serverConfigSerializer.Deserialize(xmlReader) as OldMusicBrainzConfiguration;
|
||||
}
|
||||
|
||||
private void WriteNew(string path, PluginConfiguration newPluginConfiguration)
|
||||
{
|
||||
var pluginConfigurationSerializer = new XmlSerializer(typeof(PluginConfiguration), new XmlRootAttribute("PluginConfiguration"));
|
||||
var xmlWriterSettings = new XmlWriterSettings { Indent = true };
|
||||
using (var xmlWriter = XmlWriter.Create(path, xmlWriterSettings))
|
||||
{
|
||||
pluginConfigurationSerializer.Serialize(xmlWriter, newPluginConfiguration);
|
||||
}
|
||||
using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
|
||||
pluginConfigurationSerializer.Serialize(xmlWriter, newPluginConfiguration);
|
||||
}
|
||||
|
||||
#pragma warning disable
|
||||
|
|
|
@ -43,10 +43,8 @@ public class MigrateNetworkConfiguration : IMigrationRoutine
|
|||
|
||||
try
|
||||
{
|
||||
using (var xmlReader = XmlReader.Create(path))
|
||||
{
|
||||
oldNetworkConfiguration = (OldNetworkConfiguration?)oldNetworkConfigSerializer.Deserialize(xmlReader);
|
||||
}
|
||||
using var xmlReader = XmlReader.Create(path);
|
||||
oldNetworkConfiguration = (OldNetworkConfiguration?)oldNetworkConfigSerializer.Deserialize(xmlReader);
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
|
@ -97,10 +95,8 @@ public class MigrateNetworkConfiguration : IMigrationRoutine
|
|||
|
||||
var networkConfigSerializer = new XmlSerializer(typeof(NetworkConfiguration));
|
||||
var xmlWriterSettings = new XmlWriterSettings { Indent = true };
|
||||
using (var xmlWriter = XmlWriter.Create(path, xmlWriterSettings))
|
||||
{
|
||||
networkConfigSerializer.Serialize(xmlWriter, networkConfiguration);
|
||||
}
|
||||
using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
|
||||
networkConfigSerializer.Serialize(xmlWriter, networkConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -489,10 +489,8 @@ public class SkiaEncoder : IImageEncoder
|
|||
Directory.CreateDirectory(directory);
|
||||
using (var outputStream = new SKFileWStream(outputPath))
|
||||
{
|
||||
using (var pixmap = new SKPixmap(new SKImageInfo(width, height), saveBitmap.GetPixels()))
|
||||
{
|
||||
pixmap.Encode(outputStream, skiaOutputFormat, quality);
|
||||
}
|
||||
using var pixmap = new SKPixmap(new SKImageInfo(width, height), saveBitmap.GetPixels());
|
||||
pixmap.Encode(outputStream, skiaOutputFormat, quality);
|
||||
}
|
||||
|
||||
return outputPath;
|
||||
|
|
|
@ -111,10 +111,8 @@ public sealed class ImageProcessor : IImageProcessor, IDisposable
|
|||
public async Task ProcessImage(ImageProcessingOptions options, Stream toStream)
|
||||
{
|
||||
var file = await ProcessImage(options).ConfigureAwait(false);
|
||||
using (var fileStream = AsyncFile.OpenRead(file.Path))
|
||||
{
|
||||
await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
|
||||
}
|
||||
using var fileStream = AsyncFile.OpenRead(file.Path);
|
||||
await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
|
@ -26,10 +26,8 @@ namespace Jellyfin.Extensions
|
|||
/// <returns>All lines in the stream.</returns>
|
||||
public static string[] ReadAllLines(this Stream stream, Encoding encoding)
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(stream, encoding))
|
||||
{
|
||||
return ReadAllLines(reader).ToArray();
|
||||
}
|
||||
using StreamReader reader = new StreamReader(stream, encoding);
|
||||
return ReadAllLines(reader).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -12,17 +12,16 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||
[Fact]
|
||||
public void Parse_Valid_Success()
|
||||
{
|
||||
using (var stream = File.OpenRead("Test Data/example.ass"))
|
||||
{
|
||||
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "ass");
|
||||
Assert.Single(parsed.TrackEvents);
|
||||
var trackEvent = parsed.TrackEvents[0];
|
||||
using var stream = File.OpenRead("Test Data/example.ass");
|
||||
|
||||
Assert.Equal("1", trackEvent.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:00:01.18", CultureInfo.InvariantCulture).Ticks, trackEvent.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:00:06.85", CultureInfo.InvariantCulture).Ticks, trackEvent.EndPositionTicks);
|
||||
Assert.Equal("{\\pos(400,570)}Like an Angel with pity on nobody" + Environment.NewLine + "The second line in subtitle", trackEvent.Text);
|
||||
}
|
||||
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "ass");
|
||||
Assert.Single(parsed.TrackEvents);
|
||||
var trackEvent = parsed.TrackEvents[0];
|
||||
|
||||
Assert.Equal("1", trackEvent.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:00:01.18", CultureInfo.InvariantCulture).Ticks, trackEvent.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:00:06.85", CultureInfo.InvariantCulture).Ticks, trackEvent.EndPositionTicks);
|
||||
Assert.Equal("{\\pos(400,570)}Like an Angel with pity on nobody" + Environment.NewLine + "The second line in subtitle", trackEvent.Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,45 +12,43 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||
[Fact]
|
||||
public void Parse_Valid_Success()
|
||||
{
|
||||
using (var stream = File.OpenRead("Test Data/example.srt"))
|
||||
{
|
||||
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "srt");
|
||||
Assert.Equal(2, parsed.TrackEvents.Count);
|
||||
using var stream = File.OpenRead("Test Data/example.srt");
|
||||
|
||||
var trackEvent1 = parsed.TrackEvents[0];
|
||||
Assert.Equal("1", trackEvent1.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:02:17.440", CultureInfo.InvariantCulture).Ticks, trackEvent1.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:02:20.375", CultureInfo.InvariantCulture).Ticks, trackEvent1.EndPositionTicks);
|
||||
Assert.Equal("Senator, we're making" + Environment.NewLine + "our final approach into Coruscant.", trackEvent1.Text);
|
||||
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "srt");
|
||||
Assert.Equal(2, parsed.TrackEvents.Count);
|
||||
|
||||
var trackEvent2 = parsed.TrackEvents[1];
|
||||
Assert.Equal("2", trackEvent2.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:02:20.476", CultureInfo.InvariantCulture).Ticks, trackEvent2.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:02:22.501", CultureInfo.InvariantCulture).Ticks, trackEvent2.EndPositionTicks);
|
||||
Assert.Equal("Very good, Lieutenant.", trackEvent2.Text);
|
||||
}
|
||||
var trackEvent1 = parsed.TrackEvents[0];
|
||||
Assert.Equal("1", trackEvent1.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:02:17.440", CultureInfo.InvariantCulture).Ticks, trackEvent1.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:02:20.375", CultureInfo.InvariantCulture).Ticks, trackEvent1.EndPositionTicks);
|
||||
Assert.Equal("Senator, we're making" + Environment.NewLine + "our final approach into Coruscant.", trackEvent1.Text);
|
||||
|
||||
var trackEvent2 = parsed.TrackEvents[1];
|
||||
Assert.Equal("2", trackEvent2.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:02:20.476", CultureInfo.InvariantCulture).Ticks, trackEvent2.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:02:22.501", CultureInfo.InvariantCulture).Ticks, trackEvent2.EndPositionTicks);
|
||||
Assert.Equal("Very good, Lieutenant.", trackEvent2.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_EmptyNewlineBetweenText_Success()
|
||||
{
|
||||
using (var stream = File.OpenRead("Test Data/example2.srt"))
|
||||
{
|
||||
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "srt");
|
||||
Assert.Equal(2, parsed.TrackEvents.Count);
|
||||
using var stream = File.OpenRead("Test Data/example2.srt");
|
||||
|
||||
var trackEvent1 = parsed.TrackEvents[0];
|
||||
Assert.Equal("311", trackEvent1.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:16:46.465", CultureInfo.InvariantCulture).Ticks, trackEvent1.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:16:49.009", CultureInfo.InvariantCulture).Ticks, trackEvent1.EndPositionTicks);
|
||||
Assert.Equal("Una vez que la gente se entere" + Environment.NewLine + Environment.NewLine + "de que ustedes están aquí,", trackEvent1.Text);
|
||||
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "srt");
|
||||
Assert.Equal(2, parsed.TrackEvents.Count);
|
||||
|
||||
var trackEvent2 = parsed.TrackEvents[1];
|
||||
Assert.Equal("312", trackEvent2.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:16:49.092", CultureInfo.InvariantCulture).Ticks, trackEvent2.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:16:51.470", CultureInfo.InvariantCulture).Ticks, trackEvent2.EndPositionTicks);
|
||||
Assert.Equal("este lugar se convertirá" + Environment.NewLine + Environment.NewLine + "en un maldito zoológico.", trackEvent2.Text);
|
||||
}
|
||||
var trackEvent1 = parsed.TrackEvents[0];
|
||||
Assert.Equal("311", trackEvent1.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:16:46.465", CultureInfo.InvariantCulture).Ticks, trackEvent1.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:16:49.009", CultureInfo.InvariantCulture).Ticks, trackEvent1.EndPositionTicks);
|
||||
Assert.Equal("Una vez que la gente se entere" + Environment.NewLine + Environment.NewLine + "de que ustedes están aquí,", trackEvent1.Text);
|
||||
|
||||
var trackEvent2 = parsed.TrackEvents[1];
|
||||
Assert.Equal("312", trackEvent2.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:16:49.092", CultureInfo.InvariantCulture).Ticks, trackEvent2.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:16:51.470", CultureInfo.InvariantCulture).Ticks, trackEvent2.EndPositionTicks);
|
||||
Assert.Equal("este lugar se convertirá" + Environment.NewLine + Environment.NewLine + "en un maldito zoológico.", trackEvent2.Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,22 +18,21 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||
[MemberData(nameof(Parse_MultipleDialogues_TestData))]
|
||||
public void Parse_MultipleDialogues_Success(string ssa, IReadOnlyList<SubtitleTrackEvent> expectedSubtitleTrackEvents)
|
||||
{
|
||||
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa)))
|
||||
using Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa));
|
||||
|
||||
SubtitleTrackInfo subtitleTrackInfo = _parser.Parse(stream, "ssa");
|
||||
|
||||
Assert.Equal(expectedSubtitleTrackEvents.Count, subtitleTrackInfo.TrackEvents.Count);
|
||||
|
||||
for (int i = 0; i < expectedSubtitleTrackEvents.Count; ++i)
|
||||
{
|
||||
SubtitleTrackInfo subtitleTrackInfo = _parser.Parse(stream, "ssa");
|
||||
SubtitleTrackEvent expected = expectedSubtitleTrackEvents[i];
|
||||
SubtitleTrackEvent actual = subtitleTrackInfo.TrackEvents[i];
|
||||
|
||||
Assert.Equal(expectedSubtitleTrackEvents.Count, subtitleTrackInfo.TrackEvents.Count);
|
||||
|
||||
for (int i = 0; i < expectedSubtitleTrackEvents.Count; ++i)
|
||||
{
|
||||
SubtitleTrackEvent expected = expectedSubtitleTrackEvents[i];
|
||||
SubtitleTrackEvent actual = subtitleTrackInfo.TrackEvents[i];
|
||||
|
||||
Assert.Equal(expected.Id, actual.Id);
|
||||
Assert.Equal(expected.Text, actual.Text);
|
||||
Assert.Equal(expected.StartPositionTicks, actual.StartPositionTicks);
|
||||
Assert.Equal(expected.EndPositionTicks, actual.EndPositionTicks);
|
||||
}
|
||||
Assert.Equal(expected.Id, actual.Id);
|
||||
Assert.Equal(expected.Text, actual.Text);
|
||||
Assert.Equal(expected.StartPositionTicks, actual.StartPositionTicks);
|
||||
Assert.Equal(expected.EndPositionTicks, actual.EndPositionTicks);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,17 +72,16 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||
[Fact]
|
||||
public void Parse_Valid_Success()
|
||||
{
|
||||
using (var stream = File.OpenRead("Test Data/example.ssa"))
|
||||
{
|
||||
var parsed = _parser.Parse(stream, "ssa");
|
||||
Assert.Single(parsed.TrackEvents);
|
||||
var trackEvent = parsed.TrackEvents[0];
|
||||
using var stream = File.OpenRead("Test Data/example.ssa");
|
||||
|
||||
Assert.Equal("1", trackEvent.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:00:01.18", CultureInfo.InvariantCulture).Ticks, trackEvent.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:00:06.85", CultureInfo.InvariantCulture).Ticks, trackEvent.EndPositionTicks);
|
||||
Assert.Equal("{\\pos(400,570)}Like an angel with pity on nobody", trackEvent.Text);
|
||||
}
|
||||
var parsed = _parser.Parse(stream, "ssa");
|
||||
Assert.Single(parsed.TrackEvents);
|
||||
var trackEvent = parsed.TrackEvents[0];
|
||||
|
||||
Assert.Equal("1", trackEvent.Id);
|
||||
Assert.Equal(TimeSpan.Parse("00:00:01.18", CultureInfo.InvariantCulture).Ticks, trackEvent.StartPositionTicks);
|
||||
Assert.Equal(TimeSpan.Parse("00:00:06.85", CultureInfo.InvariantCulture).Ticks, trackEvent.EndPositionTicks);
|
||||
Assert.Equal("{\\pos(400,570)}Like an angel with pity on nobody", trackEvent.Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -488,16 +488,16 @@ namespace Jellyfin.Model.Tests
|
|||
private static async ValueTask<T> TestData<T>(string name)
|
||||
{
|
||||
var path = Path.Join("Test Data", typeof(T).Name + "-" + name + ".json");
|
||||
using (var stream = File.OpenRead(path))
|
||||
{
|
||||
var value = await JsonSerializer.DeserializeAsync<T>(stream, JsonDefaults.Options);
|
||||
if (value is not null)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
throw new SerializationException("Invalid test data: " + name);
|
||||
using var stream = File.OpenRead(path);
|
||||
|
||||
var value = await JsonSerializer.DeserializeAsync<T>(stream, JsonDefaults.Options);
|
||||
if (value is not null)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
throw new SerializationException("Invalid test data: " + name);
|
||||
}
|
||||
|
||||
private StreamBuilder GetStreamBuilder()
|
||||
|
|
Loading…
Reference in New Issue