mirror of
https://github.com/Readarr/Readarr
synced 2026-01-01 21:24:52 +01:00
Fixed: Handle filename changes when retagging with calibre
This commit is contained in:
parent
bfb42929a2
commit
dc843ec63e
2 changed files with 34 additions and 12 deletions
|
|
@ -60,6 +60,14 @@ public CalibreProxy(IHttpClient httpClient,
|
|||
_logger = logger;
|
||||
}
|
||||
|
||||
public static string GetOriginalFormat(Dictionary<string, CalibreBookFormat> 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<string> GetAllBookFilePaths(CalibreSettings settings)
|
|||
var response = _httpClient.Get<Dictionary<int, CalibreBook>>(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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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<Edition> editions)
|
||||
|
|
@ -114,8 +113,7 @@ public void SyncTags(List<Edition> 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<RetagBookFilePreview> GetPreviews(List<BookFile> files)
|
||||
{
|
||||
var calibreFiles = files.Where(x => x.CalibreId > 0).OrderBy(x => x.Edition.Value.Title).ToList();
|
||||
|
|
|
|||
Loading…
Reference in a new issue