mirror of
https://github.com/Sonarr/Sonarr
synced 2025-01-18 21:29:46 +00:00
Include language in suffix when importing
This commit is contained in:
parent
fee8da88a6
commit
b365d8a537
4 changed files with 80 additions and 69 deletions
|
@ -1,5 +1,8 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Disk;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Configuration;
|
||||
|
@ -23,16 +26,19 @@ namespace NzbDrone.Core.Extras.Files
|
|||
|
||||
{
|
||||
private readonly IConfigService _configService;
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly IDiskTransferService _diskTransferService;
|
||||
private readonly IExtraFileService<TExtraFile> _extraFileService;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public ExtraFileManager(IConfigService configService,
|
||||
IDiskProvider diskProvider,
|
||||
IDiskTransferService diskTransferService,
|
||||
IExtraFileService<TExtraFile> extraFileService)
|
||||
Logger logger)
|
||||
{
|
||||
_configService = configService;
|
||||
_diskProvider = diskProvider;
|
||||
_diskTransferService = diskTransferService;
|
||||
_extraFileService = extraFileService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public abstract int Order { get; }
|
||||
|
@ -42,10 +48,19 @@ namespace NzbDrone.Core.Extras.Files
|
|||
public abstract IEnumerable<ExtraFile> MoveFilesAfterRename(Series series, List<EpisodeFile> episodeFiles);
|
||||
public abstract ExtraFile Import(Series series, EpisodeFile episodeFile, string path, string extension, bool readOnly);
|
||||
|
||||
protected TExtraFile ImportFile(Series series, EpisodeFile episodeFile, string path, string extension, bool readOnly)
|
||||
protected TExtraFile ImportFile(Series series, EpisodeFile episodeFile, string path, bool readOnly, string extension, string fileNameSuffix = null)
|
||||
{
|
||||
var newFileName = Path.Combine(series.Path, Path.ChangeExtension(episodeFile.RelativePath, extension));
|
||||
var newFolder = Path.GetDirectoryName(Path.Combine(series.Path, episodeFile.RelativePath));
|
||||
var filenameBuilder = new StringBuilder(Path.GetFileNameWithoutExtension(episodeFile.RelativePath));
|
||||
|
||||
if (fileNameSuffix.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
filenameBuilder.Append(fileNameSuffix);
|
||||
}
|
||||
|
||||
filenameBuilder.Append(extension);
|
||||
|
||||
var newFileName = Path.Combine(newFolder, filenameBuilder.ToString());
|
||||
var transferMode = TransferMode.Move;
|
||||
|
||||
if (readOnly)
|
||||
|
@ -61,8 +76,41 @@ namespace NzbDrone.Core.Extras.Files
|
|||
SeasonNumber = episodeFile.SeasonNumber,
|
||||
EpisodeFileId = episodeFile.Id,
|
||||
RelativePath = series.Path.GetRelativePath(newFileName),
|
||||
Extension = Path.GetExtension(path)
|
||||
Extension = extension
|
||||
};
|
||||
}
|
||||
|
||||
protected TExtraFile MoveFile(Series series, EpisodeFile episodeFile, TExtraFile extraFile, string fileNameSuffix = null)
|
||||
{
|
||||
var newFolder = Path.GetDirectoryName(Path.Combine(series.Path, episodeFile.RelativePath));
|
||||
var filenameBuilder = new StringBuilder(Path.GetFileNameWithoutExtension(episodeFile.RelativePath));
|
||||
|
||||
if (fileNameSuffix.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
filenameBuilder.Append(fileNameSuffix);
|
||||
}
|
||||
|
||||
filenameBuilder.Append(extraFile.Extension);
|
||||
|
||||
var existingFileName = Path.Combine(series.Path, extraFile.RelativePath);
|
||||
var newFileName = Path.Combine(newFolder, filenameBuilder.ToString());
|
||||
|
||||
if (newFileName.PathNotEquals(existingFileName))
|
||||
{
|
||||
try
|
||||
{
|
||||
_diskProvider.MoveFile(existingFileName, newFileName);
|
||||
extraFile.RelativePath = series.Path.GetRelativePath(newFileName);
|
||||
|
||||
return extraFile;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warn(ex, "Unable to move file after rename: {0}", existingFileName);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,15 +27,15 @@ namespace NzbDrone.Core.Extras.Metadata
|
|||
private readonly Logger _logger;
|
||||
|
||||
public MetadataService(IConfigService configService,
|
||||
IDiskProvider diskProvider,
|
||||
IDiskTransferService diskTransferService,
|
||||
IMetadataFactory metadataFactory,
|
||||
ICleanMetadataService cleanMetadataService,
|
||||
IDiskProvider diskProvider,
|
||||
IHttpClient httpClient,
|
||||
IMediaFileAttributeService mediaFileAttributeService,
|
||||
IMetadataFileService metadataFileService,
|
||||
Logger logger)
|
||||
: base(configService, diskTransferService, metadataFileService)
|
||||
: base(configService, diskProvider, diskTransferService, logger)
|
||||
{
|
||||
_metadataFactory = metadataFactory;
|
||||
_cleanMetadataService = cleanMetadataService;
|
||||
|
@ -159,7 +159,7 @@ namespace NzbDrone.Core.Extras.Metadata
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warn(ex, "Unable to move metadata file: {0}", existingFileName);
|
||||
_logger.Warn(ex, "Unable to move metadata file after rename: {0}", existingFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
|
@ -15,19 +14,15 @@ namespace NzbDrone.Core.Extras.Others
|
|||
public class OtherExtraService : ExtraFileManager<OtherExtraFile>
|
||||
{
|
||||
private readonly IOtherExtraFileService _otherExtraFileService;
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public OtherExtraService(IConfigService configService,
|
||||
IDiskProvider diskProvider,
|
||||
IDiskTransferService diskTransferService,
|
||||
IOtherExtraFileService otherExtraFileService,
|
||||
IDiskProvider diskProvider,
|
||||
Logger logger)
|
||||
: base(configService, diskTransferService, otherExtraFileService)
|
||||
: base(configService, diskProvider, diskTransferService, logger)
|
||||
{
|
||||
_otherExtraFileService = otherExtraFileService;
|
||||
_diskProvider = diskProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override int Order => 2;
|
||||
|
@ -58,23 +53,7 @@ namespace NzbDrone.Core.Extras.Others
|
|||
|
||||
foreach (var extraFile in extraFilesForEpisodeFile)
|
||||
{
|
||||
var existingFileName = Path.Combine(series.Path, extraFile.RelativePath);
|
||||
var extension = Path.GetExtension(existingFileName).TrimStart('.');
|
||||
var newFileName = Path.ChangeExtension(Path.Combine(series.Path, episodeFile.RelativePath), extension);
|
||||
|
||||
if (newFileName.PathNotEquals(existingFileName))
|
||||
{
|
||||
try
|
||||
{
|
||||
_diskProvider.MoveFile(existingFileName, newFileName);
|
||||
extraFile.RelativePath = series.Path.GetRelativePath(newFileName);
|
||||
movedFiles.Add(extraFile);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warn(ex, "Unable to move extra file: {0}", existingFileName);
|
||||
}
|
||||
}
|
||||
movedFiles.AddIfNotNull(MoveFile(series, episodeFile, extraFile));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -91,7 +70,7 @@ namespace NzbDrone.Core.Extras.Others
|
|||
extension += "-orig";
|
||||
}
|
||||
|
||||
var extraFile = ImportFile(series, episodeFile, path, extension, readOnly);
|
||||
var extraFile = ImportFile(series, episodeFile, path, readOnly, extension, null);
|
||||
|
||||
_otherExtraFileService.Upsert(extraFile);
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
@ -17,18 +16,16 @@ namespace NzbDrone.Core.Extras.Subtitles
|
|||
public class SubtitleService : ExtraFileManager<SubtitleFile>
|
||||
{
|
||||
private readonly ISubtitleFileService _subtitleFileService;
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public SubtitleService(IConfigService configService,
|
||||
IDiskProvider diskProvider,
|
||||
IDiskTransferService diskTransferService,
|
||||
ISubtitleFileService subtitleFileService,
|
||||
IDiskProvider diskProvider,
|
||||
Logger logger)
|
||||
: base(configService, diskTransferService, subtitleFileService)
|
||||
: base(configService, diskProvider, diskTransferService, logger)
|
||||
{
|
||||
_subtitleFileService = subtitleFileService;
|
||||
_diskProvider = diskProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
@ -70,25 +67,10 @@ namespace NzbDrone.Core.Extras.Subtitles
|
|||
_logger.Warn("Multiple subtitle files found with the same language and extension for {0}", Path.Combine(series.Path, episodeFile.RelativePath));
|
||||
}
|
||||
|
||||
foreach (var extraFile in group)
|
||||
foreach (var subtitleFile in group)
|
||||
{
|
||||
var existingFileName = Path.Combine(series.Path, extraFile.RelativePath);
|
||||
var extension = GetExtension(extraFile, existingFileName, copy, groupCount > 1);
|
||||
var newFileName = Path.ChangeExtension(Path.Combine(series.Path, episodeFile.RelativePath), extension);
|
||||
|
||||
if (newFileName.PathNotEquals(existingFileName))
|
||||
{
|
||||
try
|
||||
{
|
||||
_diskProvider.MoveFile(existingFileName, newFileName);
|
||||
extraFile.RelativePath = series.Path.GetRelativePath(newFileName);
|
||||
movedFiles.Add(extraFile);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warn(ex, "Unable to move subtitle file: {0}", existingFileName);
|
||||
}
|
||||
}
|
||||
var suffix = GetSuffix(subtitleFile.Language, copy, groupCount > 1);
|
||||
movedFiles.AddIfNotNull(MoveFile(series, episodeFile, subtitleFile, suffix));
|
||||
|
||||
copy++;
|
||||
}
|
||||
|
@ -104,8 +86,10 @@ namespace NzbDrone.Core.Extras.Subtitles
|
|||
{
|
||||
if (SubtitleFileExtensions.Extensions.Contains(Path.GetExtension(path)))
|
||||
{
|
||||
var subtitleFile = ImportFile(series, episodeFile, path, extension, readOnly);
|
||||
subtitleFile.Language = LanguageParser.ParseSubtitleLanguage(path);
|
||||
var language = LanguageParser.ParseSubtitleLanguage(path);
|
||||
var suffix = GetSuffix(language, 1, false);
|
||||
var subtitleFile = ImportFile(series, episodeFile, path, readOnly, extension, suffix);
|
||||
subtitleFile.Language = language;
|
||||
|
||||
_subtitleFileService.Upsert(subtitleFile);
|
||||
|
||||
|
@ -115,24 +99,24 @@ namespace NzbDrone.Core.Extras.Subtitles
|
|||
return null;
|
||||
}
|
||||
|
||||
private string GetExtension(SubtitleFile extraFile, string existingFileName, int copy, bool multipleCopies = false)
|
||||
private string GetSuffix(Language language, int copy, bool multipleCopies = false)
|
||||
{
|
||||
var fileExtension = Path.GetExtension(existingFileName);
|
||||
var extensionBuilder = new StringBuilder();
|
||||
var extensionBuilder = new StringBuilder(".");
|
||||
|
||||
if (multipleCopies)
|
||||
{
|
||||
extensionBuilder.Append(copy);
|
||||
extensionBuilder.Append(".");
|
||||
}
|
||||
|
||||
if (extraFile.Language != Language.Unknown)
|
||||
if (multipleCopies && language != Language.Unknown)
|
||||
{
|
||||
extensionBuilder.Append(IsoLanguages.Get(extraFile.Language).TwoLetterCode);
|
||||
extensionBuilder.Append(".");
|
||||
}
|
||||
|
||||
extensionBuilder.Append(fileExtension.TrimStart('.'));
|
||||
if (language != Language.Unknown)
|
||||
{
|
||||
extensionBuilder.Append(IsoLanguages.Get(language).TwoLetterCode);
|
||||
}
|
||||
|
||||
return extensionBuilder.ToString();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue