mirror of
https://github.com/lidarr/Lidarr
synced 2025-02-26 15:52:42 +00:00
Fixed: ReleasePush Validation and Error on Parse (#711)
* Fixed: ReleasePush Validation and Error on Parse * Fixed: Validation on Release module, Catch Errors in format
This commit is contained in:
parent
d8a361dd91
commit
57df287547
3 changed files with 56 additions and 17 deletions
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using Nancy;
|
using Nancy;
|
||||||
using Nancy.ModelBinding;
|
using Nancy.ModelBinding;
|
||||||
|
@ -13,6 +14,7 @@
|
||||||
using NzbDrone.Core.Parser.Model;
|
using NzbDrone.Core.Parser.Model;
|
||||||
using NzbDrone.Core.Validation;
|
using NzbDrone.Core.Validation;
|
||||||
using Lidarr.Http.Extensions;
|
using Lidarr.Http.Extensions;
|
||||||
|
using Lidarr.Http.REST;
|
||||||
using HttpStatusCode = System.Net.HttpStatusCode;
|
using HttpStatusCode = System.Net.HttpStatusCode;
|
||||||
|
|
||||||
namespace Lidarr.Api.V1.Indexers
|
namespace Lidarr.Api.V1.Indexers
|
||||||
|
@ -25,6 +27,7 @@ public class ReleaseModule : ReleaseModuleBase
|
||||||
private readonly IPrioritizeDownloadDecision _prioritizeDownloadDecision;
|
private readonly IPrioritizeDownloadDecision _prioritizeDownloadDecision;
|
||||||
private readonly IDownloadService _downloadService;
|
private readonly IDownloadService _downloadService;
|
||||||
private readonly Logger _logger;
|
private readonly Logger _logger;
|
||||||
|
private ResourceValidator<ReleaseResource> _releaseValidator;
|
||||||
|
|
||||||
private readonly ICached<RemoteAlbum> _remoteAlbumCache;
|
private readonly ICached<RemoteAlbum> _remoteAlbumCache;
|
||||||
|
|
||||||
|
@ -43,17 +46,25 @@ public ReleaseModule(IFetchAndParseRss rssFetcherAndParser,
|
||||||
_downloadService = downloadService;
|
_downloadService = downloadService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
|
||||||
|
_releaseValidator = new ResourceValidator<ReleaseResource>();
|
||||||
|
_releaseValidator.RuleFor(s => s.IndexerId).ValidId();
|
||||||
|
_releaseValidator.RuleFor(s => s.Guid).NotEmpty();
|
||||||
|
|
||||||
GetResourceAll = GetReleases;
|
GetResourceAll = GetReleases;
|
||||||
Post["/"] = x => DownloadRelease(this.Bind<ReleaseResource>());
|
Post["/"] = x => DownloadRelease(this.Bind<ReleaseResource>());
|
||||||
|
|
||||||
PostValidator.RuleFor(s => s.IndexerId).ValidId();
|
|
||||||
PostValidator.RuleFor(s => s.Guid).NotEmpty();
|
|
||||||
|
|
||||||
_remoteAlbumCache = cacheManager.GetCache<RemoteAlbum>(GetType(), "remoteAlbums");
|
_remoteAlbumCache = cacheManager.GetCache<RemoteAlbum>(GetType(), "remoteAlbums");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Response DownloadRelease(ReleaseResource release)
|
private Response DownloadRelease(ReleaseResource release)
|
||||||
{
|
{
|
||||||
|
var validationFailures = _releaseValidator.Validate(release).Errors;
|
||||||
|
|
||||||
|
if (validationFailures.Any())
|
||||||
|
{
|
||||||
|
throw new ValidationException(validationFailures);
|
||||||
|
}
|
||||||
|
|
||||||
var remoteAlbum = _remoteAlbumCache.Find(GetCacheKey(release));
|
var remoteAlbum = _remoteAlbumCache.Find(GetCacheKey(release));
|
||||||
|
|
||||||
if (remoteAlbum == null)
|
if (remoteAlbum == null)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
|
using FluentValidation.Results;
|
||||||
using Nancy;
|
using Nancy;
|
||||||
using Nancy.ModelBinding;
|
using Nancy.ModelBinding;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
@ -11,6 +12,10 @@
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Core.Datastore;
|
using NzbDrone.Core.Datastore;
|
||||||
using NzbDrone.Core.Indexers;
|
using NzbDrone.Core.Indexers;
|
||||||
|
using Lidarr.Http.REST;
|
||||||
|
using System;
|
||||||
|
using NzbDrone.Core.Exceptions;
|
||||||
|
using HttpStatusCode = System.Net.HttpStatusCode;
|
||||||
|
|
||||||
namespace Lidarr.Api.V1.Indexers
|
namespace Lidarr.Api.V1.Indexers
|
||||||
{
|
{
|
||||||
|
@ -20,6 +25,7 @@ class ReleasePushModule : ReleaseModuleBase
|
||||||
private readonly IProcessDownloadDecisions _downloadDecisionProcessor;
|
private readonly IProcessDownloadDecisions _downloadDecisionProcessor;
|
||||||
private readonly IIndexerFactory _indexerFactory;
|
private readonly IIndexerFactory _indexerFactory;
|
||||||
private readonly Logger _logger;
|
private readonly Logger _logger;
|
||||||
|
private ResourceValidator<ReleaseResource> _releaseValidator;
|
||||||
|
|
||||||
public ReleasePushModule(IMakeDownloadDecision downloadDecisionMaker,
|
public ReleasePushModule(IMakeDownloadDecision downloadDecisionMaker,
|
||||||
IProcessDownloadDecisions downloadDecisionProcessor,
|
IProcessDownloadDecisions downloadDecisionProcessor,
|
||||||
|
@ -31,19 +37,39 @@ public ReleasePushModule(IMakeDownloadDecision downloadDecisionMaker,
|
||||||
_indexerFactory = indexerFactory;
|
_indexerFactory = indexerFactory;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
|
||||||
Post["/push"] = x => ProcessRelease(this.Bind<ReleaseResource>());
|
_releaseValidator = new ResourceValidator<ReleaseResource>();
|
||||||
|
_releaseValidator.RuleFor(s => s.Title).NotEmpty();
|
||||||
|
_releaseValidator.RuleFor(s => s.DownloadUrl).NotEmpty();
|
||||||
|
_releaseValidator.RuleFor(s => s.DownloadProtocol).NotEmpty();
|
||||||
|
_releaseValidator.RuleFor(s => s.PublishDate).NotEmpty();
|
||||||
|
|
||||||
PostValidator.RuleFor(s => s.Title).NotEmpty();
|
Post["/push"] = x => ProcessRelease();
|
||||||
PostValidator.RuleFor(s => s.DownloadUrl).NotEmpty();
|
|
||||||
PostValidator.RuleFor(s => s.DownloadProtocol).NotEmpty();
|
|
||||||
PostValidator.RuleFor(s => s.PublishDate).NotEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Response ProcessRelease(ReleaseResource release)
|
private Response ProcessRelease()
|
||||||
{
|
{
|
||||||
_logger.Info("Release pushed: {0} - {1}", release.Title, release.DownloadUrl);
|
|
||||||
|
|
||||||
var info = release.ToModel();
|
var resource = new ReleaseResource();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
resource = Request.Body.FromJson<ReleaseResource>();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new NzbDroneClientException(HttpStatusCode.BadRequest, ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
var validationFailures = _releaseValidator.Validate(resource).Errors;
|
||||||
|
|
||||||
|
if (validationFailures.Any())
|
||||||
|
{
|
||||||
|
throw new ValidationException(validationFailures);
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.Info("Release pushed: {0} - {1}", resource.Title, resource.DownloadUrl);
|
||||||
|
|
||||||
|
var info = resource.ToModel();
|
||||||
|
|
||||||
info.Guid = "PUSH-" + info.DownloadUrl;
|
info.Guid = "PUSH-" + info.DownloadUrl;
|
||||||
|
|
||||||
|
@ -52,7 +78,14 @@ private Response ProcessRelease(ReleaseResource release)
|
||||||
var decisions = _downloadDecisionMaker.GetRssDecision(new List<ReleaseInfo> { info });
|
var decisions = _downloadDecisionMaker.GetRssDecision(new List<ReleaseInfo> { info });
|
||||||
_downloadDecisionProcessor.ProcessDecisions(decisions);
|
_downloadDecisionProcessor.ProcessDecisions(decisions);
|
||||||
|
|
||||||
return MapDecisions(decisions).First().AsResponse();
|
var firstDecision = decisions.FirstOrDefault();
|
||||||
|
|
||||||
|
if (firstDecision?.RemoteAlbum.ParsedAlbumInfo == null)
|
||||||
|
{
|
||||||
|
throw new ValidationException(new List<ValidationFailure> { new ValidationFailure("Title", "Unable to parse", resource.Title) });
|
||||||
|
}
|
||||||
|
|
||||||
|
return MapDecisions(new[] { firstDecision }).AsResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ResolveIndexer(ReleaseInfo release)
|
private void ResolveIndexer(ReleaseInfo release)
|
||||||
|
|
|
@ -54,11 +54,6 @@ public class ReleaseResource : RestResource
|
||||||
//TODO: besides a test I don't think this is used...
|
//TODO: besides a test I don't think this is used...
|
||||||
public DownloadProtocol DownloadProtocol { get; set; }
|
public DownloadProtocol DownloadProtocol { get; set; }
|
||||||
|
|
||||||
//public bool IsDaily { get; set; }
|
|
||||||
//public bool IsAbsoluteNumbering { get; set; }
|
|
||||||
//public bool IsPossibleSpecialEpisode { get; set; }
|
|
||||||
//public bool Special { get; set; }
|
|
||||||
|
|
||||||
// Sent when queuing an unknown release
|
// Sent when queuing an unknown release
|
||||||
|
|
||||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
|
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||||
|
|
Loading…
Reference in a new issue