2017-09-04 02:20:56 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-01-03 12:49:24 +00:00
|
|
|
using FluentValidation;
|
2021-08-04 20:42:40 +00:00
|
|
|
using Lidarr.Http;
|
|
|
|
using Lidarr.Http.REST.Attributes;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
using NzbDrone.Core.ArtistStats;
|
2018-01-18 02:28:47 +00:00
|
|
|
using NzbDrone.Core.Datastore.Events;
|
2019-02-23 22:39:11 +00:00
|
|
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
2019-02-16 14:49:24 +00:00
|
|
|
using NzbDrone.Core.Download;
|
2019-05-07 23:10:09 +00:00
|
|
|
using NzbDrone.Core.MediaCover;
|
2019-10-29 21:27:27 +00:00
|
|
|
using NzbDrone.Core.MediaFiles;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Music;
|
|
|
|
using NzbDrone.Core.Music.Events;
|
2019-12-16 21:21:32 +00:00
|
|
|
using NzbDrone.Core.Validation;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.Validation.Paths;
|
|
|
|
using NzbDrone.SignalR;
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-10-31 01:28:29 +00:00
|
|
|
namespace Lidarr.Api.V1.Albums
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2021-08-04 20:42:40 +00:00
|
|
|
[V1ApiController]
|
|
|
|
public class AlbumController : AlbumControllerWithSignalR,
|
2019-02-16 14:49:24 +00:00
|
|
|
IHandle<AlbumGrabbedEvent>,
|
|
|
|
IHandle<AlbumEditedEvent>,
|
2019-12-16 21:21:32 +00:00
|
|
|
IHandle<AlbumUpdatedEvent>,
|
2019-02-16 14:49:24 +00:00
|
|
|
IHandle<AlbumImportedEvent>,
|
2019-10-29 21:27:27 +00:00
|
|
|
IHandle<TrackImportedEvent>,
|
|
|
|
IHandle<TrackFileDeletedEvent>
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2020-08-26 21:03:59 +00:00
|
|
|
protected readonly IArtistService _artistService;
|
2018-12-15 00:02:43 +00:00
|
|
|
protected readonly IReleaseService _releaseService;
|
2019-12-16 21:21:32 +00:00
|
|
|
protected readonly IAddAlbumService _addAlbumService;
|
2019-05-07 23:10:09 +00:00
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
public AlbumController(IArtistService artistService,
|
2020-08-26 21:03:59 +00:00
|
|
|
IAlbumService albumService,
|
2019-12-16 21:21:32 +00:00
|
|
|
IAddAlbumService addAlbumService,
|
2018-12-15 00:02:43 +00:00
|
|
|
IReleaseService releaseService,
|
|
|
|
IArtistStatisticsService artistStatisticsService,
|
2019-05-07 23:10:09 +00:00
|
|
|
IMapCoversToLocal coverMapper,
|
2018-12-15 00:02:43 +00:00
|
|
|
IUpgradableSpecification upgradableSpecification,
|
2019-12-16 21:21:32 +00:00
|
|
|
IBroadcastSignalRMessage signalRBroadcaster,
|
2024-09-22 01:38:55 +00:00
|
|
|
RootFolderValidator rootFolderValidator,
|
|
|
|
MappedNetworkDriveValidator mappedNetworkDriveValidator,
|
|
|
|
ArtistAncestorValidator artistAncestorValidator,
|
|
|
|
RecycleBinValidator recycleBinValidator,
|
|
|
|
SystemFolderValidator systemFolderValidator,
|
|
|
|
AlbumExistsValidator albumExistsValidator,
|
|
|
|
RootFolderExistsValidator rootFolderExistsValidator,
|
2020-02-09 19:15:43 +00:00
|
|
|
QualityProfileExistsValidator qualityProfileExistsValidator,
|
2019-12-16 21:21:32 +00:00
|
|
|
MetadataProfileExistsValidator metadataProfileExistsValidator)
|
|
|
|
|
2019-05-07 23:10:09 +00:00
|
|
|
: base(albumService, artistStatisticsService, coverMapper, upgradableSpecification, signalRBroadcaster)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2020-08-26 21:03:59 +00:00
|
|
|
_artistService = artistService;
|
2018-12-15 00:02:43 +00:00
|
|
|
_releaseService = releaseService;
|
2019-12-16 21:21:32 +00:00
|
|
|
_addAlbumService = addAlbumService;
|
|
|
|
|
2024-09-22 01:38:55 +00:00
|
|
|
PostValidator.RuleFor(s => s.ForeignAlbumId).NotEmpty().SetValidator(albumExistsValidator);
|
|
|
|
PostValidator.RuleFor(s => s.Artist).NotNull();
|
|
|
|
PostValidator.RuleFor(s => s.Artist.ForeignArtistId).NotEmpty().When(s => s.Artist != null);
|
|
|
|
|
|
|
|
PostValidator.RuleFor(s => s.Artist.QualityProfileId).Cascade(CascadeMode.Stop)
|
|
|
|
.ValidId()
|
|
|
|
.SetValidator(qualityProfileExistsValidator)
|
|
|
|
.When(s => s.Artist != null);
|
|
|
|
|
|
|
|
PostValidator.RuleFor(s => s.Artist.MetadataProfileId).Cascade(CascadeMode.Stop)
|
|
|
|
.ValidId()
|
|
|
|
.SetValidator(metadataProfileExistsValidator)
|
|
|
|
.When(s => s.Artist != null);
|
|
|
|
|
|
|
|
PostValidator.RuleFor(s => s.Artist.Path).Cascade(CascadeMode.Stop)
|
|
|
|
.IsValidPath()
|
|
|
|
.SetValidator(rootFolderValidator)
|
|
|
|
.SetValidator(mappedNetworkDriveValidator)
|
|
|
|
.SetValidator(artistAncestorValidator)
|
|
|
|
.SetValidator(recycleBinValidator)
|
|
|
|
.SetValidator(systemFolderValidator)
|
|
|
|
.When(s => s.Artist != null && s.Artist.Path.IsNotNullOrWhiteSpace());
|
|
|
|
|
|
|
|
PostValidator.RuleFor(s => s.Artist.RootFolderPath)
|
|
|
|
.IsValidPath()
|
|
|
|
.SetValidator(rootFolderExistsValidator)
|
|
|
|
.When(s => s.Artist != null && s.Artist.Path.IsNullOrWhiteSpace());
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[HttpGet]
|
|
|
|
public List<AlbumResource> GetAlbums([FromQuery]int? artistId,
|
|
|
|
[FromQuery] List<int> albumIds,
|
|
|
|
[FromQuery]string foreignAlbumId,
|
|
|
|
[FromQuery]bool includeAllArtistAlbums = false)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2021-08-04 20:42:40 +00:00
|
|
|
if (!artistId.HasValue && !albumIds.Any() && foreignAlbumId.IsNullOrWhiteSpace())
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2020-08-26 21:03:59 +00:00
|
|
|
var albums = _albumService.GetAllAlbums();
|
|
|
|
|
|
|
|
var artists = _artistService.GetAllArtists().ToDictionary(x => x.ArtistMetadataId);
|
|
|
|
var releases = _releaseService.GetAllReleases().GroupBy(x => x.AlbumId).ToDictionary(x => x.Key, y => y.ToList());
|
|
|
|
|
|
|
|
foreach (var album in albums)
|
|
|
|
{
|
2023-10-24 19:43:47 +00:00
|
|
|
if (!artists.TryGetValue(album.ArtistMetadataId, out var albumArtist))
|
2020-08-26 21:03:59 +00:00
|
|
|
{
|
2023-10-24 19:43:47 +00:00
|
|
|
continue;
|
2020-08-26 21:03:59 +00:00
|
|
|
}
|
2023-10-24 19:43:47 +00:00
|
|
|
|
|
|
|
album.Artist = albumArtist;
|
|
|
|
album.AlbumReleases = releases.TryGetValue(album.Id, out var albumReleases) ? albumReleases : new List<AlbumRelease>();
|
2020-08-26 21:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return MapToResource(albums, false);
|
2017-09-16 04:16:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
if (artistId.HasValue)
|
2017-09-16 04:16:26 +00:00
|
|
|
{
|
2021-08-04 20:42:40 +00:00
|
|
|
return MapToResource(_albumService.GetAlbumsByArtist(artistId.Value), false);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
if (foreignAlbumId.IsNotNullOrWhiteSpace())
|
2018-01-18 02:28:47 +00:00
|
|
|
{
|
2019-04-11 06:38:05 +00:00
|
|
|
var album = _albumService.FindById(foreignAlbumId);
|
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
if (album == null)
|
|
|
|
{
|
|
|
|
return MapToResource(new List<Album>(), false);
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
if (includeAllArtistAlbums)
|
2019-04-11 06:38:05 +00:00
|
|
|
{
|
|
|
|
return MapToResource(_albumService.GetAlbumsByArtist(album.ArtistId), false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return MapToResource(new List<Album> { album }, false);
|
|
|
|
}
|
2018-01-18 02:28:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 22:38:31 +00:00
|
|
|
return MapToResource(_albumService.GetAlbums(albumIds), false);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[RestPostById]
|
|
|
|
public ActionResult<AlbumResource> AddAlbum(AlbumResource albumResource)
|
2019-12-16 21:21:32 +00:00
|
|
|
{
|
|
|
|
var album = _addAlbumService.AddAlbum(albumResource.ToModel());
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
return Created(album.Id);
|
2019-12-16 21:21:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[RestPutById]
|
|
|
|
public ActionResult<AlbumResource> UpdateAlbum(AlbumResource albumResource)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2018-01-18 02:28:47 +00:00
|
|
|
var album = _albumService.GetAlbum(albumResource.Id);
|
|
|
|
|
|
|
|
var model = albumResource.ToModel(album);
|
|
|
|
|
|
|
|
_albumService.UpdateAlbum(model);
|
2018-12-15 00:02:43 +00:00
|
|
|
_releaseService.UpdateMany(model.AlbumReleases.Value);
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2018-12-15 00:02:43 +00:00
|
|
|
BroadcastResourceChange(ModelAction.Updated, model.Id);
|
2021-08-04 20:42:40 +00:00
|
|
|
|
|
|
|
return Accepted(model.Id);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[RestDeleteById]
|
2023-05-14 07:20:28 +00:00
|
|
|
public void DeleteAlbum(int id, bool deleteFiles = false, bool addImportListExclusion = false)
|
2019-12-16 21:21:32 +00:00
|
|
|
{
|
|
|
|
_albumService.DeleteAlbum(id, deleteFiles, addImportListExclusion);
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[HttpPut("monitor")]
|
|
|
|
public IActionResult SetAlbumsMonitored([FromBody]AlbumsMonitoredResource resource)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
|
|
|
_albumService.SetMonitored(resource.AlbumIds, resource.Monitored);
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
return Accepted(MapToResource(_albumService.GetAlbums(resource.AlbumIds), false));
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
2019-02-16 14:49:24 +00:00
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[NonAction]
|
2019-02-16 14:49:24 +00:00
|
|
|
public void Handle(AlbumGrabbedEvent message)
|
|
|
|
{
|
|
|
|
foreach (var album in message.Album.Albums)
|
|
|
|
{
|
|
|
|
var resource = album.ToResource();
|
|
|
|
resource.Grabbed = true;
|
|
|
|
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, resource);
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[NonAction]
|
2019-02-16 14:49:24 +00:00
|
|
|
public void Handle(AlbumEditedEvent message)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.Album, true));
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[NonAction]
|
2019-12-16 21:21:32 +00:00
|
|
|
public void Handle(AlbumUpdatedEvent message)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.Album, true));
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[NonAction]
|
2019-12-16 21:21:32 +00:00
|
|
|
public void Handle(AlbumDeletedEvent message)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Deleted, message.Album.ToResource());
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[NonAction]
|
2019-02-16 14:49:24 +00:00
|
|
|
public void Handle(AlbumImportedEvent message)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.Album, true));
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[NonAction]
|
2019-02-16 14:49:24 +00:00
|
|
|
public void Handle(TrackImportedEvent message)
|
|
|
|
{
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, message.TrackInfo.Album.ToResource());
|
|
|
|
}
|
2019-10-29 21:27:27 +00:00
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[NonAction]
|
2019-10-29 21:27:27 +00:00
|
|
|
public void Handle(TrackFileDeletedEvent message)
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
if (message.Reason == DeleteMediaFileReason.Upgrade)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-10-29 21:27:27 +00:00
|
|
|
|
|
|
|
BroadcastResourceChange(ModelAction.Updated, MapToResource(message.TrackFile.Album.Value, true));
|
|
|
|
}
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|