Fixed: Deleting author removes books from Calibre

Fixes #1144
pull/1145/head
ta264 3 years ago
parent ca7ba125d2
commit b3dd116d27

@ -23,6 +23,7 @@ namespace NzbDrone.Core.Books.Calibre
{
CalibreImportJob AddBook(BookFile book, CalibreSettings settings);
void DeleteBook(BookFile book, CalibreSettings settings);
void DeleteBooks(List<BookFile> books, CalibreSettings settings);
void AddFormat(BookFile file, CalibreSettings settings);
void RemoveFormats(int calibreId, IEnumerable<string> formats, CalibreSettings settings);
void SetFields(BookFile file, CalibreSettings settings, bool updateCover = true, bool embed = false);
@ -102,16 +103,15 @@ namespace NzbDrone.Core.Books.Calibre
public void DeleteBook(BookFile book, CalibreSettings settings)
{
try
{
var request = GetBuilder($"cdb/delete-books/{book.CalibreId}/{settings.Library}", settings).Build();
_httpClient.Post(request);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
var request = GetBuilder($"cdb/delete-books/{book.CalibreId}/{settings.Library}", settings).Build();
_httpClient.Post(request);
}
public void DeleteBooks(List<BookFile> books, CalibreSettings settings)
{
var idString = books.Where(x => x.CalibreId != 0).Select(x => x.CalibreId).ConcatToString(",");
var request = GetBuilder($"cdb/delete-books/{idString}/{settings.Library}", settings).Build();
_httpClient.Post(request);
}
public void AddFormat(BookFile file, CalibreSettings settings)

@ -23,6 +23,7 @@ namespace NzbDrone.Core.MediaFiles
}
public class MediaFileDeletionService : IDeleteMediaFiles,
IHandle<AuthorDeletedEvent>,
IHandleAsync<AuthorDeletedEvent>,
IHandleAsync<BookDeletedEvent>,
IHandle<BookFileDeletedEvent>
@ -121,37 +122,63 @@ namespace NzbDrone.Core.MediaFiles
}
}
[EventHandleOrder(EventHandleOrder.First)]
public void Handle(AuthorDeletedEvent message)
{
if (message.DeleteFiles)
{
var author = message.Author;
var rootFolder = _rootFolderService.GetBestRootFolder(message.Author.Path);
var isCalibre = rootFolder.IsCalibreLibrary && rootFolder.CalibreSettings != null;
if (isCalibre)
{
// use metadataId instead of authorId so that query works even after author deleted
var books = _mediaFileService.GetFilesByAuthorMetadataId(author.AuthorMetadataId);
_calibre.DeleteBooks(books, rootFolder.CalibreSettings);
}
}
}
public void HandleAsync(AuthorDeletedEvent message)
{
if (message.DeleteFiles)
{
var author = message.Author;
var allAuthors = _authorService.AllAuthorPaths();
foreach (var s in allAuthors)
var rootFolder = _rootFolderService.GetBestRootFolder(message.Author.Path);
var isCalibre = rootFolder.IsCalibreLibrary && rootFolder.CalibreSettings != null;
if (!isCalibre)
{
if (s.Key == author.Id)
{
continue;
}
var allAuthors = _authorService.AllAuthorPaths();
if (author.Path.IsParentPath(s.Value))
foreach (var s in allAuthors)
{
_logger.Error("Author path: '{0}' is a parent of another author, not deleting files.", author.Path);
return;
if (s.Key == author.Id)
{
continue;
}
if (author.Path.IsParentPath(s.Value))
{
_logger.Error("Author path: '{0}' is a parent of another author, not deleting files.", author.Path);
return;
}
if (author.Path.PathEquals(s.Value))
{
_logger.Error("Author path: '{0}' is the same as another author, not deleting files.", author.Path);
return;
}
}
if (author.Path.PathEquals(s.Value))
if (_diskProvider.FolderExists(message.Author.Path))
{
_logger.Error("Author path: '{0}' is the same as another author, not deleting files.", author.Path);
return;
_recycleBinProvider.DeleteFolder(message.Author.Path);
}
}
if (_diskProvider.FolderExists(message.Author.Path))
{
_recycleBinProvider.DeleteFolder(message.Author.Path);
}
}
}

@ -12,6 +12,7 @@ namespace NzbDrone.Core.MediaFiles
public interface IMediaFileRepository : IBasicRepository<BookFile>
{
List<BookFile> GetFilesByAuthor(int authorId);
List<BookFile> GetFilesByAuthorMetadataId(int authorMetadataId);
List<BookFile> GetFilesByBook(int bookId);
List<BookFile> GetFilesByEdition(int editionId);
List<BookFile> GetUnmappedFiles();
@ -68,6 +69,11 @@ namespace NzbDrone.Core.MediaFiles
return Query(Builder().Where<Author>(a => a.Id == authorId));
}
public List<BookFile> GetFilesByAuthorMetadataId(int authorMetadataId)
{
return Query(Builder().Where<Book>(b => b.AuthorMetadataId == authorMetadataId));
}
public List<BookFile> GetFilesByBook(int bookId)
{
return Query(Builder().Where<Book>(b => b.Id == bookId));

@ -22,6 +22,7 @@ namespace NzbDrone.Core.MediaFiles
void Delete(BookFile bookFile, DeleteMediaFileReason reason);
void DeleteMany(List<BookFile> bookFiles, DeleteMediaFileReason reason);
List<BookFile> GetFilesByAuthor(int authorId);
List<BookFile> GetFilesByAuthorMetadataId(int authorMetadataId);
List<BookFile> GetFilesByBook(int bookId);
List<BookFile> GetFilesByEdition(int editionId);
List<BookFile> GetUnmappedFiles();
@ -182,6 +183,11 @@ namespace NzbDrone.Core.MediaFiles
return _mediaFileRepository.GetFilesByAuthor(authorId);
}
public List<BookFile> GetFilesByAuthorMetadataId(int authorMetadataId)
{
return _mediaFileRepository.GetFilesByAuthorMetadataId(authorMetadataId);
}
public List<BookFile> GetFilesByBook(int bookId)
{
return _mediaFileRepository.GetFilesByBook(bookId);

Loading…
Cancel
Save