New: Support for deleting books from calibre

pull/834/head
ta264 4 years ago
parent fa459ea7ac
commit b652cf9563

@ -22,6 +22,7 @@ namespace NzbDrone.Core.Books.Calibre
public interface ICalibreProxy public interface ICalibreProxy
{ {
CalibreImportJob AddBook(BookFile book, CalibreSettings settings); CalibreImportJob AddBook(BookFile book, CalibreSettings settings);
void DeleteBook(BookFile book, CalibreSettings settings);
void AddFormat(BookFile file, CalibreSettings settings); void AddFormat(BookFile file, CalibreSettings settings);
void RemoveFormats(int calibreId, IEnumerable<string> formats, CalibreSettings settings); void RemoveFormats(int calibreId, IEnumerable<string> formats, CalibreSettings settings);
void SetFields(BookFile file, CalibreSettings settings); void SetFields(BookFile file, CalibreSettings settings);
@ -87,6 +88,20 @@ 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;
}
}
public void AddFormat(BookFile file, CalibreSettings settings) public void AddFormat(BookFile file, CalibreSettings settings)
{ {
var format = Path.GetExtension(file.Path); var format = Path.GetExtension(file.Path);

@ -5,12 +5,14 @@ using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Books; using NzbDrone.Core.Books;
using NzbDrone.Core.Books.Calibre;
using NzbDrone.Core.Books.Events; using NzbDrone.Core.Books.Events;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Exceptions; using NzbDrone.Core.Exceptions;
using NzbDrone.Core.MediaFiles.Events; using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Messaging; using NzbDrone.Core.Messaging;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.RootFolders;
namespace NzbDrone.Core.MediaFiles namespace NzbDrone.Core.MediaFiles
{ {
@ -30,6 +32,8 @@ namespace NzbDrone.Core.MediaFiles
private readonly IMediaFileService _mediaFileService; private readonly IMediaFileService _mediaFileService;
private readonly IAuthorService _authorService; private readonly IAuthorService _authorService;
private readonly IConfigService _configService; private readonly IConfigService _configService;
private readonly IRootFolderService _rootFolderService;
private readonly ICalibreProxy _calibre;
private readonly Logger _logger; private readonly Logger _logger;
public MediaFileDeletionService(IDiskProvider diskProvider, public MediaFileDeletionService(IDiskProvider diskProvider,
@ -37,6 +41,8 @@ namespace NzbDrone.Core.MediaFiles
IMediaFileService mediaFileService, IMediaFileService mediaFileService,
IAuthorService authorService, IAuthorService authorService,
IConfigService configService, IConfigService configService,
IRootFolderService rootFolderService,
ICalibreProxy calibre,
Logger logger) Logger logger)
{ {
_diskProvider = diskProvider; _diskProvider = diskProvider;
@ -44,6 +50,8 @@ namespace NzbDrone.Core.MediaFiles
_mediaFileService = mediaFileService; _mediaFileService = mediaFileService;
_authorService = authorService; _authorService = authorService;
_configService = configService; _configService = configService;
_rootFolderService = rootFolderService;
_calibre = calibre;
_logger = logger; _logger = logger;
} }
@ -83,20 +91,34 @@ namespace NzbDrone.Core.MediaFiles
if (_diskProvider.FileExists(fullPath)) if (_diskProvider.FileExists(fullPath))
{ {
_logger.Info("Deleting book file: {0}", fullPath); _logger.Info("Deleting book file: {0}", fullPath);
DeleteFile(bookFile, subfolder);
}
// Delete the track file from the database to clean it up even if the file was already deleted
_mediaFileService.Delete(bookFile, DeleteMediaFileReason.Manual);
}
private void DeleteFile(BookFile bookFile, string subfolder = "")
{
var rootFolder = _rootFolderService.GetBestRootFolder(bookFile.Path);
var isCalibre = rootFolder.IsCalibreLibrary && rootFolder.CalibreSettings != null;
try try
{
if (!isCalibre)
{ {
_recycleBinProvider.DeleteFile(fullPath, subfolder); _recycleBinProvider.DeleteFile(bookFile.Path, subfolder);
} }
catch (Exception e) else
{ {
_logger.Error(e, "Unable to delete book file"); _calibre.DeleteBook(bookFile, rootFolder.CalibreSettings);
throw new NzbDroneClientException(HttpStatusCode.InternalServerError, "Unable to delete book file");
} }
} }
catch (Exception e)
// Delete the track file from the database to clean it up even if the file was already deleted {
_mediaFileService.Delete(bookFile, DeleteMediaFileReason.Manual); _logger.Error(e, "Unable to delete book file");
throw new NzbDroneClientException(HttpStatusCode.InternalServerError, "Unable to delete book file");
}
} }
public void HandleAsync(AuthorDeletedEvent message) public void HandleAsync(AuthorDeletedEvent message)
@ -140,7 +162,7 @@ namespace NzbDrone.Core.MediaFiles
var files = _mediaFileService.GetFilesByBook(message.Book.Id); var files = _mediaFileService.GetFilesByBook(message.Book.Id);
foreach (var file in files) foreach (var file in files)
{ {
_recycleBinProvider.DeleteFile(file.Path); DeleteFile(file);
} }
} }
} }

Loading…
Cancel
Save