Merge pull request #5307 from barronpm/using-declarations

This commit is contained in:
Bond-009 2021-05-16 09:58:10 +00:00 committed by GitHub
commit f06f3450c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 158 additions and 192 deletions

View File

@ -69,58 +69,52 @@ namespace MediaBrowser.Providers.Music
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(Stream stream)
{
using (var oReader = new StreamReader(stream, Encoding.UTF8))
using var oReader = new StreamReader(stream, Encoding.UTF8);
var settings = new XmlReaderSettings()
{
var settings = new XmlReaderSettings()
{
ValidationType = ValidationType.None,
CheckCharacters = false,
IgnoreProcessingInstructions = true,
IgnoreComments = true
};
ValidationType = ValidationType.None,
CheckCharacters = false,
IgnoreProcessingInstructions = true,
IgnoreComments = true
};
using (var reader = XmlReader.Create(oReader, settings))
{
reader.MoveToContent();
reader.Read();
using var reader = XmlReader.Create(oReader, settings);
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
if (reader.NodeType == XmlNodeType.Element)
case "artist-list":
{
switch (reader.Name)
if (reader.IsEmptyElement)
{
case "artist-list":
{
if (reader.IsEmptyElement)
{
reader.Read();
continue;
}
using (var subReader = reader.ReadSubtree())
{
return ParseArtistList(subReader).ToList();
}
}
default:
{
reader.Skip();
break;
}
reader.Read();
continue;
}
using var subReader = reader.ReadSubtree();
return ParseArtistList(subReader).ToList();
}
else
default:
{
reader.Read();
reader.Skip();
break;
}
}
return Enumerable.Empty<RemoteSearchResult>();
}
else
{
reader.Read();
}
}
return Enumerable.Empty<RemoteSearchResult>();
}
private IEnumerable<RemoteSearchResult> ParseArtistList(XmlReader reader)
@ -145,13 +139,11 @@ namespace MediaBrowser.Providers.Music
var mbzId = reader.GetAttribute("id");
using (var subReader = reader.ReadSubtree())
using var subReader = reader.ReadSubtree();
var artist = ParseArtist(subReader, mbzId);
if (artist != null)
{
var artist = ParseArtist(subReader, mbzId);
if (artist != null)
{
yield return artist;
}
yield return artist;
}
break;

View File

@ -128,53 +128,49 @@ namespace MediaBrowser.Providers.Music
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(Stream stream)
{
using (var oReader = new StreamReader(stream, Encoding.UTF8))
using var oReader = new StreamReader(stream, Encoding.UTF8);
var settings = new XmlReaderSettings()
{
var settings = new XmlReaderSettings()
ValidationType = ValidationType.None,
CheckCharacters = false,
IgnoreProcessingInstructions = true,
IgnoreComments = true
};
using var reader = XmlReader.Create(oReader, settings);
var results = ReleaseResult.Parse(reader);
return results.Select(i =>
{
var result = new RemoteSearchResult
{
ValidationType = ValidationType.None,
CheckCharacters = false,
IgnoreProcessingInstructions = true,
IgnoreComments = true
Name = i.Title,
ProductionYear = i.Year
};
using (var reader = XmlReader.Create(oReader, settings))
if (i.Artists.Count > 0)
{
var results = ReleaseResult.Parse(reader);
return results.Select(i =>
result.AlbumArtist = new RemoteSearchResult
{
var result = new RemoteSearchResult
{
Name = i.Title,
ProductionYear = i.Year
};
SearchProviderName = Name,
Name = i.Artists[0].Item1
};
if (i.Artists.Count > 0)
{
result.AlbumArtist = new RemoteSearchResult
{
SearchProviderName = Name,
Name = i.Artists[0].Item1
};
result.AlbumArtist.SetProviderId(MetadataProvider.MusicBrainzArtist, i.Artists[0].Item2);
}
if (!string.IsNullOrWhiteSpace(i.ReleaseId))
{
result.SetProviderId(MetadataProvider.MusicBrainzAlbum, i.ReleaseId);
}
if (!string.IsNullOrWhiteSpace(i.ReleaseGroupId))
{
result.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, i.ReleaseGroupId);
}
return result;
});
result.AlbumArtist.SetProviderId(MetadataProvider.MusicBrainzArtist, i.Artists[0].Item2);
}
}
if (!string.IsNullOrWhiteSpace(i.ReleaseId))
{
result.SetProviderId(MetadataProvider.MusicBrainzAlbum, i.ReleaseId);
}
if (!string.IsNullOrWhiteSpace(i.ReleaseGroupId))
{
result.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, i.ReleaseGroupId);
}
return result;
});
}
/// <inheritdoc />
@ -339,10 +335,8 @@ namespace MediaBrowser.Providers.Music
continue;
}
using (var subReader = reader.ReadSubtree())
{
return ParseReleaseList(subReader).ToList();
}
using var subReader = reader.ReadSubtree();
return ParseReleaseList(subReader).ToList();
}
default:
@ -383,13 +377,11 @@ namespace MediaBrowser.Providers.Music
var releaseId = reader.GetAttribute("id");
using (var subReader = reader.ReadSubtree())
using var subReader = reader.ReadSubtree();
var release = ParseRelease(subReader, releaseId);
if (release != null)
{
var release = ParseRelease(subReader, releaseId);
if (release != null)
{
yield return release;
}
yield return release;
}
break;
@ -460,14 +452,12 @@ namespace MediaBrowser.Providers.Music
case "artist-credit":
{
using (var subReader = reader.ReadSubtree())
{
var artist = ParseArtistCredit(subReader);
using var subReader = reader.ReadSubtree();
var artist = ParseArtistCredit(subReader);
if (!string.IsNullOrEmpty(artist.Item1))
{
result.Artists.Add(artist);
}
if (!string.IsNullOrEmpty(artist.Item1))
{
result.Artists.Add(artist);
}
break;
@ -505,12 +495,10 @@ namespace MediaBrowser.Providers.Music
switch (reader.Name)
{
case "name-credit":
{
using (var subReader = reader.ReadSubtree())
{
return ParseArtistNameCredit(subReader);
}
}
{
using var subReader = reader.ReadSubtree();
return ParseArtistNameCredit(subReader);
}
default:
{
@ -545,10 +533,8 @@ namespace MediaBrowser.Providers.Music
case "artist":
{
var id = reader.GetAttribute("id");
using (var subReader = reader.ReadSubtree())
{
return ParseArtistArtistCredit(subReader, id);
}
using var subReader = reader.ReadSubtree();
return ParseArtistArtistCredit(subReader, id);
}
default:
@ -647,47 +633,43 @@ namespace MediaBrowser.Providers.Music
IgnoreComments = true
};
using (var reader = XmlReader.Create(oReader, settings))
using var reader = XmlReader.Create(oReader, settings);
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.NodeType == XmlNodeType.Element)
switch (reader.Name)
{
switch (reader.Name)
case "release-group-list":
{
case "release-group-list":
if (reader.IsEmptyElement)
{
if (reader.IsEmptyElement)
{
reader.Read();
continue;
}
using (var subReader = reader.ReadSubtree())
{
return GetFirstReleaseGroupId(subReader);
}
reader.Read();
continue;
}
default:
{
reader.Skip();
break;
}
using var subReader = reader.ReadSubtree();
return GetFirstReleaseGroupId(subReader);
}
default:
{
reader.Skip();
break;
}
}
else
{
reader.Read();
}
}
return null;
else
{
reader.Read();
}
}
return null;
}
private string GetFirstReleaseGroupId(XmlReader reader)

View File

@ -172,23 +172,19 @@ namespace MediaBrowser.Providers.Studios
public IEnumerable<string> GetAvailableImages(string file)
{
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
using var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
using var reader = new StreamReader(fileStream);
var lines = new List<string>();
foreach (var line in reader.ReadAllLines())
{
using (var reader = new StreamReader(fileStream))
if (!string.IsNullOrWhiteSpace(line))
{
var lines = new List<string>();
foreach (var line in reader.ReadAllLines())
{
if (!string.IsNullOrWhiteSpace(line))
{
lines.Add(line);
}
}
return lines;
lines.Add(line);
}
}
return lines;
}
}
}

View File

@ -187,48 +187,46 @@ namespace MediaBrowser.Providers.Subtitles
{
var saveInMediaFolder = libraryOptions.SaveSubtitlesWithMedia;
using (var stream = response.Stream)
using (var memoryStream = new MemoryStream())
using var stream = response.Stream;
using var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0;
var savePaths = new List<string>();
var saveFileName = Path.GetFileNameWithoutExtension(video.Path) + "." + response.Language.ToLowerInvariant();
if (response.IsForced)
{
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0;
saveFileName += ".forced";
}
var savePaths = new List<string>();
var saveFileName = Path.GetFileNameWithoutExtension(video.Path) + "." + response.Language.ToLowerInvariant();
saveFileName += "." + response.Format.ToLowerInvariant();
if (response.IsForced)
if (saveInMediaFolder)
{
var mediaFolderPath = Path.GetFullPath(Path.Combine(video.ContainingFolderPath, saveFileName));
// TODO: Add some error handling to the API user: return BadRequest("Could not save subtitle, bad path.");
if (mediaFolderPath.StartsWith(video.ContainingFolderPath, StringComparison.Ordinal))
{
saveFileName += ".forced";
savePaths.Add(mediaFolderPath);
}
}
saveFileName += "." + response.Format.ToLowerInvariant();
var internalPath = Path.GetFullPath(Path.Combine(video.GetInternalMetadataPath(), saveFileName));
if (saveInMediaFolder)
{
var mediaFolderPath = Path.GetFullPath(Path.Combine(video.ContainingFolderPath, saveFileName));
// TODO: Add some error handling to the API user: return BadRequest("Could not save subtitle, bad path.");
if (mediaFolderPath.StartsWith(video.ContainingFolderPath, StringComparison.Ordinal))
{
savePaths.Add(mediaFolderPath);
}
}
// TODO: Add some error to the user: return BadRequest("Could not save subtitle, bad path.");
if (internalPath.StartsWith(video.GetInternalMetadataPath(), StringComparison.Ordinal))
{
savePaths.Add(internalPath);
}
var internalPath = Path.GetFullPath(Path.Combine(video.GetInternalMetadataPath(), saveFileName));
// TODO: Add some error to the user: return BadRequest("Could not save subtitle, bad path.");
if (internalPath.StartsWith(video.GetInternalMetadataPath(), StringComparison.Ordinal))
{
savePaths.Add(internalPath);
}
if (savePaths.Count > 0)
{
await TrySaveToFiles(memoryStream, savePaths).ConfigureAwait(false);
}
else
{
_logger.LogError("An uploaded subtitle could not be saved because the resulting paths were invalid.");
}
if (savePaths.Count > 0)
{
await TrySaveToFiles(memoryStream, savePaths).ConfigureAwait(false);
}
else
{
_logger.LogError("An uploaded subtitle could not be saved because the resulting paths were invalid.");
}
}
@ -247,10 +245,8 @@ namespace MediaBrowser.Providers.Subtitles
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
// use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
using (var fs = new FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.None, FileStreamBufferSize, true))
{
await stream.CopyToAsync(fs).ConfigureAwait(false);
}
using var fs = new FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.None, FileStreamBufferSize, true);
await stream.CopyToAsync(fs).ConfigureAwait(false);
return;
}