jellyfin/Emby.Dlna/Api/DlnaServerService.cs

307 lines
13 KiB
C#
Raw Normal View History

using System;
2014-04-10 15:06:54 +00:00
using System.Collections.Generic;
using System.IO;
2019-01-13 19:16:19 +00:00
using System.Linq;
using System.Text;
2014-04-10 15:06:54 +00:00
using System.Threading.Tasks;
2019-01-13 19:16:19 +00:00
using Emby.Dlna.Main;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Common.Extensions;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Controller.Dlna;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Controller.Net;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Model.Services;
2014-04-10 15:06:54 +00:00
2018-09-12 17:26:21 +00:00
namespace Emby.Dlna.Api
2014-04-10 15:06:54 +00:00
{
[Route("/Dlna/{UuId}/description.xml", "GET", Summary = "Gets dlna server info")]
[Route("/Dlna/{UuId}/description", "GET", Summary = "Gets dlna server info")]
public class GetDescriptionXml
{
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
}
2015-01-30 21:14:08 +00:00
[Route("/Dlna/{UuId}/contentdirectory/contentdirectory.xml", "GET", Summary = "Gets dlna content directory xml")]
[Route("/Dlna/{UuId}/contentdirectory/contentdirectory", "GET", Summary = "Gets dlna content directory xml")]
2014-04-10 15:06:54 +00:00
public class GetContentDirectory
{
2015-01-30 21:14:08 +00:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
2014-04-10 15:06:54 +00:00
}
2015-01-30 21:14:08 +00:00
[Route("/Dlna/{UuId}/connectionmanager/connectionmanager.xml", "GET", Summary = "Gets dlna connection manager xml")]
[Route("/Dlna/{UuId}/connectionmanager/connectionmanager", "GET", Summary = "Gets dlna connection manager xml")]
2014-05-21 00:56:24 +00:00
public class GetConnnectionManager
{
2015-01-30 21:14:08 +00:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
2014-05-21 00:56:24 +00:00
}
2015-01-30 21:14:08 +00:00
[Route("/Dlna/{UuId}/mediareceiverregistrar/mediareceiverregistrar.xml", "GET", Summary = "Gets dlna mediareceiverregistrar xml")]
[Route("/Dlna/{UuId}/mediareceiverregistrar/mediareceiverregistrar", "GET", Summary = "Gets dlna mediareceiverregistrar xml")]
2015-01-30 17:58:38 +00:00
public class GetMediaReceiverRegistrar
{
2015-01-30 21:14:08 +00:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
2015-01-30 17:58:38 +00:00
}
2015-01-30 21:14:08 +00:00
[Route("/Dlna/{UuId}/contentdirectory/control", "POST", Summary = "Processes a control request")]
2014-05-21 00:56:24 +00:00
public class ProcessContentDirectoryControlRequest : IRequiresRequestStream
{
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
public Stream RequestStream { get; set; }
}
2015-01-30 21:14:08 +00:00
[Route("/Dlna/{UuId}/connectionmanager/control", "POST", Summary = "Processes a control request")]
2014-05-21 00:56:24 +00:00
public class ProcessConnectionManagerControlRequest : IRequiresRequestStream
2014-04-10 15:06:54 +00:00
{
2014-04-18 05:03:01 +00:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
2014-04-20 05:21:08 +00:00
2014-04-10 15:06:54 +00:00
public Stream RequestStream { get; set; }
}
2015-01-30 21:14:08 +00:00
[Route("/Dlna/{UuId}/mediareceiverregistrar/control", "POST", Summary = "Processes a control request")]
2015-01-30 17:58:38 +00:00
public class ProcessMediaReceiverRegistrarControlRequest : IRequiresRequestStream
{
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
public Stream RequestStream { get; set; }
}
2017-08-30 18:52:29 +00:00
[Route("/Dlna/{UuId}/mediareceiverregistrar/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
[Route("/Dlna/{UuId}/mediareceiverregistrar/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
2015-01-30 17:58:38 +00:00
public class ProcessMediaReceiverRegistrarEventRequest
{
2017-08-30 18:52:29 +00:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
2015-01-30 17:58:38 +00:00
public string UuId { get; set; }
}
2017-08-30 18:52:29 +00:00
[Route("/Dlna/{UuId}/contentdirectory/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
[Route("/Dlna/{UuId}/contentdirectory/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
2014-05-21 01:15:46 +00:00
public class ProcessContentDirectoryEventRequest
{
2017-08-30 18:52:29 +00:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
2014-05-21 01:15:46 +00:00
public string UuId { get; set; }
}
2017-08-30 18:52:29 +00:00
[Route("/Dlna/{UuId}/connectionmanager/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
[Route("/Dlna/{UuId}/connectionmanager/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
2014-05-21 01:15:46 +00:00
public class ProcessConnectionManagerEventRequest
2014-04-20 05:21:08 +00:00
{
2017-08-30 18:52:29 +00:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
2014-04-20 05:21:08 +00:00
public string UuId { get; set; }
}
2015-01-30 21:14:08 +00:00
[Route("/Dlna/{UuId}/icons/{Filename}", "GET", Summary = "Gets a server icon")]
2014-04-16 05:08:12 +00:00
[Route("/Dlna/icons/{Filename}", "GET", Summary = "Gets a server icon")]
public class GetIcon
{
2018-09-12 17:26:21 +00:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2015-01-30 21:14:08 +00:00
public string UuId { get; set; }
2016-11-04 08:31:05 +00:00
2014-04-16 05:08:12 +00:00
[ApiMember(Name = "Filename", Description = "The icon filename", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Filename { get; set; }
}
2018-09-12 17:26:21 +00:00
public class DlnaServerService : IService, IRequiresRequest
2014-04-10 15:06:54 +00:00
{
private readonly IDlnaManager _dlnaManager;
2015-05-13 04:16:33 +00:00
private const string XMLContentType = "text/xml; charset=UTF-8";
2015-05-11 23:04:16 +00:00
2018-09-12 17:26:21 +00:00
public IRequest Request { get; set; }
private IHttpResultFactory _resultFactory;
private IContentDirectory ContentDirectory => DlnaEntryPoint.Current.ContentDirectory;
2018-09-12 17:26:21 +00:00
private IConnectionManager ConnectionManager => DlnaEntryPoint.Current.ConnectionManager;
2018-09-12 17:26:21 +00:00
private IMediaReceiverRegistrar MediaReceiverRegistrar => DlnaEntryPoint.Current.MediaReceiverRegistrar;
2018-09-12 17:26:21 +00:00
public DlnaServerService(IDlnaManager dlnaManager, IHttpResultFactory httpResultFactory)
2014-04-10 15:06:54 +00:00
{
_dlnaManager = dlnaManager;
2018-09-12 17:26:21 +00:00
_resultFactory = httpResultFactory;
}
private string GetHeader(string name)
{
return Request.Headers[name];
2014-04-10 15:06:54 +00:00
}
public object Get(GetDescriptionXml request)
{
2015-01-30 21:14:08 +00:00
var url = Request.AbsoluteUri;
var serverAddress = url.Substring(0, url.IndexOf("/dlna/", StringComparison.OrdinalIgnoreCase));
2016-10-25 19:02:04 +00:00
var xml = _dlnaManager.GetServerDescriptionXml(Request.Headers.ToDictionary(), request.UuId, serverAddress);
2014-04-10 15:06:54 +00:00
2018-09-12 17:26:21 +00:00
var cacheLength = TimeSpan.FromDays(1);
var cacheKey = Request.RawUrl.GetMD5();
var bytes = Encoding.UTF8.GetBytes(xml);
return _resultFactory.GetStaticResult(Request, cacheKey, null, cacheLength, XMLContentType, () => Task.FromResult<Stream>(new MemoryStream(bytes)));
2014-04-10 15:06:54 +00:00
}
public object Get(GetContentDirectory request)
{
2018-09-12 17:26:21 +00:00
var xml = ContentDirectory.GetServiceXml(Request.Headers.ToDictionary());
2014-04-10 15:06:54 +00:00
2018-09-12 17:26:21 +00:00
return _resultFactory.GetResult(Request, xml, XMLContentType);
2014-04-10 15:06:54 +00:00
}
2015-01-30 17:58:38 +00:00
public object Get(GetMediaReceiverRegistrar request)
{
2018-09-12 17:26:21 +00:00
var xml = MediaReceiverRegistrar.GetServiceXml(Request.Headers.ToDictionary());
2015-01-30 17:58:38 +00:00
2018-09-12 17:26:21 +00:00
return _resultFactory.GetResult(Request, xml, XMLContentType);
2015-01-30 17:58:38 +00:00
}
2014-05-21 00:56:24 +00:00
public object Get(GetConnnectionManager request)
{
2018-09-12 17:26:21 +00:00
var xml = ConnectionManager.GetServiceXml(Request.Headers.ToDictionary());
2014-05-21 00:56:24 +00:00
2018-09-12 17:26:21 +00:00
return _resultFactory.GetResult(Request, xml, XMLContentType);
2014-05-21 00:56:24 +00:00
}
2016-11-04 08:31:05 +00:00
public object Post(ProcessMediaReceiverRegistrarControlRequest request)
2014-05-21 00:56:24 +00:00
{
2018-09-12 17:26:21 +00:00
var response = PostAsync(request.RequestStream, MediaReceiverRegistrar);
2014-05-21 00:56:24 +00:00
2018-09-12 17:26:21 +00:00
return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
2014-05-21 00:56:24 +00:00
}
2016-11-04 08:31:05 +00:00
public object Post(ProcessContentDirectoryControlRequest request)
2015-01-30 17:58:38 +00:00
{
2018-09-12 17:26:21 +00:00
var response = PostAsync(request.RequestStream, ContentDirectory);
2015-01-30 17:58:38 +00:00
2018-09-12 17:26:21 +00:00
return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
2015-01-30 17:58:38 +00:00
}
2016-11-04 08:31:05 +00:00
public object Post(ProcessConnectionManagerControlRequest request)
2014-04-10 15:06:54 +00:00
{
2018-09-12 17:26:21 +00:00
var response = PostAsync(request.RequestStream, ConnectionManager);
2014-04-10 15:06:54 +00:00
2018-09-12 17:26:21 +00:00
return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
2014-04-10 15:06:54 +00:00
}
2016-11-04 08:31:05 +00:00
private ControlResponse PostAsync(Stream requestStream, IUpnpService service)
2014-04-10 15:06:54 +00:00
{
2015-01-22 16:41:34 +00:00
var id = GetPathValue(2);
2014-04-20 05:21:08 +00:00
2016-11-04 08:31:05 +00:00
return service.ProcessControlRequest(new ControlRequest
2014-04-10 15:06:54 +00:00
{
2016-11-04 08:31:05 +00:00
Headers = Request.Headers.ToDictionary(),
InputXml = requestStream,
TargetServerUuId = id,
RequestedUrl = Request.AbsoluteUri
});
2014-04-16 05:08:12 +00:00
}
2014-04-10 15:06:54 +00:00
2018-09-12 17:26:21 +00:00
protected string GetPathValue(int index)
2014-04-16 05:08:12 +00:00
{
2018-09-12 17:26:21 +00:00
var pathInfo = Parse(Request.PathInfo);
var first = pathInfo[0];
// backwards compatibility
2018-12-13 09:18:29 +00:00
// TODO: Work out what this is doing.
2018-09-12 17:26:21 +00:00
if (string.Equals(first, "mediabrowser", StringComparison.OrdinalIgnoreCase) ||
2018-12-13 09:18:29 +00:00
string.Equals(first, "emby", StringComparison.OrdinalIgnoreCase) ||
2019-01-13 19:16:19 +00:00
string.Equals(first, "jellyfin", StringComparison.OrdinalIgnoreCase))
2014-04-16 05:08:12 +00:00
{
2018-09-12 17:26:21 +00:00
index++;
}
2014-04-16 05:08:12 +00:00
2018-09-12 17:26:21 +00:00
return pathInfo[index];
}
private List<string> Parse(string pathUri)
{
var actionParts = pathUri.Split(new[] { "://" }, StringSplitOptions.None);
var pathInfo = actionParts[actionParts.Length - 1];
var optionsPos = pathInfo.LastIndexOf('?');
if (optionsPos != -1)
{
pathInfo = pathInfo.Substring(0, optionsPos);
2014-04-16 05:08:12 +00:00
}
2018-09-12 17:26:21 +00:00
var args = pathInfo.Split('/');
return args.Skip(1).ToList();
}
public object Get(GetIcon request)
{
2019-01-27 11:03:43 +00:00
var contentType = "image/" + Path.GetExtension(request.Filename)
.TrimStart('.')
.ToLowerInvariant();
2018-09-12 17:26:21 +00:00
var cacheLength = TimeSpan.FromDays(365);
var cacheKey = Request.RawUrl.GetMD5();
2019-01-13 20:37:13 +00:00
return _resultFactory.GetStaticResult(Request, cacheKey, null, cacheLength, contentType, () => Task.FromResult(_dlnaManager.GetIcon(request.Filename).Stream));
2014-04-16 05:08:12 +00:00
}
2014-04-20 05:21:08 +00:00
2017-08-30 18:52:29 +00:00
public object Subscribe(ProcessContentDirectoryEventRequest request)
2014-05-21 01:15:46 +00:00
{
2018-09-12 17:26:21 +00:00
return ProcessEventRequest(ContentDirectory);
2014-05-21 01:15:46 +00:00
}
2017-08-30 18:52:29 +00:00
public object Subscribe(ProcessConnectionManagerEventRequest request)
2014-05-21 01:15:46 +00:00
{
2018-09-12 17:26:21 +00:00
return ProcessEventRequest(ConnectionManager);
2014-05-21 01:15:46 +00:00
}
2017-08-30 18:52:29 +00:00
public object Subscribe(ProcessMediaReceiverRegistrarEventRequest request)
{
2018-09-12 17:26:21 +00:00
return ProcessEventRequest(MediaReceiverRegistrar);
2017-08-30 18:52:29 +00:00
}
public object Unsubscribe(ProcessContentDirectoryEventRequest request)
{
2018-09-12 17:26:21 +00:00
return ProcessEventRequest(ContentDirectory);
2017-08-30 18:52:29 +00:00
}
public object Unsubscribe(ProcessConnectionManagerEventRequest request)
{
2018-09-12 17:26:21 +00:00
return ProcessEventRequest(ConnectionManager);
2017-08-30 18:52:29 +00:00
}
public object Unsubscribe(ProcessMediaReceiverRegistrarEventRequest request)
2015-01-30 17:58:38 +00:00
{
2018-09-12 17:26:21 +00:00
return ProcessEventRequest(MediaReceiverRegistrar);
2015-01-30 17:58:38 +00:00
}
2014-05-21 01:15:46 +00:00
private object ProcessEventRequest(IEventManager eventManager)
2014-04-20 05:21:08 +00:00
{
var subscriptionId = GetHeader("SID");
if (string.Equals(Request.Verb, "SUBSCRIBE", StringComparison.OrdinalIgnoreCase))
{
2017-07-20 20:37:13 +00:00
var notificationType = GetHeader("NT");
var callback = GetHeader("CALLBACK");
var timeoutString = GetHeader("TIMEOUT");
2014-04-20 05:21:08 +00:00
if (string.IsNullOrEmpty(notificationType))
{
2017-10-04 18:51:26 +00:00
return GetSubscriptionResponse(eventManager.RenewEventSubscription(subscriptionId, notificationType, timeoutString, callback));
2014-04-20 05:21:08 +00:00
}
2017-07-20 20:37:13 +00:00
return GetSubscriptionResponse(eventManager.CreateEventSubscription(notificationType, timeoutString, callback));
2014-04-20 05:21:08 +00:00
}
2014-05-21 01:15:46 +00:00
return GetSubscriptionResponse(eventManager.CancelEventSubscription(subscriptionId));
2014-04-20 05:21:08 +00:00
}
2014-04-21 16:02:30 +00:00
private object GetSubscriptionResponse(EventSubscriptionResponse response)
2014-04-20 05:21:08 +00:00
{
2018-09-12 17:26:21 +00:00
return _resultFactory.GetResult(Request, response.Content, response.ContentType, response.Headers);
2014-04-20 05:21:08 +00:00
}
2014-04-10 15:06:54 +00:00
}
}