From dc843ec63e8cdf17d39d641edea3bdb976029251 Mon Sep 17 00:00:00 2001 From: ta264 Date: Wed, 12 May 2021 21:26:52 +0100 Subject: [PATCH] Fixed: Handle filename changes when retagging with calibre --- .../Books/Calibre/CalibreProxy.cs | 13 +++++--- .../MediaFiles/EbookTagService.cs | 33 ++++++++++++++----- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/NzbDrone.Core/Books/Calibre/CalibreProxy.cs b/src/NzbDrone.Core/Books/Calibre/CalibreProxy.cs index 60dcb53b2..ff964ed52 100644 --- a/src/NzbDrone.Core/Books/Calibre/CalibreProxy.cs +++ b/src/NzbDrone.Core/Books/Calibre/CalibreProxy.cs @@ -60,6 +60,14 @@ public CalibreProxy(IHttpClient httpClient, _logger = logger; } + public static string GetOriginalFormat(Dictionary formats) + { + return formats + .Where(x => MediaFileExtensions.TextExtensions.Contains("." + x.Key)) + .OrderBy(f => f.Value.LastModified) + .FirstOrDefault().Value?.Path; + } + public CalibreImportJob AddBook(BookFile book, CalibreSettings settings) { var jobid = (int)(DateTime.UtcNow.Ticks % 1000000000); @@ -338,10 +346,7 @@ public List GetAllBookFilePaths(CalibreSettings settings) var response = _httpClient.Get>(request); foreach (var book in response.Resource.Values) { - var remotePath = book?.Formats - .Where(x => MediaFileExtensions.TextExtensions.Contains("." + x.Key)) - .OrderBy(f => f.Value.LastModified) - .FirstOrDefault().Value?.Path; + var remotePath = GetOriginalFormat(book?.Formats); if (remotePath == null) { diff --git a/src/NzbDrone.Core/MediaFiles/EbookTagService.cs b/src/NzbDrone.Core/MediaFiles/EbookTagService.cs index 6e404a2d7..69222027a 100644 --- a/src/NzbDrone.Core/MediaFiles/EbookTagService.cs +++ b/src/NzbDrone.Core/MediaFiles/EbookTagService.cs @@ -90,8 +90,7 @@ public void WriteTags(BookFile bookFile, bool newDownload, bool force = false) _logger.Debug($"Writing tags for {bookFile}"); - var rootFolder = _rootFolderService.GetBestRootFolder(bookFile.Path); - _calibre.SetFields(bookFile, rootFolder.CalibreSettings, _configService.UpdateCovers, _configService.EmbedMetadata); + WriteTagsInternal(bookFile, _configService.UpdateCovers, _configService.EmbedMetadata); } public void SyncTags(List editions) @@ -114,8 +113,7 @@ public void SyncTags(List editions) // not all of the updates will have been committed to the database yet file.Edition = edition; - var rootFolder = _rootFolderService.GetBestRootFolder(file.Path); - _calibre.SetFields(file, rootFolder.CalibreSettings, _configService.UpdateCovers, _configService.EmbedMetadata); + WriteTagsInternal(file, _configService.UpdateCovers, _configService.EmbedMetadata); } } } @@ -143,8 +141,7 @@ public void Execute(RetagFilesCommand message) foreach (var file in files.Where(x => x.CalibreId != 0)) { - var rootFolder = _rootFolderService.GetBestRootFolder(file.Path); - _calibre.SetFields(file, rootFolder.CalibreSettings, message.UpdateCovers, message.EmbedMetadata); + WriteTagsInternal(file, message.UpdateCovers, message.EmbedMetadata); } _logger.ProgressInfo("Selected files re-tagged for {0}", author.Name); @@ -163,14 +160,34 @@ public void Execute(RetagAuthorCommand message) foreach (var file in files.Where(x => x.CalibreId != 0)) { - var rootFolder = _rootFolderService.GetBestRootFolder(file.Path); - _calibre.SetFields(file, rootFolder.CalibreSettings, message.UpdateCovers, message.EmbedMetadata); + WriteTagsInternal(file, message.UpdateCovers, message.EmbedMetadata); } _logger.ProgressInfo("All files re-tagged for {0}", author.Name); } } + private void WriteTagsInternal(BookFile file, bool updateCover, bool embedMetadata) + { + var rootFolder = _rootFolderService.GetBestRootFolder(file.Path); + _calibre.SetFields(file, rootFolder.CalibreSettings, updateCover, embedMetadata); + + // updating the calibre metadata may have renamed the file, so track that + var updated = _calibre.GetBook(file.CalibreId, rootFolder.CalibreSettings); + + var updatedPath = CalibreProxy.GetOriginalFormat(updated.Formats); + + if (updatedPath != file.Path) + { + file.Path = updatedPath; + + if (file.Id > 0) + { + _mediaFileService.Update(file); + } + } + } + private IEnumerable GetPreviews(List files) { var calibreFiles = files.Where(x => x.CalibreId > 0).OrderBy(x => x.Edition.Value.Title).ToList();