Fixed: Correctly delete files on Album Delete

Fixes #2551
This commit is contained in:
Qstick 2023-01-15 00:09:03 -06:00
parent 71c1edd47c
commit b230faaa34
3 changed files with 34 additions and 3 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using NLog;
using NzbDrone.Common.Disk;
@ -147,13 +148,28 @@ namespace NzbDrone.Core.MediaFiles
{
if (message.DeleteFiles)
{
var files = _mediaFileService.GetFilesByAlbum(message.Album.Id);
var files = message.TrackFilesToDelete;
foreach (var file in files)
{
_recycleBinProvider.DeleteFile(file.Path);
}
if (_configService.DeleteEmptyFolders)
{
var artist = message.Album.Artist.Value;
var albumFolder = message.TrackFilesToDelete.FirstOrDefault()?.Path.GetParentPath();
if (_diskProvider.GetFiles(artist.Path, SearchOption.AllDirectories).Empty())
{
_diskProvider.DeleteFolder(artist.Path, true);
}
else if (_diskProvider.GetFiles(albumFolder, SearchOption.AllDirectories).Empty())
{
_diskProvider.RemoveEmptySubfolders(albumFolder);
}
}
_eventAggregator.PublishEvent(new DeleteCompletedEvent());
}
}

View File

@ -1,4 +1,6 @@
using NzbDrone.Common.Messaging;
using System.Collections.Generic;
using NzbDrone.Common.Messaging;
using NzbDrone.Core.MediaFiles;
namespace NzbDrone.Core.Music.Events
{
@ -7,6 +9,7 @@ namespace NzbDrone.Core.Music.Events
public Album Album { get; private set; }
public bool DeleteFiles { get; private set; }
public bool AddImportListExclusion { get; private set; }
public List<TrackFile> TrackFilesToDelete { get; set; }
public AlbumDeletedEvent(Album album, bool deleteFiles, bool addImportListExclusion)
{

View File

@ -4,6 +4,7 @@ using System.Linq;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Music.Events;
using NzbDrone.Core.Parser;
@ -46,14 +47,17 @@ namespace NzbDrone.Core.Music
{
private readonly IAlbumRepository _albumRepository;
private readonly IEventAggregator _eventAggregator;
private readonly IMediaFileService _mediaFileService;
private readonly Logger _logger;
public AlbumService(IAlbumRepository albumRepository,
IEventAggregator eventAggregator,
IMediaFileService mediaFileService,
Logger logger)
{
_albumRepository = albumRepository;
_eventAggregator = eventAggregator;
_mediaFileService = mediaFileService;
_logger = logger;
}
@ -71,7 +75,15 @@ namespace NzbDrone.Core.Music
var album = _albumRepository.Get(albumId);
album.Artist.LazyLoad();
_albumRepository.Delete(albumId);
_eventAggregator.PublishEvent(new AlbumDeletedEvent(album, deleteFiles, addImportListExclusion));
var deleteEvent = new AlbumDeletedEvent(album, deleteFiles, addImportListExclusion);
if (deleteFiles)
{
deleteEvent.TrackFilesToDelete = _mediaFileService.GetFilesByAlbum(albumId);
}
_eventAggregator.PublishEvent(deleteEvent);
}
public Album FindById(string foreignId)