mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2026-04-18 21:55:12 -04:00
70f2361d69
(cherry picked from commit 728df146ada115a367bf1ce808482a4625e6098d)
69 lines
2.3 KiB
C#
69 lines
2.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Xml;
|
|
using System.Xml.Linq;
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
namespace NzbDrone.Core.Download
|
|
{
|
|
public interface IValidateNzbs
|
|
{
|
|
void Validate(byte[] fileContent);
|
|
}
|
|
|
|
public class NzbValidationService : IValidateNzbs
|
|
{
|
|
public void Validate(byte[] fileContent)
|
|
{
|
|
try
|
|
{
|
|
var reader = new StreamReader(new MemoryStream(fileContent));
|
|
|
|
using (var xmlTextReader = XmlReader.Create(reader,
|
|
new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, IgnoreComments = true }))
|
|
{
|
|
var xDoc = XDocument.Load(xmlTextReader);
|
|
var nzb = xDoc.Root;
|
|
|
|
if (nzb == null)
|
|
{
|
|
throw new InvalidNzbException("Invalid NZB: No Root element");
|
|
}
|
|
|
|
// nZEDb has a bug in their error reporting code spitting out invalid http status codes
|
|
if (nzb.Name.LocalName.Equals("error") &&
|
|
nzb.TryGetAttributeValue("code", out var code) &&
|
|
nzb.TryGetAttributeValue("description", out var description))
|
|
{
|
|
throw new InvalidNzbException("Invalid NZB: Contains indexer error: {0} - {1}", code, description);
|
|
}
|
|
|
|
if (!nzb.Name.LocalName.Equals("nzb"))
|
|
{
|
|
throw new InvalidNzbException(
|
|
"Invalid NZB: Unexpected root element. Expected 'nzb' found '{0}'", nzb.Name.LocalName);
|
|
}
|
|
|
|
var ns = nzb.Name.Namespace;
|
|
var files = nzb.Elements(ns + "file").ToList();
|
|
|
|
if (files.Empty())
|
|
{
|
|
throw new InvalidNzbException("Invalid NZB: No files");
|
|
}
|
|
}
|
|
}
|
|
catch (InvalidNzbException)
|
|
{
|
|
// Throw the original exception
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new InvalidNzbException("Invalid NZB: Unable to parse", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|