mirror of
https://github.com/Sonarr/Sonarr.git
synced 2026-04-19 21:46:43 -04:00
4bdb0408f1
Closes #7942
95 lines
3.8 KiB
C#
95 lines
3.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NzbDrone.Common.Extensions;
|
|
using NzbDrone.Core.Languages;
|
|
using NzbDrone.Core.MediaFiles.EpisodeImport.Manual;
|
|
using NzbDrone.Core.Qualities;
|
|
using Sonarr.Api.V3.CustomFormats;
|
|
using Sonarr.Api.V3.Episodes;
|
|
using Sonarr.Http;
|
|
using Sonarr.Http.REST;
|
|
|
|
namespace Sonarr.Api.V3.ManualImport
|
|
{
|
|
[V3ApiController]
|
|
public class ManualImportController : Controller
|
|
{
|
|
private readonly IManualImportService _manualImportService;
|
|
|
|
public ManualImportController(IManualImportService manualImportService)
|
|
{
|
|
_manualImportService = manualImportService;
|
|
}
|
|
|
|
[HttpGet]
|
|
[Produces("application/json")]
|
|
public List<ManualImportResource> GetMediaFiles(string folder, string downloadId, int? seriesId, int? seasonNumber, bool filterExistingFiles = true)
|
|
{
|
|
if (seriesId.HasValue && downloadId.IsNullOrWhiteSpace())
|
|
{
|
|
return _manualImportService.GetMediaFiles(seriesId.Value, seasonNumber).ToResource().Select(AddQualityWeight).ToList();
|
|
}
|
|
|
|
return _manualImportService.GetMediaFiles(folder, downloadId, seriesId, filterExistingFiles).ToResource().Select(AddQualityWeight).ToList();
|
|
}
|
|
|
|
[HttpPost]
|
|
[Consumes("application/json")]
|
|
public object ReprocessItems([FromBody] List<ManualImportReprocessResource> items)
|
|
{
|
|
if (items is { Count: 0 })
|
|
{
|
|
throw new BadRequestException("items must be provided");
|
|
}
|
|
|
|
foreach (var item in items)
|
|
{
|
|
var processedItem = _manualImportService.ReprocessItem(item.Path, item.DownloadId, item.SeriesId, item.SeasonNumber, item.EpisodeIds ?? new List<int>(), item.ReleaseGroup, item.Quality, item.Languages, item.IndexerFlags, item.ReleaseType);
|
|
|
|
item.SeasonNumber = processedItem.SeasonNumber;
|
|
item.Episodes = processedItem.Episodes.ToResource();
|
|
item.ReleaseType = processedItem.ReleaseType;
|
|
item.IndexerFlags = processedItem.IndexerFlags;
|
|
item.Rejections = processedItem.Rejections.Select(r => r.ToResource());
|
|
item.CustomFormats = processedItem.CustomFormats.ToResource(false);
|
|
item.CustomFormatScore = processedItem.CustomFormatScore;
|
|
|
|
// Only set the language/quality if they're unknown and languages were returned.
|
|
// Languages won't be returned when reprocessing if the season/episode isn't filled in yet and we don't want to return no languages to the client.
|
|
if (item.Languages.Count <= 1 && (item.Languages.SingleOrDefault() ?? Language.Unknown) == Language.Unknown && processedItem.Languages.Any())
|
|
{
|
|
item.Languages = processedItem.Languages;
|
|
}
|
|
|
|
if (item.Quality?.Quality == Quality.Unknown)
|
|
{
|
|
item.Quality = processedItem.Quality;
|
|
}
|
|
|
|
if (item.ReleaseGroup.IsNotNullOrWhiteSpace())
|
|
{
|
|
item.ReleaseGroup = processedItem.ReleaseGroup;
|
|
}
|
|
|
|
// Clear episode IDs in favour of the full episode
|
|
item.EpisodeIds = null;
|
|
}
|
|
|
|
return items;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
return item;
|
|
}
|
|
}
|
|
}
|