Fixed: Deleting author removes books from Calibre

Fixes #1144
This commit is contained in:
ta264 2021-07-20 21:41:57 +01:00
parent ca7ba125d2
commit b3dd116d27
4 changed files with 67 additions and 28 deletions

View file

@ -23,6 +23,7 @@ public interface ICalibreProxy
{
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 @@ public CalibreImportJob AddBook(BookFile book, CalibreSettings settings)
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)

View file

@ -23,6 +23,7 @@ public interface IDeleteMediaFiles
}
public class MediaFileDeletionService : IDeleteMediaFiles,
IHandle<AuthorDeletedEvent>,
IHandleAsync<AuthorDeletedEvent>,
IHandleAsync<BookDeletedEvent>,
IHandle<BookFileDeletedEvent>
@ -121,36 +122,62 @@ private void DeleteFile(BookFile bookFile, string subfolder = "")
}
}
[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)
var allAuthors = _authorService.AllAuthorPaths();
foreach (var s in allAuthors)
{
continue;
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.IsParentPath(s.Value))
if (_diskProvider.FolderExists(message.Author.Path))
{
_logger.Error("Author path: '{0}' is a parent of another author, not deleting files.", author.Path);
return;
_recycleBinProvider.DeleteFolder(message.Author.Path);
}
if (author.Path.PathEquals(s.Value))
{
_logger.Error("Author path: '{0}' is the same as another author, not deleting files.", author.Path);
return;
}
}
if (_diskProvider.FolderExists(message.Author.Path))
{
_recycleBinProvider.DeleteFolder(message.Author.Path);
}
}
}

View file

@ -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 @@ public List<BookFile> GetFilesByAuthor(int authorId)
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));

View file

@ -22,6 +22,7 @@ public interface IMediaFileService
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 @@ public List<BookFile> GetFilesByAuthor(int authorId)
return _mediaFileRepository.GetFilesByAuthor(authorId);
}
public List<BookFile> GetFilesByAuthorMetadataId(int authorMetadataId)
{
return _mediaFileRepository.GetFilesByAuthorMetadataId(authorMetadataId);
}
public List<BookFile> GetFilesByBook(int bookId)
{
return _mediaFileRepository.GetFilesByBook(bookId);