2017-09-04 02:20:56 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-01-03 12:49:24 +00:00
|
|
|
using Lidarr.Http;
|
2021-08-04 20:42:40 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NLog;
|
2019-06-08 19:13:58 +00:00
|
|
|
using NzbDrone.Core.MediaFiles;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.MediaFiles.TrackImport.Manual;
|
|
|
|
using NzbDrone.Core.Music;
|
|
|
|
using NzbDrone.Core.Qualities;
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-10-31 01:28:29 +00:00
|
|
|
namespace Lidarr.Api.V1.ManualImport
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2021-08-04 20:42:40 +00:00
|
|
|
[V1ApiController]
|
|
|
|
public class ManualImportController : Controller
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2018-11-19 03:16:55 +00:00
|
|
|
private readonly IArtistService _artistService;
|
|
|
|
private readonly IAlbumService _albumService;
|
2018-12-15 00:02:43 +00:00
|
|
|
private readonly IReleaseService _releaseService;
|
2019-04-04 08:20:47 +00:00
|
|
|
private readonly IManualImportService _manualImportService;
|
|
|
|
private readonly Logger _logger;
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
public ManualImportController(IManualImportService manualImportService,
|
2018-11-19 03:16:55 +00:00
|
|
|
IArtistService artistService,
|
|
|
|
IAlbumService albumService,
|
2018-12-15 00:02:43 +00:00
|
|
|
IReleaseService releaseService,
|
2018-11-19 03:16:55 +00:00
|
|
|
Logger logger)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2018-11-19 03:16:55 +00:00
|
|
|
_artistService = artistService;
|
2018-12-15 00:02:43 +00:00
|
|
|
_albumService = albumService;
|
|
|
|
_releaseService = releaseService;
|
2019-04-04 08:20:47 +00:00
|
|
|
_manualImportService = manualImportService;
|
|
|
|
_logger = logger;
|
2021-08-04 20:42:40 +00:00
|
|
|
}
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2023-01-14 17:45:54 +00:00
|
|
|
[HttpPost]
|
|
|
|
public IActionResult UpdateItems(List<ManualImportUpdateResource> resource)
|
2021-08-04 20:42:40 +00:00
|
|
|
{
|
|
|
|
return Accepted(UpdateImportItems(resource));
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
[HttpGet]
|
|
|
|
public List<ManualImportResource> GetMediaFiles(string folder, string downloadId, int? artistId, bool filterExistingFiles = true, bool replaceExistingFiles = true)
|
2017-09-04 02:20:56 +00:00
|
|
|
{
|
2020-03-11 21:24:55 +00:00
|
|
|
NzbDrone.Core.Music.Artist artist = null;
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
if (artistId > 0)
|
2020-03-11 21:24:55 +00:00
|
|
|
{
|
2021-08-04 20:42:40 +00:00
|
|
|
artist = _artistService.GetArtist(artistId.Value);
|
2020-03-11 21:24:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:42:40 +00:00
|
|
|
var filter = filterExistingFiles ? FilterFilesType.Matched : FilterFilesType.None;
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2020-03-11 21:24:55 +00:00
|
|
|
return _manualImportService.GetMediaFiles(folder, downloadId, artist, filter, replaceExistingFiles).ToResource().Select(AddQualityWeight).ToList();
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private ManualImportResource AddQualityWeight(ManualImportResource item)
|
|
|
|
{
|
|
|
|
if (item.Quality != null)
|
|
|
|
{
|
|
|
|
item.QualityWeight = Quality.DefaultQualityDefinitions.Single(q => q.Quality == item.Quality.Quality).Weight;
|
|
|
|
item.QualityWeight += item.Quality.Revision.Real * 10;
|
|
|
|
item.QualityWeight += item.Quality.Revision.Version;
|
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
return item;
|
|
|
|
}
|
2018-11-19 03:16:55 +00:00
|
|
|
|
2023-01-14 17:45:54 +00:00
|
|
|
private List<ManualImportResource> UpdateImportItems(List<ManualImportUpdateResource> resources)
|
2018-11-19 03:16:55 +00:00
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
var items = new List<ManualImportItem>();
|
|
|
|
foreach (var resource in resources)
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
items.Add(new ManualImportItem
|
|
|
|
{
|
|
|
|
Id = resource.Id,
|
|
|
|
Path = resource.Path,
|
|
|
|
Name = resource.Name,
|
2023-01-14 17:45:54 +00:00
|
|
|
Artist = resource.ArtistId.HasValue ? _artistService.GetArtist(resource.ArtistId.Value) : null,
|
|
|
|
Album = resource.AlbumId.HasValue ? _albumService.GetAlbum(resource.AlbumId.Value) : null,
|
|
|
|
Release = resource.AlbumReleaseId.HasValue ? _releaseService.GetRelease(resource.AlbumReleaseId.Value) : null,
|
2020-01-03 12:49:24 +00:00
|
|
|
Quality = resource.Quality,
|
2023-01-14 17:45:54 +00:00
|
|
|
ReleaseGroup = resource.ReleaseGroup,
|
2020-01-03 12:49:24 +00:00
|
|
|
DownloadId = resource.DownloadId,
|
|
|
|
AdditionalFile = resource.AdditionalFile,
|
|
|
|
ReplaceExistingFiles = resource.ReplaceExistingFiles,
|
|
|
|
DisableReleaseSwitching = resource.DisableReleaseSwitching
|
|
|
|
});
|
2019-02-16 14:49:24 +00:00
|
|
|
}
|
2020-01-03 12:49:24 +00:00
|
|
|
|
2019-04-04 08:20:47 +00:00
|
|
|
return _manualImportService.UpdateItems(items).Select(x => x.ToResource()).ToList();
|
2018-11-19 03:16:55 +00:00
|
|
|
}
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|