|
|
|
@ -48,9 +48,7 @@ namespace MediaBrowser.Providers.Subtitles
|
|
|
|
|
_monitor = monitor;
|
|
|
|
|
_mediaSourceManager = mediaSourceManager;
|
|
|
|
|
_localization = localizationManager;
|
|
|
|
|
_subtitleProviders = subtitleProviders
|
|
|
|
|
.OrderBy(i => i is IHasOrder hasOrder ? hasOrder.Order : 0)
|
|
|
|
|
.ToArray();
|
|
|
|
|
_subtitleProviders = [.. subtitleProviders.OrderBy(i => i is IHasOrder hasOrder ? hasOrder.Order : 0)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
@ -117,7 +115,7 @@ namespace MediaBrowser.Providers.Subtitles
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error downloading subtitles from {0}", i.Name);
|
|
|
|
|
_logger.LogError(ex, "Error downloading subtitles from {Name}", i.Name);
|
|
|
|
|
return Array.Empty<RemoteSubtitleInfo>();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
@ -205,8 +203,6 @@ namespace MediaBrowser.Providers.Subtitles
|
|
|
|
|
saveFileName += ".sdh";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveFileName += "." + response.Format.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
if (saveInMediaFolder)
|
|
|
|
|
{
|
|
|
|
|
var mediaFolderPath = Path.GetFullPath(Path.Combine(video.ContainingFolderPath, saveFileName));
|
|
|
|
@ -227,7 +223,7 @@ namespace MediaBrowser.Providers.Subtitles
|
|
|
|
|
|
|
|
|
|
if (savePaths.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
await TrySaveToFiles(memoryStream, savePaths).ConfigureAwait(false);
|
|
|
|
|
await TrySaveToFiles(memoryStream, savePaths, response.Format.ToLowerInvariant()).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
@ -236,24 +232,34 @@ namespace MediaBrowser.Providers.Subtitles
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task TrySaveToFiles(Stream stream, List<string> savePaths)
|
|
|
|
|
private async Task TrySaveToFiles(Stream stream, List<string> savePaths, string extension)
|
|
|
|
|
{
|
|
|
|
|
List<Exception>? exs = null;
|
|
|
|
|
|
|
|
|
|
foreach (var savePath in savePaths)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Saving subtitles to {SavePath}", savePath);
|
|
|
|
|
|
|
|
|
|
_monitor.ReportFileSystemChangeBeginning(savePath);
|
|
|
|
|
|
|
|
|
|
var path = savePath;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(savePath) ?? throw new InvalidOperationException("Path can't be a root directory."));
|
|
|
|
|
var fileExists = File.Exists(savePath + "." + extension);
|
|
|
|
|
var counter = 0;
|
|
|
|
|
|
|
|
|
|
while (fileExists)
|
|
|
|
|
{
|
|
|
|
|
counter++;
|
|
|
|
|
path = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}", savePath, counter, extension);
|
|
|
|
|
fileExists = File.Exists(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Saving subtitles to {SavePath}", path);
|
|
|
|
|
_monitor.ReportFileSystemChangeBeginning(path);
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path can't be a root directory."));
|
|
|
|
|
|
|
|
|
|
var fileOptions = AsyncFile.WriteOptions;
|
|
|
|
|
fileOptions.Mode = FileMode.CreateNew;
|
|
|
|
|
fileOptions.PreallocationSize = stream.Length;
|
|
|
|
|
var fs = new FileStream(savePath, fileOptions);
|
|
|
|
|
var fs = new FileStream(path, fileOptions);
|
|
|
|
|
await using (fs.ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
await stream.CopyToAsync(fs).ConfigureAwait(false);
|
|
|
|
@ -263,14 +269,11 @@ namespace MediaBrowser.Providers.Subtitles
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Bug in analyzer -- https://github.com/dotnet/roslyn-analyzers/issues/5160
|
|
|
|
|
#pragma warning disable CA1508
|
|
|
|
|
(exs ??= new List<Exception>()).Add(ex);
|
|
|
|
|
#pragma warning restore CA1508
|
|
|
|
|
(exs ??= []).Add(ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_monitor.ReportFileSystemChangeComplete(savePath, false);
|
|
|
|
|
_monitor.ReportFileSystemChangeComplete(path, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|