jellyfin-server/Emby.Dlna/Server/DescriptionXmlBuilder.cs

358 lines
12 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
2016-10-29 22:22:20 +00:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2020-09-16 11:08:37 +00:00
using System.Security;
2016-10-29 22:22:20 +00:00
using System.Text;
2020-09-16 11:08:23 +00:00
using Emby.Dlna.Common;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Model.Dlna;
2016-10-29 22:22:20 +00:00
2016-10-29 22:34:54 +00:00
namespace Emby.Dlna.Server
2016-10-29 22:22:20 +00:00
{
public class DescriptionXmlBuilder
{
private readonly DeviceProfile _profile;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly string _serverUdn;
private readonly string _serverAddress;
private readonly string _serverName;
private readonly string _serverId;
2020-09-16 11:08:23 +00:00
public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn, string serverAddress, string serverName, string serverId)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(serverUdn))
2016-10-29 22:22:20 +00:00
{
throw new ArgumentNullException(nameof(serverUdn));
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(serverAddress))
2016-10-29 22:22:20 +00:00
{
throw new ArgumentNullException(nameof(serverAddress));
2016-10-29 22:22:20 +00:00
}
_profile = profile;
_serverUdn = serverUdn;
_serverAddress = serverAddress;
_serverName = serverName;
_serverId = serverId;
}
public string GetXml()
{
var builder = new StringBuilder();
builder.Append("<?xml version=\"1.0\"?>");
builder.Append("<root");
var attributes = _profile.XmlRootAttributes.ToList();
attributes.Insert(0, new XmlAttribute
{
Name = "xmlns:dlna",
Value = "urn:schemas-dlna-org:device-1-0"
});
attributes.Insert(0, new XmlAttribute
{
Name = "xmlns",
Value = "urn:schemas-upnp-org:device-1-0"
});
foreach (var att in attributes)
{
2020-07-19 19:31:14 +00:00
builder.AppendFormat(CultureInfo.InvariantCulture, " {0}=\"{1}\"", att.Name, att.Value);
2016-10-29 22:22:20 +00:00
}
builder.Append('>');
2016-10-29 22:22:20 +00:00
builder.Append("<specVersion>");
builder.Append("<major>1</major>");
builder.Append("<minor>0</minor>");
builder.Append("</specVersion>");
AppendDeviceInfo(builder);
builder.Append("</root>");
return builder.ToString();
}
private void AppendDeviceInfo(StringBuilder builder)
{
builder.Append("<device>");
AppendDeviceProperties(builder);
AppendIconList(builder);
2018-09-12 17:26:21 +00:00
builder.Append("<presentationURL>")
.Append(SecurityElement.Escape(_serverAddress))
.Append("/web/index.html</presentationURL>");
2018-09-12 17:26:21 +00:00
2016-10-29 22:22:20 +00:00
AppendServiceList(builder);
builder.Append("</device>");
}
private void AppendDeviceProperties(StringBuilder builder)
{
2018-09-12 17:26:21 +00:00
builder.Append("<dlna:X_DLNACAP/>");
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">DMS-1.50</dlna:X_DLNADOC>");
2016-10-29 22:22:20 +00:00
builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">M-DMS-1.50</dlna:X_DLNADOC>");
2018-09-12 17:26:21 +00:00
builder.Append("<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>");
2016-10-29 22:22:20 +00:00
builder.Append("<friendlyName>")
.Append(SecurityElement.Escape(GetFriendlyName()))
.Append("</friendlyName>");
builder.Append("<manufacturer>")
.Append(SecurityElement.Escape(_profile.Manufacturer ?? string.Empty))
.Append("</manufacturer>");
builder.Append("<manufacturerURL>")
.Append(SecurityElement.Escape(_profile.ManufacturerUrl ?? string.Empty))
.Append("</manufacturerURL>");
builder.Append("<modelDescription>")
.Append(SecurityElement.Escape(_profile.ModelDescription ?? string.Empty))
.Append("</modelDescription>");
builder.Append("<modelName>")
.Append(SecurityElement.Escape(_profile.ModelName ?? string.Empty))
.Append("</modelName>");
builder.Append("<modelNumber>")
.Append(SecurityElement.Escape(_profile.ModelNumber ?? string.Empty))
.Append("</modelNumber>");
builder.Append("<modelURL>")
.Append(SecurityElement.Escape(_profile.ModelUrl ?? string.Empty))
.Append("</modelURL>");
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(_profile.SerialNumber))
2016-10-29 22:22:20 +00:00
{
builder.Append("<serialNumber>")
.Append(SecurityElement.Escape(_serverId))
.Append("</serialNumber>");
2016-10-29 22:22:20 +00:00
}
else
{
builder.Append("<serialNumber>")
.Append(SecurityElement.Escape(_profile.SerialNumber))
.Append("</serialNumber>");
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
builder.Append("<UPC/>");
2016-10-29 22:22:20 +00:00
builder.Append("<UDN>uuid:")
.Append(SecurityElement.Escape(_serverUdn))
.Append("</UDN>");
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
if (!string.IsNullOrEmpty(_profile.SonyAggregationFlags))
2016-10-29 22:22:20 +00:00
{
builder.Append("<av:aggregationFlags xmlns:av=\"urn:schemas-sony-com:av\">")
.Append(SecurityElement.Escape(_profile.SonyAggregationFlags))
.Append("</av:aggregationFlags>");
2016-10-29 22:22:20 +00:00
}
}
private string GetFriendlyName()
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(_profile.FriendlyName))
2016-10-29 22:22:20 +00:00
{
2020-09-16 11:08:23 +00:00
return "Jellyfin - " + _serverName;
2016-10-29 22:22:20 +00:00
}
2016-11-04 08:31:05 +00:00
var characterList = new List<char>();
foreach (var c in _serverName)
{
if (char.IsLetterOrDigit(c) || c == '-')
{
characterList.Add(c);
}
}
2018-09-12 17:26:21 +00:00
var characters = characterList.ToArray();
2016-10-29 22:22:20 +00:00
var serverName = new string(characters);
2019-01-27 11:03:43 +00:00
var name = _profile.FriendlyName?.Replace("${HostName}", serverName, StringComparison.OrdinalIgnoreCase);
2016-10-29 22:22:20 +00:00
2019-01-27 11:03:43 +00:00
return name ?? string.Empty;
2016-10-29 22:22:20 +00:00
}
private void AppendIconList(StringBuilder builder)
{
builder.Append("<iconList>");
foreach (var icon in GetIcons())
{
builder.Append("<icon>");
builder.Append("<mimetype>")
.Append(SecurityElement.Escape(icon.MimeType ?? string.Empty))
.Append("</mimetype>");
builder.Append("<width>")
.Append(SecurityElement.Escape(icon.Width.ToString(_usCulture)))
.Append("</width>");
builder.Append("<height>")
.Append(SecurityElement.Escape(icon.Height.ToString(_usCulture)))
.Append("</height>");
builder.Append("<depth>")
.Append(SecurityElement.Escape(icon.Depth ?? string.Empty))
.Append("</depth>");
builder.Append("<url>")
.Append(BuildUrl(icon.Url))
.Append("</url>");
2016-10-29 22:22:20 +00:00
builder.Append("</icon>");
}
builder.Append("</iconList>");
}
private void AppendServiceList(StringBuilder builder)
{
builder.Append("<serviceList>");
foreach (var service in GetServices())
{
builder.Append("<service>");
builder.Append("<serviceType>")
.Append(SecurityElement.Escape(service.ServiceType ?? string.Empty))
.Append("</serviceType>");
builder.Append("<serviceId>")
.Append(SecurityElement.Escape(service.ServiceId ?? string.Empty))
.Append("</serviceId>");
builder.Append("<SCPDURL>")
2020-09-25 17:46:20 +00:00
.Append(BuildUrl(service.ScpdUrl))
.Append("</SCPDURL>");
builder.Append("<controlURL>")
2020-09-25 17:46:20 +00:00
.Append(BuildUrl(service.ControlUrl))
.Append("</controlURL>");
builder.Append("<eventSubURL>")
2020-09-25 17:46:20 +00:00
.Append(BuildUrl(service.EventSubUrl))
.Append("</eventSubURL>");
2016-10-29 22:22:20 +00:00
builder.Append("</service>");
}
builder.Append("</serviceList>");
}
2020-09-25 17:47:24 +00:00
private string BuildUrl(string url)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(url))
2016-10-29 22:22:20 +00:00
{
return string.Empty;
}
2020-11-21 19:50:40 +00:00
url = _serverAddress.TrimEnd('/') + "/dlna/" + _serverUdn + "/" + url.TrimStart('/');
2016-10-29 22:22:20 +00:00
// TODO: @bond remove null-coalescing operator when https://github.com/dotnet/runtime/pull/52442 is merged/released
return SecurityElement.Escape(url) ?? string.Empty;
2016-10-29 22:22:20 +00:00
}
2020-09-16 11:08:23 +00:00
private IEnumerable<DeviceIcon> GetIcons()
2019-01-27 11:03:43 +00:00
=> new[]
2016-10-29 22:22:20 +00:00
{
2019-01-27 11:03:43 +00:00
new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 240,
Height = 240,
Url = "icons/logo240.png"
},
new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 240,
Height = 240,
Url = "icons/logo240.jpg"
},
new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 120,
Height = 120,
Url = "icons/logo120.png"
},
new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 120,
Height = 120,
Url = "icons/logo120.jpg"
},
new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 48,
Height = 48,
Url = "icons/logo48.png"
},
new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 48,
Height = 48,
Url = "icons/logo48.jpg"
}
};
2016-10-29 22:22:20 +00:00
private IEnumerable<DeviceService> GetServices()
{
2020-09-16 11:08:23 +00:00
var list = new List<DeviceService>();
list.Add(new DeviceService
2016-10-29 22:22:20 +00:00
{
2020-09-16 11:08:23 +00:00
ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
ScpdUrl = "contentdirectory/contentdirectory.xml",
ControlUrl = "contentdirectory/control",
EventSubUrl = "contentdirectory/events"
});
2016-10-29 22:22:20 +00:00
2020-09-16 11:08:23 +00:00
list.Add(new DeviceService
{
ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
ScpdUrl = "connectionmanager/connectionmanager.xml",
ControlUrl = "connectionmanager/control",
EventSubUrl = "connectionmanager/events"
});
2016-10-29 22:22:20 +00:00
if (_profile.EnableMSMediaReceiverRegistrar)
{
list.Add(new DeviceService
{
ServiceType = "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
ServiceId = "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar",
ScpdUrl = "mediareceiverregistrar/mediareceiverregistrar.xml",
ControlUrl = "mediareceiverregistrar/control",
EventSubUrl = "mediareceiverregistrar/events"
});
}
return list;
}
public override string ToString()
{
return GetXml();
}
}
}