DescriptionXmlBuilder.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Security;
  7. using System.Text;
  8. using Emby.Dlna.Common;
  9. using MediaBrowser.Model.Dlna;
  10. namespace Emby.Dlna.Server
  11. {
  12. public class DescriptionXmlBuilder
  13. {
  14. private readonly DeviceProfile _profile;
  15. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  16. private readonly string _serverUdn;
  17. private readonly string _serverAddress;
  18. private readonly string _serverName;
  19. private readonly string _serverId;
  20. public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn, string serverAddress, string serverName, string serverId)
  21. {
  22. if (string.IsNullOrEmpty(serverUdn))
  23. {
  24. throw new ArgumentNullException(nameof(serverUdn));
  25. }
  26. if (string.IsNullOrEmpty(serverAddress))
  27. {
  28. throw new ArgumentNullException(nameof(serverAddress));
  29. }
  30. _profile = profile;
  31. _serverUdn = serverUdn;
  32. _serverAddress = serverAddress;
  33. _serverName = serverName;
  34. _serverId = serverId;
  35. }
  36. private static bool EnableAbsoluteUrls => false;
  37. public string GetXml()
  38. {
  39. var builder = new StringBuilder();
  40. builder.Append("<?xml version=\"1.0\"?>");
  41. builder.Append("<root");
  42. var attributes = _profile.XmlRootAttributes.ToList();
  43. attributes.Insert(0, new XmlAttribute
  44. {
  45. Name = "xmlns:dlna",
  46. Value = "urn:schemas-dlna-org:device-1-0"
  47. });
  48. attributes.Insert(0, new XmlAttribute
  49. {
  50. Name = "xmlns",
  51. Value = "urn:schemas-upnp-org:device-1-0"
  52. });
  53. foreach (var att in attributes)
  54. {
  55. builder.AppendFormat(CultureInfo.InvariantCulture, " {0}=\"{1}\"", att.Name, att.Value);
  56. }
  57. builder.Append('>');
  58. builder.Append("<specVersion>");
  59. builder.Append("<major>1</major>");
  60. builder.Append("<minor>0</minor>");
  61. builder.Append("</specVersion>");
  62. if (!EnableAbsoluteUrls)
  63. {
  64. builder.Append("<URLBase>")
  65. .Append(SecurityElement.Escape(_serverAddress))
  66. .Append("</URLBase>");
  67. }
  68. AppendDeviceInfo(builder);
  69. builder.Append("</root>");
  70. return builder.ToString();
  71. }
  72. private void AppendDeviceInfo(StringBuilder builder)
  73. {
  74. builder.Append("<device>");
  75. AppendDeviceProperties(builder);
  76. AppendIconList(builder);
  77. builder.Append("<presentationURL>")
  78. .Append(SecurityElement.Escape(_serverAddress))
  79. .Append("/web/index.html</presentationURL>");
  80. AppendServiceList(builder);
  81. builder.Append("</device>");
  82. }
  83. private void AppendDeviceProperties(StringBuilder builder)
  84. {
  85. builder.Append("<dlna:X_DLNACAP/>");
  86. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">DMS-1.50</dlna:X_DLNADOC>");
  87. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">M-DMS-1.50</dlna:X_DLNADOC>");
  88. builder.Append("<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>");
  89. builder.Append("<friendlyName>")
  90. .Append(SecurityElement.Escape(GetFriendlyName()))
  91. .Append("</friendlyName>");
  92. builder.Append("<manufacturer>")
  93. .Append(SecurityElement.Escape(_profile.Manufacturer ?? string.Empty))
  94. .Append("</manufacturer>");
  95. builder.Append("<manufacturerURL>")
  96. .Append(SecurityElement.Escape(_profile.ManufacturerUrl ?? string.Empty))
  97. .Append("</manufacturerURL>");
  98. builder.Append("<modelDescription>")
  99. .Append(SecurityElement.Escape(_profile.ModelDescription ?? string.Empty))
  100. .Append("</modelDescription>");
  101. builder.Append("<modelName>")
  102. .Append(SecurityElement.Escape(_profile.ModelName ?? string.Empty))
  103. .Append("</modelName>");
  104. builder.Append("<modelNumber>")
  105. .Append(SecurityElement.Escape(_profile.ModelNumber ?? string.Empty))
  106. .Append("</modelNumber>");
  107. builder.Append("<modelURL>")
  108. .Append(SecurityElement.Escape(_profile.ModelUrl ?? string.Empty))
  109. .Append("</modelURL>");
  110. if (string.IsNullOrEmpty(_profile.SerialNumber))
  111. {
  112. builder.Append("<serialNumber>")
  113. .Append(SecurityElement.Escape(_serverId))
  114. .Append("</serialNumber>");
  115. }
  116. else
  117. {
  118. builder.Append("<serialNumber>")
  119. .Append(SecurityElement.Escape(_profile.SerialNumber))
  120. .Append("</serialNumber>");
  121. }
  122. builder.Append("<UPC/>");
  123. builder.Append("<UDN>uuid:")
  124. .Append(SecurityElement.Escape(_serverUdn))
  125. .Append("</UDN>");
  126. if (!string.IsNullOrEmpty(_profile.SonyAggregationFlags))
  127. {
  128. builder.Append("<av:aggregationFlags xmlns:av=\"urn:schemas-sony-com:av\">")
  129. .Append(SecurityElement.Escape(_profile.SonyAggregationFlags))
  130. .Append("</av:aggregationFlags>");
  131. }
  132. }
  133. private string GetFriendlyName()
  134. {
  135. if (string.IsNullOrEmpty(_profile.FriendlyName))
  136. {
  137. return "Jellyfin - " + _serverName;
  138. }
  139. var characterList = new List<char>();
  140. foreach (var c in _serverName)
  141. {
  142. if (char.IsLetterOrDigit(c) || c == '-')
  143. {
  144. characterList.Add(c);
  145. }
  146. }
  147. var characters = characterList.ToArray();
  148. var serverName = new string(characters);
  149. var name = _profile.FriendlyName?.Replace("${HostName}", serverName, StringComparison.OrdinalIgnoreCase);
  150. return name ?? string.Empty;
  151. }
  152. private void AppendIconList(StringBuilder builder)
  153. {
  154. builder.Append("<iconList>");
  155. foreach (var icon in GetIcons())
  156. {
  157. builder.Append("<icon>");
  158. builder.Append("<mimetype>")
  159. .Append(SecurityElement.Escape(icon.MimeType ?? string.Empty))
  160. .Append("</mimetype>");
  161. builder.Append("<width>")
  162. .Append(SecurityElement.Escape(icon.Width.ToString(_usCulture)))
  163. .Append("</width>");
  164. builder.Append("<height>")
  165. .Append(SecurityElement.Escape(icon.Height.ToString(_usCulture)))
  166. .Append("</height>");
  167. builder.Append("<depth>")
  168. .Append(SecurityElement.Escape(icon.Depth ?? string.Empty))
  169. .Append("</depth>");
  170. builder.Append("<url>")
  171. .Append(BuildUrl(icon.Url))
  172. .Append("</url>");
  173. builder.Append("</icon>");
  174. }
  175. builder.Append("</iconList>");
  176. }
  177. private void AppendServiceList(StringBuilder builder)
  178. {
  179. builder.Append("<serviceList>");
  180. foreach (var service in GetServices())
  181. {
  182. builder.Append("<service>");
  183. builder.Append("<serviceType>")
  184. .Append(SecurityElement.Escape(service.ServiceType ?? string.Empty))
  185. .Append("</serviceType>");
  186. builder.Append("<serviceId>")
  187. .Append(SecurityElement.Escape(service.ServiceId ?? string.Empty))
  188. .Append("</serviceId>");
  189. builder.Append("<SCPDURL>")
  190. .Append(BuildUrl(service.ScpdUrl, true))
  191. .Append("</SCPDURL>");
  192. builder.Append("<controlURL>")
  193. .Append(BuildUrl(service.ControlUrl, true))
  194. .Append("</controlURL>");
  195. builder.Append("<eventSubURL>")
  196. .Append(BuildUrl(service.EventSubUrl, true))
  197. .Append("</eventSubURL>");
  198. builder.Append("</service>");
  199. }
  200. builder.Append("</serviceList>");
  201. }
  202. /// <summary>
  203. /// Builds a valid url for inclusion in the xml.
  204. /// </summary>
  205. /// <param name="url">Url to include.</param>
  206. /// <param name="absoluteUrl">Optional. When set to true, the absolute url is always used.</param>
  207. /// <returns>The url to use for the element.</returns>
  208. private string BuildUrl(string url, bool absoluteUrl = false)
  209. {
  210. if (string.IsNullOrEmpty(url))
  211. {
  212. return string.Empty;
  213. }
  214. url = url.TrimStart('/');
  215. url = "/dlna/" + _serverUdn + "/" + url;
  216. if (EnableAbsoluteUrls || absoluteUrl)
  217. {
  218. url = _serverAddress.TrimEnd('/') + url;
  219. }
  220. return SecurityElement.Escape(url);
  221. }
  222. private IEnumerable<DeviceIcon> GetIcons()
  223. => new[]
  224. {
  225. new DeviceIcon
  226. {
  227. MimeType = "image/png",
  228. Depth = "24",
  229. Width = 240,
  230. Height = 240,
  231. Url = "icons/logo240.png"
  232. },
  233. new DeviceIcon
  234. {
  235. MimeType = "image/jpeg",
  236. Depth = "24",
  237. Width = 240,
  238. Height = 240,
  239. Url = "icons/logo240.jpg"
  240. },
  241. new DeviceIcon
  242. {
  243. MimeType = "image/png",
  244. Depth = "24",
  245. Width = 120,
  246. Height = 120,
  247. Url = "icons/logo120.png"
  248. },
  249. new DeviceIcon
  250. {
  251. MimeType = "image/jpeg",
  252. Depth = "24",
  253. Width = 120,
  254. Height = 120,
  255. Url = "icons/logo120.jpg"
  256. },
  257. new DeviceIcon
  258. {
  259. MimeType = "image/png",
  260. Depth = "24",
  261. Width = 48,
  262. Height = 48,
  263. Url = "icons/logo48.png"
  264. },
  265. new DeviceIcon
  266. {
  267. MimeType = "image/jpeg",
  268. Depth = "24",
  269. Width = 48,
  270. Height = 48,
  271. Url = "icons/logo48.jpg"
  272. }
  273. };
  274. private IEnumerable<DeviceService> GetServices()
  275. {
  276. var list = new List<DeviceService>();
  277. list.Add(new DeviceService
  278. {
  279. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  280. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  281. ScpdUrl = "contentdirectory/contentdirectory.xml",
  282. ControlUrl = "contentdirectory/control",
  283. EventSubUrl = "contentdirectory/events"
  284. });
  285. list.Add(new DeviceService
  286. {
  287. ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
  288. ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
  289. ScpdUrl = "connectionmanager/connectionmanager.xml",
  290. ControlUrl = "connectionmanager/control",
  291. EventSubUrl = "connectionmanager/events"
  292. });
  293. if (_profile.EnableMSMediaReceiverRegistrar)
  294. {
  295. list.Add(new DeviceService
  296. {
  297. ServiceType = "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
  298. ServiceId = "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar",
  299. ScpdUrl = "mediareceiverregistrar/mediareceiverregistrar.xml",
  300. ControlUrl = "mediareceiverregistrar/control",
  301. EventSubUrl = "mediareceiverregistrar/events"
  302. });
  303. }
  304. return list;
  305. }
  306. public override string ToString()
  307. {
  308. return GetXml();
  309. }
  310. }
  311. }