mirror of
https://github.com/Readarr/Readarr
synced 2025-12-30 20:25:18 +01:00
New: Support for deleting books from calibre
This commit is contained in:
parent
fa459ea7ac
commit
b652cf9563
2 changed files with 48 additions and 11 deletions
|
|
@ -22,6 +22,7 @@ namespace NzbDrone.Core.Books.Calibre
|
|||
public interface ICalibreProxy
|
||||
{
|
||||
CalibreImportJob AddBook(BookFile book, CalibreSettings settings);
|
||||
void DeleteBook(BookFile book, CalibreSettings settings);
|
||||
void AddFormat(BookFile file, CalibreSettings settings);
|
||||
void RemoveFormats(int calibreId, IEnumerable<string> formats, CalibreSettings settings);
|
||||
void SetFields(BookFile file, CalibreSettings settings);
|
||||
|
|
@ -87,6 +88,20 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddFormat(BookFile file, CalibreSettings settings)
|
||||
{
|
||||
var format = Path.GetExtension(file.Path);
|
||||
|
|
|
|||
|
|
@ -5,12 +5,14 @@
|
|||
using NzbDrone.Common.Disk;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Books;
|
||||
using NzbDrone.Core.Books.Calibre;
|
||||
using NzbDrone.Core.Books.Events;
|
||||
using NzbDrone.Core.Configuration;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
using NzbDrone.Core.MediaFiles.Events;
|
||||
using NzbDrone.Core.Messaging;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.RootFolders;
|
||||
|
||||
namespace NzbDrone.Core.MediaFiles
|
||||
{
|
||||
|
|
@ -30,6 +32,8 @@ public class MediaFileDeletionService : IDeleteMediaFiles,
|
|||
private readonly IMediaFileService _mediaFileService;
|
||||
private readonly IAuthorService _authorService;
|
||||
private readonly IConfigService _configService;
|
||||
private readonly IRootFolderService _rootFolderService;
|
||||
private readonly ICalibreProxy _calibre;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public MediaFileDeletionService(IDiskProvider diskProvider,
|
||||
|
|
@ -37,6 +41,8 @@ public MediaFileDeletionService(IDiskProvider diskProvider,
|
|||
IMediaFileService mediaFileService,
|
||||
IAuthorService authorService,
|
||||
IConfigService configService,
|
||||
IRootFolderService rootFolderService,
|
||||
ICalibreProxy calibre,
|
||||
Logger logger)
|
||||
{
|
||||
_diskProvider = diskProvider;
|
||||
|
|
@ -44,6 +50,8 @@ public MediaFileDeletionService(IDiskProvider diskProvider,
|
|||
_mediaFileService = mediaFileService;
|
||||
_authorService = authorService;
|
||||
_configService = configService;
|
||||
_rootFolderService = rootFolderService;
|
||||
_calibre = calibre;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
|
@ -83,22 +91,36 @@ public void DeleteTrackFile(BookFile bookFile, string subfolder = "")
|
|||
if (_diskProvider.FileExists(fullPath))
|
||||
{
|
||||
_logger.Info("Deleting book file: {0}", fullPath);
|
||||
|
||||
try
|
||||
{
|
||||
_recycleBinProvider.DeleteFile(fullPath, subfolder);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Unable to delete book file");
|
||||
throw new NzbDroneClientException(HttpStatusCode.InternalServerError, "Unable to delete book file");
|
||||
}
|
||||
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
|
||||
{
|
||||
if (!isCalibre)
|
||||
{
|
||||
_recycleBinProvider.DeleteFile(bookFile.Path, subfolder);
|
||||
}
|
||||
else
|
||||
{
|
||||
_calibre.DeleteBook(bookFile, rootFolder.CalibreSettings);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Unable to delete book file");
|
||||
throw new NzbDroneClientException(HttpStatusCode.InternalServerError, "Unable to delete book file");
|
||||
}
|
||||
}
|
||||
|
||||
public void HandleAsync(AuthorDeletedEvent message)
|
||||
{
|
||||
if (message.DeleteFiles)
|
||||
|
|
@ -140,7 +162,7 @@ public void HandleAsync(BookDeletedEvent message)
|
|||
var files = _mediaFileService.GetFilesByBook(message.Book.Id);
|
||||
foreach (var file in files)
|
||||
{
|
||||
_recycleBinProvider.DeleteFile(file.Path);
|
||||
DeleteFile(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue