private void RenameFiles(List <BookFile> bookFiles, Author author) { var allFiles = _mediaFileService.GetFilesByAuthor(author.Id); var counts = allFiles.GroupBy(x => x.EditionId).ToDictionary(g => g.Key, g => g.Count()); var renamed = new List <BookFile>(); // Don't rename Calibre files foreach (var bookFile in bookFiles.Where(x => x.CalibreId == 0)) { var bookFilePath = bookFile.Path; bookFile.PartCount = counts[bookFile.EditionId]; try { _logger.Debug("Renaming book file: {0}", bookFile); _bookFileMover.MoveBookFile(bookFile, author); _mediaFileService.Update(bookFile); renamed.Add(bookFile); _logger.Debug("Renamed book file: {0}", bookFile); _eventAggregator.PublishEvent(new BookFileRenamedEvent(author, bookFile, bookFilePath)); } catch (SameFilenameException ex) { _logger.Debug("File not renamed, source and destination are the same: {0}", ex.Filename); } catch (Exception ex) { _logger.Error(ex, "Failed to rename file {0}", bookFilePath); } } if (renamed.Any()) { _eventAggregator.PublishEvent(new AuthorRenamedEvent(author)); _logger.Debug("Removing Empty Subfolders from: {0}", author.Path); _diskProvider.RemoveEmptySubfolders(author.Path); } }
private void RenameFiles(List <BookFile> bookFiles, Author author) { var renamed = new List <BookFile>(); foreach (var bookFile in bookFiles) { var bookFilePath = bookFile.Path; try { _logger.Debug("Renaming book file: {0}", bookFile); _bookFileMover.MoveBookFile(bookFile, author); _mediaFileService.Update(bookFile); renamed.Add(bookFile); _logger.Debug("Renamed book file: {0}", bookFile); _eventAggregator.PublishEvent(new BookFileRenamedEvent(author, bookFile, bookFilePath)); } catch (SameFilenameException ex) { _logger.Debug("File not renamed, source and destination are the same: {0}", ex.Filename); } catch (Exception ex) { _logger.Error(ex, "Failed to rename file {0}", bookFilePath); } } if (renamed.Any()) { _eventAggregator.PublishEvent(new AuthorRenamedEvent(author)); _logger.Debug("Removing Empty Subfolders from: {0}", author.Path); _diskProvider.RemoveEmptySubfolders(author.Path); } }
public BookFileMoveResult UpgradeBookFile(BookFile bookFile, LocalBook localBook, bool copyOnly = false) { var moveFileResult = new BookFileMoveResult(); var existingFiles = localBook.Book.BookFiles.Value; var rootFolderPath = _diskProvider.GetParentFolder(localBook.Author.Path); var rootFolder = _rootFolderService.GetBestRootFolder(rootFolderPath); var isCalibre = rootFolder.IsCalibreLibrary && rootFolder.CalibreSettings != null; var settings = rootFolder.CalibreSettings; // If there are existing book files and the root folder is missing, throw, so the old file isn't left behind during the import process. if (existingFiles.Any() && !_diskProvider.FolderExists(rootFolderPath)) { throw new RootFolderNotFoundException($"Root folder '{rootFolderPath}' was not found."); } foreach (var file in existingFiles) { var bookFilePath = file.Path; var subfolder = rootFolderPath.GetRelativePath(_diskProvider.GetParentFolder(bookFilePath)); bookFile.CalibreId = file.CalibreId; if (_diskProvider.FileExists(bookFilePath)) { _logger.Debug("Removing existing book file: {0} CalibreId: {1}", file, file.CalibreId); if (!isCalibre) { _recycleBinProvider.DeleteFile(bookFilePath, subfolder); } else { var existing = _calibre.GetBook(file.CalibreId, settings); var existingFormats = existing.Formats.Keys; _logger.Debug($"Removing existing formats {existingFormats.ConcatToString()} from calibre"); _calibre.RemoveFormats(file.CalibreId, existingFormats, settings); } } moveFileResult.OldFiles.Add(file); _mediaFileService.Delete(file, DeleteMediaFileReason.Upgrade); } if (!isCalibre) { if (copyOnly) { moveFileResult.BookFile = _bookFileMover.CopyBookFile(bookFile, localBook); } else { moveFileResult.BookFile = _bookFileMover.MoveBookFile(bookFile, localBook); } _metadataTagService.WriteTags(bookFile, true); } else { var source = bookFile.Path; moveFileResult.BookFile = _calibre.AddAndConvert(bookFile, settings); if (!copyOnly) { _diskProvider.DeleteFile(source); } } return(moveFileResult); }