ControlHandler.cs 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Xml;
  10. using Emby.Dlna.Didl;
  11. using Emby.Dlna.Service;
  12. using Jellyfin.Data.Entities;
  13. using Jellyfin.Data.Enums;
  14. using MediaBrowser.Common.Extensions;
  15. using MediaBrowser.Controller.Configuration;
  16. using MediaBrowser.Controller.Drawing;
  17. using MediaBrowser.Controller.Dto;
  18. using MediaBrowser.Controller.Entities;
  19. using MediaBrowser.Controller.Entities.Audio;
  20. using MediaBrowser.Controller.Entities.Movies;
  21. using MediaBrowser.Controller.Library;
  22. using MediaBrowser.Controller.LiveTv;
  23. using MediaBrowser.Controller.MediaEncoding;
  24. using MediaBrowser.Controller.Playlists;
  25. using MediaBrowser.Controller.TV;
  26. using MediaBrowser.Model.Dlna;
  27. using MediaBrowser.Model.Entities;
  28. using MediaBrowser.Model.Globalization;
  29. using MediaBrowser.Model.Querying;
  30. using Microsoft.Extensions.Logging;
  31. using Book = MediaBrowser.Controller.Entities.Book;
  32. using Episode = MediaBrowser.Controller.Entities.TV.Episode;
  33. using Genre = MediaBrowser.Controller.Entities.Genre;
  34. using Movie = MediaBrowser.Controller.Entities.Movies.Movie;
  35. using MusicAlbum = MediaBrowser.Controller.Entities.Audio.MusicAlbum;
  36. using Series = MediaBrowser.Controller.Entities.TV.Series;
  37. namespace Emby.Dlna.ContentDirectory
  38. {
  39. public class ControlHandler : BaseControlHandler
  40. {
  41. private readonly ILibraryManager _libraryManager;
  42. private readonly IUserDataManager _userDataManager;
  43. private readonly IServerConfigurationManager _config;
  44. private readonly User _user;
  45. private readonly IUserViewManager _userViewManager;
  46. private readonly ITVSeriesManager _tvSeriesManager;
  47. private const string NS_DC = "http://purl.org/dc/elements/1.1/";
  48. private const string NS_DIDL = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
  49. private const string NS_DLNA = "urn:schemas-dlna-org:metadata-1-0/";
  50. private const string NS_UPNP = "urn:schemas-upnp-org:metadata-1-0/upnp/";
  51. private readonly int _systemUpdateId;
  52. private readonly DidlBuilder _didlBuilder;
  53. private readonly DeviceProfile _profile;
  54. public ControlHandler(
  55. ILogger logger,
  56. ILibraryManager libraryManager,
  57. DeviceProfile profile,
  58. string serverAddress,
  59. string accessToken,
  60. IImageProcessor imageProcessor,
  61. IUserDataManager userDataManager,
  62. User user,
  63. int systemUpdateId,
  64. IServerConfigurationManager config,
  65. ILocalizationManager localization,
  66. IMediaSourceManager mediaSourceManager,
  67. IUserViewManager userViewManager,
  68. IMediaEncoder mediaEncoder,
  69. ITVSeriesManager tvSeriesManager)
  70. : base(config, logger)
  71. {
  72. _libraryManager = libraryManager;
  73. _userDataManager = userDataManager;
  74. _user = user;
  75. _systemUpdateId = systemUpdateId;
  76. _userViewManager = userViewManager;
  77. _tvSeriesManager = tvSeriesManager;
  78. _profile = profile;
  79. _config = config;
  80. _didlBuilder = new DidlBuilder(
  81. profile,
  82. user,
  83. imageProcessor,
  84. serverAddress,
  85. accessToken,
  86. userDataManager,
  87. localization,
  88. mediaSourceManager,
  89. Logger,
  90. mediaEncoder,
  91. libraryManager);
  92. }
  93. /// <inheritdoc />
  94. protected override void WriteResult(string methodName, IDictionary<string, string> methodParams, XmlWriter xmlWriter)
  95. {
  96. const string DeviceId = "test";
  97. if (string.Equals(methodName, "GetSearchCapabilities", StringComparison.OrdinalIgnoreCase))
  98. {
  99. HandleGetSearchCapabilities(xmlWriter);
  100. return;
  101. }
  102. if (string.Equals(methodName, "GetSortCapabilities", StringComparison.OrdinalIgnoreCase))
  103. {
  104. HandleGetSortCapabilities(xmlWriter);
  105. return;
  106. }
  107. if (string.Equals(methodName, "GetSortExtensionCapabilities", StringComparison.OrdinalIgnoreCase))
  108. {
  109. HandleGetSortExtensionCapabilities(xmlWriter);
  110. return;
  111. }
  112. if (string.Equals(methodName, "GetSystemUpdateID", StringComparison.OrdinalIgnoreCase))
  113. {
  114. HandleGetSystemUpdateID(xmlWriter);
  115. return;
  116. }
  117. if (string.Equals(methodName, "Browse", StringComparison.OrdinalIgnoreCase))
  118. {
  119. HandleBrowse(xmlWriter, methodParams, DeviceId);
  120. return;
  121. }
  122. if (string.Equals(methodName, "X_GetFeatureList", StringComparison.OrdinalIgnoreCase))
  123. {
  124. HandleXGetFeatureList(xmlWriter);
  125. return;
  126. }
  127. if (string.Equals(methodName, "GetFeatureList", StringComparison.OrdinalIgnoreCase))
  128. {
  129. HandleGetFeatureList(xmlWriter);
  130. return;
  131. }
  132. if (string.Equals(methodName, "X_SetBookmark", StringComparison.OrdinalIgnoreCase))
  133. {
  134. HandleXSetBookmark(methodParams);
  135. return;
  136. }
  137. if (string.Equals(methodName, "Search", StringComparison.OrdinalIgnoreCase))
  138. {
  139. HandleSearch(xmlWriter, methodParams, DeviceId);
  140. return;
  141. }
  142. if (string.Equals(methodName, "X_BrowseByLetter", StringComparison.OrdinalIgnoreCase))
  143. {
  144. HandleXBrowseByLetter(xmlWriter, methodParams, DeviceId);
  145. return;
  146. }
  147. throw new ResourceNotFoundException("Unexpected control request name: " + methodName);
  148. }
  149. private void HandleXSetBookmark(IDictionary<string, string> sparams)
  150. {
  151. var id = sparams["ObjectID"];
  152. var serverItem = GetItemFromObjectId(id);
  153. var item = serverItem.Item;
  154. var newbookmark = int.Parse(sparams["PosSecond"], CultureInfo.InvariantCulture);
  155. var userdata = _userDataManager.GetUserData(_user, item);
  156. userdata.PlaybackPositionTicks = TimeSpan.FromSeconds(newbookmark).Ticks;
  157. _userDataManager.SaveUserData(_user, item, userdata, UserDataSaveReason.TogglePlayed,
  158. CancellationToken.None);
  159. }
  160. private void HandleGetSearchCapabilities(XmlWriter xmlWriter)
  161. {
  162. xmlWriter.WriteElementString(
  163. "SearchCaps",
  164. "res@resolution,res@size,res@duration,dc:title,dc:creator,upnp:actor,upnp:artist,upnp:genre,upnp:album,dc:date,upnp:class,@id,@refID,@protocolInfo,upnp:author,dc:description,pv:avKeywords");
  165. }
  166. private void HandleGetSortCapabilities(XmlWriter xmlWriter)
  167. {
  168. xmlWriter.WriteElementString(
  169. "SortCaps",
  170. "res@duration,res@size,res@bitrate,dc:date,dc:title,dc:size,upnp:album,upnp:artist,upnp:albumArtist,upnp:episodeNumber,upnp:genre,upnp:originalTrackNumber,upnp:rating");
  171. }
  172. private void HandleGetSortExtensionCapabilities(XmlWriter xmlWriter)
  173. {
  174. xmlWriter.WriteElementString(
  175. "SortExtensionCaps",
  176. "res@duration,res@size,res@bitrate,dc:date,dc:title,dc:size,upnp:album,upnp:artist,upnp:albumArtist,upnp:episodeNumber,upnp:genre,upnp:originalTrackNumber,upnp:rating");
  177. }
  178. private void HandleGetSystemUpdateID(XmlWriter xmlWriter)
  179. {
  180. xmlWriter.WriteElementString("Id", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
  181. }
  182. private void HandleGetFeatureList(XmlWriter xmlWriter)
  183. {
  184. xmlWriter.WriteElementString("FeatureList", WriteFeatureListXml());
  185. }
  186. private void HandleXGetFeatureList(XmlWriter xmlWriter)
  187. => HandleGetFeatureList(xmlWriter);
  188. private string WriteFeatureListXml()
  189. {
  190. // TODO: clean this up
  191. var builder = new StringBuilder();
  192. builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  193. builder.Append("<Features xmlns=\"urn:schemas-upnp-org:av:avs\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:schemas-upnp-org:av:avs http://www.upnp.org/schemas/av/avs.xsd\">");
  194. builder.Append("<Feature name=\"samsung.com_BASICVIEW\" version=\"1\">");
  195. builder.Append("<container id=\"I\" type=\"object.item.imageItem\"/>");
  196. builder.Append("<container id=\"A\" type=\"object.item.audioItem\"/>");
  197. builder.Append("<container id=\"V\" type=\"object.item.videoItem\"/>");
  198. builder.Append("</Feature>");
  199. builder.Append("</Features>");
  200. return builder.ToString();
  201. }
  202. public string GetValueOrDefault(IDictionary<string, string> sparams, string key, string defaultValue)
  203. {
  204. if (sparams.TryGetValue(key, out string val))
  205. {
  206. return val;
  207. }
  208. return defaultValue;
  209. }
  210. private void HandleBrowse(XmlWriter xmlWriter, IDictionary<string, string> sparams, string deviceId)
  211. {
  212. var id = sparams["ObjectID"];
  213. var flag = sparams["BrowseFlag"];
  214. var filter = new Filter(GetValueOrDefault(sparams, "Filter", "*"));
  215. var sortCriteria = new SortCriteria(GetValueOrDefault(sparams, "SortCriteria", ""));
  216. var provided = 0;
  217. // Default to null instead of 0
  218. // Upnp inspector sends 0 as requestedCount when it wants everything
  219. int? requestedCount = null;
  220. int? start = 0;
  221. if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out var requestedVal) && requestedVal > 0)
  222. {
  223. requestedCount = requestedVal;
  224. }
  225. if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out var startVal) && startVal > 0)
  226. {
  227. start = startVal;
  228. }
  229. int totalCount;
  230. using (StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8))
  231. {
  232. var settings = new XmlWriterSettings()
  233. {
  234. Encoding = Encoding.UTF8,
  235. CloseOutput = false,
  236. OmitXmlDeclaration = true,
  237. ConformanceLevel = ConformanceLevel.Fragment
  238. };
  239. using (var writer = XmlWriter.Create(builder, settings))
  240. {
  241. writer.WriteStartElement(string.Empty, "DIDL-Lite", NS_DIDL);
  242. writer.WriteAttributeString("xmlns", "dc", null, NS_DC);
  243. writer.WriteAttributeString("xmlns", "dlna", null, NS_DLNA);
  244. writer.WriteAttributeString("xmlns", "upnp", null, NS_UPNP);
  245. DidlBuilder.WriteXmlRootAttributes(_profile, writer);
  246. var serverItem = GetItemFromObjectId(id);
  247. var item = serverItem.Item;
  248. if (string.Equals(flag, "BrowseMetadata", StringComparison.Ordinal))
  249. {
  250. totalCount = 1;
  251. if (item.IsDisplayedAsFolder || serverItem.StubType.HasValue)
  252. {
  253. var childrenResult = GetUserItems(item, serverItem.StubType, _user, sortCriteria, start, requestedCount);
  254. _didlBuilder.WriteFolderElement(writer, item, serverItem.StubType, null, childrenResult.TotalRecordCount, filter, id);
  255. }
  256. else
  257. {
  258. var dlnaOptions = _config.GetDlnaConfiguration();
  259. _didlBuilder.WriteItemElement(writer, item, _user, null, null, deviceId, filter);
  260. }
  261. provided++;
  262. }
  263. else
  264. {
  265. var childrenResult = GetUserItems(item, serverItem.StubType, _user, sortCriteria, start, requestedCount);
  266. totalCount = childrenResult.TotalRecordCount;
  267. provided = childrenResult.Items.Count;
  268. var dlnaOptions = _config.GetDlnaConfiguration();
  269. foreach (var i in childrenResult.Items)
  270. {
  271. var childItem = i.Item;
  272. var displayStubType = i.StubType;
  273. if (childItem.IsDisplayedAsFolder || displayStubType.HasValue)
  274. {
  275. var childCount = GetUserItems(childItem, displayStubType, _user, sortCriteria, null, 0)
  276. .TotalRecordCount;
  277. _didlBuilder.WriteFolderElement(writer, childItem, displayStubType, item, childCount, filter);
  278. }
  279. else
  280. {
  281. _didlBuilder.WriteItemElement(writer, childItem, _user, item, serverItem.StubType, deviceId, filter);
  282. }
  283. }
  284. }
  285. writer.WriteFullEndElement();
  286. }
  287. xmlWriter.WriteElementString("Result", builder.ToString());
  288. }
  289. xmlWriter.WriteElementString("NumberReturned", provided.ToString(CultureInfo.InvariantCulture));
  290. xmlWriter.WriteElementString("TotalMatches", totalCount.ToString(CultureInfo.InvariantCulture));
  291. xmlWriter.WriteElementString("UpdateID", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
  292. }
  293. private void HandleXBrowseByLetter(XmlWriter xmlWriter, IDictionary<string, string> sparams, string deviceId)
  294. {
  295. // TODO: Implement this method
  296. HandleSearch(xmlWriter, sparams, deviceId);
  297. }
  298. private void HandleSearch(XmlWriter xmlWriter, IDictionary<string, string> sparams, string deviceId)
  299. {
  300. var searchCriteria = new SearchCriteria(GetValueOrDefault(sparams, "SearchCriteria", ""));
  301. var sortCriteria = new SortCriteria(GetValueOrDefault(sparams, "SortCriteria", ""));
  302. var filter = new Filter(GetValueOrDefault(sparams, "Filter", "*"));
  303. // sort example: dc:title, dc:date
  304. // Default to null instead of 0
  305. // Upnp inspector sends 0 as requestedCount when it wants everything
  306. int? requestedCount = null;
  307. int? start = 0;
  308. if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out var requestedVal) && requestedVal > 0)
  309. {
  310. requestedCount = requestedVal;
  311. }
  312. if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out var startVal) && startVal > 0)
  313. {
  314. start = startVal;
  315. }
  316. QueryResult<BaseItem> childrenResult;
  317. using (StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8))
  318. {
  319. var settings = new XmlWriterSettings()
  320. {
  321. Encoding = Encoding.UTF8,
  322. CloseOutput = false,
  323. OmitXmlDeclaration = true,
  324. ConformanceLevel = ConformanceLevel.Fragment
  325. };
  326. using (var writer = XmlWriter.Create(builder, settings))
  327. {
  328. writer.WriteStartElement(string.Empty, "DIDL-Lite", NS_DIDL);
  329. writer.WriteAttributeString("xmlns", "dc", null, NS_DC);
  330. writer.WriteAttributeString("xmlns", "dlna", null, NS_DLNA);
  331. writer.WriteAttributeString("xmlns", "upnp", null, NS_UPNP);
  332. DidlBuilder.WriteXmlRootAttributes(_profile, writer);
  333. var serverItem = GetItemFromObjectId(sparams["ContainerID"]);
  334. var item = serverItem.Item;
  335. childrenResult = GetChildrenSorted(item, _user, searchCriteria, sortCriteria, start, requestedCount);
  336. var dlnaOptions = _config.GetDlnaConfiguration();
  337. foreach (var i in childrenResult.Items)
  338. {
  339. if (i.IsDisplayedAsFolder)
  340. {
  341. var childCount = GetChildrenSorted(i, _user, searchCriteria, sortCriteria, null, 0)
  342. .TotalRecordCount;
  343. _didlBuilder.WriteFolderElement(writer, i, null, item, childCount, filter);
  344. }
  345. else
  346. {
  347. _didlBuilder.WriteItemElement(writer, i, _user, item, serverItem.StubType, deviceId, filter);
  348. }
  349. }
  350. writer.WriteFullEndElement();
  351. }
  352. xmlWriter.WriteElementString("Result", builder.ToString());
  353. }
  354. xmlWriter.WriteElementString("NumberReturned", childrenResult.Items.Count.ToString(CultureInfo.InvariantCulture));
  355. xmlWriter.WriteElementString("TotalMatches", childrenResult.TotalRecordCount.ToString(CultureInfo.InvariantCulture));
  356. xmlWriter.WriteElementString("UpdateID", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
  357. }
  358. private QueryResult<BaseItem> GetChildrenSorted(BaseItem item, User user, SearchCriteria search, SortCriteria sort, int? startIndex, int? limit)
  359. {
  360. var folder = (Folder)item;
  361. var sortOrders = folder.IsPreSorted
  362. ? Array.Empty<(string, SortOrder)>()
  363. : new[] { (ItemSortBy.SortName, sort.SortOrder) };
  364. string[] mediaTypes = Array.Empty<string>();
  365. bool? isFolder = null;
  366. if (search.SearchType == SearchType.Audio)
  367. {
  368. mediaTypes = new[] { MediaType.Audio };
  369. isFolder = false;
  370. }
  371. else if (search.SearchType == SearchType.Video)
  372. {
  373. mediaTypes = new[] { MediaType.Video };
  374. isFolder = false;
  375. }
  376. else if (search.SearchType == SearchType.Image)
  377. {
  378. mediaTypes = new[] { MediaType.Photo };
  379. isFolder = false;
  380. }
  381. else if (search.SearchType == SearchType.Playlist)
  382. {
  383. // items = items.OfType<Playlist>();
  384. isFolder = true;
  385. }
  386. else if (search.SearchType == SearchType.MusicAlbum)
  387. {
  388. // items = items.OfType<MusicAlbum>();
  389. isFolder = true;
  390. }
  391. return folder.GetItems(new InternalItemsQuery
  392. {
  393. Limit = limit,
  394. StartIndex = startIndex,
  395. OrderBy = sortOrders,
  396. User = user,
  397. Recursive = true,
  398. IsMissing = false,
  399. ExcludeItemTypes = new[] { typeof(Book).Name },
  400. IsFolder = isFolder,
  401. MediaTypes = mediaTypes,
  402. DtoOptions = GetDtoOptions()
  403. });
  404. }
  405. private DtoOptions GetDtoOptions()
  406. {
  407. return new DtoOptions(true);
  408. }
  409. private QueryResult<ServerItem> GetUserItems(BaseItem item, StubType? stubType, User user, SortCriteria sort, int? startIndex, int? limit)
  410. {
  411. if (item is MusicGenre)
  412. {
  413. return GetMusicGenreItems(item, Guid.Empty, user, sort, startIndex, limit);
  414. }
  415. if (item is MusicArtist)
  416. {
  417. return GetMusicArtistItems(item, Guid.Empty, user, sort, startIndex, limit);
  418. }
  419. if (item is Genre)
  420. {
  421. return GetGenreItems(item, Guid.Empty, user, sort, startIndex, limit);
  422. }
  423. if ((!stubType.HasValue || stubType.Value != StubType.Folder)
  424. && item is IHasCollectionType collectionFolder)
  425. {
  426. if (string.Equals(CollectionType.Music, collectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
  427. {
  428. return GetMusicFolders(item, user, stubType, sort, startIndex, limit);
  429. }
  430. else if (string.Equals(CollectionType.Movies, collectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
  431. {
  432. return GetMovieFolders(item, user, stubType, sort, startIndex, limit);
  433. }
  434. else if (string.Equals(CollectionType.TvShows, collectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
  435. {
  436. return GetTvFolders(item, user, stubType, sort, startIndex, limit);
  437. }
  438. else if (string.Equals(CollectionType.Folders, collectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
  439. {
  440. return GetFolders(user, startIndex, limit);
  441. }
  442. else if (string.Equals(CollectionType.LiveTv, collectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
  443. {
  444. return GetLiveTvChannels(user, sort, startIndex, limit);
  445. }
  446. }
  447. if (stubType.HasValue)
  448. {
  449. if (stubType.Value != StubType.Folder)
  450. {
  451. return ApplyPaging(new QueryResult<ServerItem>(), startIndex, limit);
  452. }
  453. }
  454. var folder = (Folder)item;
  455. var query = new InternalItemsQuery(user)
  456. {
  457. Limit = limit,
  458. StartIndex = startIndex,
  459. IsVirtualItem = false,
  460. ExcludeItemTypes = new[] { typeof(Book).Name },
  461. IsPlaceHolder = false,
  462. DtoOptions = GetDtoOptions()
  463. };
  464. SetSorting(query, sort, folder.IsPreSorted);
  465. var queryResult = folder.GetItems(query);
  466. return ToResult(queryResult);
  467. }
  468. private QueryResult<ServerItem> GetLiveTvChannels(User user, SortCriteria sort, int? startIndex, int? limit)
  469. {
  470. var query = new InternalItemsQuery(user)
  471. {
  472. StartIndex = startIndex,
  473. Limit = limit,
  474. };
  475. query.IncludeItemTypes = new[] { typeof(LiveTvChannel).Name };
  476. SetSorting(query, sort, false);
  477. var result = _libraryManager.GetItemsResult(query);
  478. return ToResult(result);
  479. }
  480. private QueryResult<ServerItem> GetMusicFolders(BaseItem item, User user, StubType? stubType, SortCriteria sort, int? startIndex, int? limit)
  481. {
  482. var query = new InternalItemsQuery(user)
  483. {
  484. StartIndex = startIndex,
  485. Limit = limit
  486. };
  487. SetSorting(query, sort, false);
  488. if (stubType.HasValue && stubType.Value == StubType.Latest)
  489. {
  490. return GetMusicLatest(item, user, query);
  491. }
  492. if (stubType.HasValue && stubType.Value == StubType.Playlists)
  493. {
  494. return GetMusicPlaylists(user, query);
  495. }
  496. if (stubType.HasValue && stubType.Value == StubType.Albums)
  497. {
  498. return GetMusicAlbums(item, user, query);
  499. }
  500. if (stubType.HasValue && stubType.Value == StubType.Artists)
  501. {
  502. return GetMusicArtists(item, user, query);
  503. }
  504. if (stubType.HasValue && stubType.Value == StubType.AlbumArtists)
  505. {
  506. return GetMusicAlbumArtists(item, user, query);
  507. }
  508. if (stubType.HasValue && stubType.Value == StubType.FavoriteAlbums)
  509. {
  510. return GetFavoriteAlbums(item, user, query);
  511. }
  512. if (stubType.HasValue && stubType.Value == StubType.FavoriteArtists)
  513. {
  514. return GetFavoriteArtists(item, user, query);
  515. }
  516. if (stubType.HasValue && stubType.Value == StubType.FavoriteSongs)
  517. {
  518. return GetFavoriteSongs(item, user, query);
  519. }
  520. if (stubType.HasValue && stubType.Value == StubType.Songs)
  521. {
  522. return GetMusicSongs(item, user, query);
  523. }
  524. if (stubType.HasValue && stubType.Value == StubType.Genres)
  525. {
  526. return GetMusicGenres(item, user, query);
  527. }
  528. var list = new List<ServerItem>();
  529. list.Add(new ServerItem(item)
  530. {
  531. StubType = StubType.Latest
  532. });
  533. list.Add(new ServerItem(item)
  534. {
  535. StubType = StubType.Playlists
  536. });
  537. list.Add(new ServerItem(item)
  538. {
  539. StubType = StubType.Albums
  540. });
  541. list.Add(new ServerItem(item)
  542. {
  543. StubType = StubType.AlbumArtists
  544. });
  545. list.Add(new ServerItem(item)
  546. {
  547. StubType = StubType.Artists
  548. });
  549. list.Add(new ServerItem(item)
  550. {
  551. StubType = StubType.Songs
  552. });
  553. list.Add(new ServerItem(item)
  554. {
  555. StubType = StubType.Genres
  556. });
  557. list.Add(new ServerItem(item)
  558. {
  559. StubType = StubType.FavoriteArtists
  560. });
  561. list.Add(new ServerItem(item)
  562. {
  563. StubType = StubType.FavoriteAlbums
  564. });
  565. list.Add(new ServerItem(item)
  566. {
  567. StubType = StubType.FavoriteSongs
  568. });
  569. return new QueryResult<ServerItem>
  570. {
  571. Items = list,
  572. TotalRecordCount = list.Count
  573. };
  574. }
  575. private QueryResult<ServerItem> GetMovieFolders(BaseItem item, User user, StubType? stubType, SortCriteria sort, int? startIndex, int? limit)
  576. {
  577. var query = new InternalItemsQuery(user)
  578. {
  579. StartIndex = startIndex,
  580. Limit = limit
  581. };
  582. SetSorting(query, sort, false);
  583. if (stubType.HasValue && stubType.Value == StubType.ContinueWatching)
  584. {
  585. return GetMovieContinueWatching(item, user, query);
  586. }
  587. if (stubType.HasValue && stubType.Value == StubType.Latest)
  588. {
  589. return GetMovieLatest(item, user, query);
  590. }
  591. if (stubType.HasValue && stubType.Value == StubType.Movies)
  592. {
  593. return GetMovieMovies(item, user, query);
  594. }
  595. if (stubType.HasValue && stubType.Value == StubType.Collections)
  596. {
  597. return GetMovieCollections(user, query);
  598. }
  599. if (stubType.HasValue && stubType.Value == StubType.Favorites)
  600. {
  601. return GetMovieFavorites(item, user, query);
  602. }
  603. if (stubType.HasValue && stubType.Value == StubType.Genres)
  604. {
  605. return GetGenres(item, user, query);
  606. }
  607. var array = new[]
  608. {
  609. new ServerItem(item)
  610. {
  611. StubType = StubType.ContinueWatching
  612. },
  613. new ServerItem(item)
  614. {
  615. StubType = StubType.Latest
  616. },
  617. new ServerItem(item)
  618. {
  619. StubType = StubType.Movies
  620. },
  621. new ServerItem(item)
  622. {
  623. StubType = StubType.Collections
  624. },
  625. new ServerItem(item)
  626. {
  627. StubType = StubType.Favorites
  628. },
  629. new ServerItem(item)
  630. {
  631. StubType = StubType.Genres
  632. }
  633. };
  634. return new QueryResult<ServerItem>
  635. {
  636. Items = array,
  637. TotalRecordCount = array.Length
  638. };
  639. }
  640. private QueryResult<ServerItem> GetFolders(User user, int? startIndex, int? limit)
  641. {
  642. var folders = _libraryManager.GetUserRootFolder().GetChildren(user, true)
  643. .OrderBy(i => i.SortName)
  644. .Select(i => new ServerItem(i)
  645. {
  646. StubType = StubType.Folder
  647. })
  648. .ToArray();
  649. return ApplyPaging(new QueryResult<ServerItem>
  650. {
  651. Items = folders,
  652. TotalRecordCount = folders.Length
  653. }, startIndex, limit);
  654. }
  655. private QueryResult<ServerItem> GetTvFolders(BaseItem item, User user, StubType? stubType, SortCriteria sort, int? startIndex, int? limit)
  656. {
  657. var query = new InternalItemsQuery(user)
  658. {
  659. StartIndex = startIndex,
  660. Limit = limit
  661. };
  662. SetSorting(query, sort, false);
  663. if (stubType.HasValue && stubType.Value == StubType.ContinueWatching)
  664. {
  665. return GetMovieContinueWatching(item, user, query);
  666. }
  667. if (stubType.HasValue && stubType.Value == StubType.NextUp)
  668. {
  669. return GetNextUp(item, query);
  670. }
  671. if (stubType.HasValue && stubType.Value == StubType.Latest)
  672. {
  673. return GetTvLatest(item, user, query);
  674. }
  675. if (stubType.HasValue && stubType.Value == StubType.Series)
  676. {
  677. return GetSeries(item, user, query);
  678. }
  679. if (stubType.HasValue && stubType.Value == StubType.FavoriteSeries)
  680. {
  681. return GetFavoriteSeries(item, user, query);
  682. }
  683. if (stubType.HasValue && stubType.Value == StubType.FavoriteEpisodes)
  684. {
  685. return GetFavoriteEpisodes(item, user, query);
  686. }
  687. if (stubType.HasValue && stubType.Value == StubType.Genres)
  688. {
  689. return GetGenres(item, user, query);
  690. }
  691. var list = new List<ServerItem>();
  692. list.Add(new ServerItem(item)
  693. {
  694. StubType = StubType.ContinueWatching
  695. });
  696. list.Add(new ServerItem(item)
  697. {
  698. StubType = StubType.NextUp
  699. });
  700. list.Add(new ServerItem(item)
  701. {
  702. StubType = StubType.Latest
  703. });
  704. list.Add(new ServerItem(item)
  705. {
  706. StubType = StubType.Series
  707. });
  708. list.Add(new ServerItem(item)
  709. {
  710. StubType = StubType.FavoriteSeries
  711. });
  712. list.Add(new ServerItem(item)
  713. {
  714. StubType = StubType.FavoriteEpisodes
  715. });
  716. list.Add(new ServerItem(item)
  717. {
  718. StubType = StubType.Genres
  719. });
  720. return new QueryResult<ServerItem>
  721. {
  722. Items = list,
  723. TotalRecordCount = list.Count
  724. };
  725. }
  726. private QueryResult<ServerItem> GetMovieContinueWatching(BaseItem parent, User user, InternalItemsQuery query)
  727. {
  728. query.Recursive = true;
  729. query.Parent = parent;
  730. query.SetUser(user);
  731. query.OrderBy = new[]
  732. {
  733. (ItemSortBy.DatePlayed, SortOrder.Descending),
  734. (ItemSortBy.SortName, SortOrder.Ascending)
  735. };
  736. query.IsResumable = true;
  737. query.Limit = 10;
  738. var result = _libraryManager.GetItemsResult(query);
  739. return ToResult(result);
  740. }
  741. private QueryResult<ServerItem> GetSeries(BaseItem parent, User user, InternalItemsQuery query)
  742. {
  743. query.Recursive = true;
  744. query.Parent = parent;
  745. query.SetUser(user);
  746. query.IncludeItemTypes = new[] { typeof(Series).Name };
  747. var result = _libraryManager.GetItemsResult(query);
  748. return ToResult(result);
  749. }
  750. private QueryResult<ServerItem> GetMovieMovies(BaseItem parent, User user, InternalItemsQuery query)
  751. {
  752. query.Recursive = true;
  753. query.Parent = parent;
  754. query.SetUser(user);
  755. query.IncludeItemTypes = new[] { typeof(Movie).Name };
  756. var result = _libraryManager.GetItemsResult(query);
  757. return ToResult(result);
  758. }
  759. private QueryResult<ServerItem> GetMovieCollections(User user, InternalItemsQuery query)
  760. {
  761. query.Recursive = true;
  762. // query.Parent = parent;
  763. query.SetUser(user);
  764. query.IncludeItemTypes = new[] { typeof(BoxSet).Name };
  765. var result = _libraryManager.GetItemsResult(query);
  766. return ToResult(result);
  767. }
  768. private QueryResult<ServerItem> GetMusicAlbums(BaseItem parent, User user, InternalItemsQuery query)
  769. {
  770. query.Recursive = true;
  771. query.Parent = parent;
  772. query.SetUser(user);
  773. query.IncludeItemTypes = new[] { typeof(MusicAlbum).Name };
  774. var result = _libraryManager.GetItemsResult(query);
  775. return ToResult(result);
  776. }
  777. private QueryResult<ServerItem> GetMusicSongs(BaseItem parent, User user, InternalItemsQuery query)
  778. {
  779. query.Recursive = true;
  780. query.Parent = parent;
  781. query.SetUser(user);
  782. query.IncludeItemTypes = new[] { typeof(Audio).Name };
  783. var result = _libraryManager.GetItemsResult(query);
  784. return ToResult(result);
  785. }
  786. private QueryResult<ServerItem> GetFavoriteSongs(BaseItem parent, User user, InternalItemsQuery query)
  787. {
  788. query.Recursive = true;
  789. query.Parent = parent;
  790. query.SetUser(user);
  791. query.IsFavorite = true;
  792. query.IncludeItemTypes = new[] { typeof(Audio).Name };
  793. var result = _libraryManager.GetItemsResult(query);
  794. return ToResult(result);
  795. }
  796. private QueryResult<ServerItem> GetFavoriteSeries(BaseItem parent, User user, InternalItemsQuery query)
  797. {
  798. query.Recursive = true;
  799. query.Parent = parent;
  800. query.SetUser(user);
  801. query.IsFavorite = true;
  802. query.IncludeItemTypes = new[] { typeof(Series).Name };
  803. var result = _libraryManager.GetItemsResult(query);
  804. return ToResult(result);
  805. }
  806. private QueryResult<ServerItem> GetFavoriteEpisodes(BaseItem parent, User user, InternalItemsQuery query)
  807. {
  808. query.Recursive = true;
  809. query.Parent = parent;
  810. query.SetUser(user);
  811. query.IsFavorite = true;
  812. query.IncludeItemTypes = new[] { typeof(Episode).Name };
  813. var result = _libraryManager.GetItemsResult(query);
  814. return ToResult(result);
  815. }
  816. private QueryResult<ServerItem> GetMovieFavorites(BaseItem parent, User user, InternalItemsQuery query)
  817. {
  818. query.Recursive = true;
  819. query.Parent = parent;
  820. query.SetUser(user);
  821. query.IsFavorite = true;
  822. query.IncludeItemTypes = new[] { typeof(Movie).Name };
  823. var result = _libraryManager.GetItemsResult(query);
  824. return ToResult(result);
  825. }
  826. private QueryResult<ServerItem> GetFavoriteAlbums(BaseItem parent, User user, InternalItemsQuery query)
  827. {
  828. query.Recursive = true;
  829. query.Parent = parent;
  830. query.SetUser(user);
  831. query.IsFavorite = true;
  832. query.IncludeItemTypes = new[] { typeof(MusicAlbum).Name };
  833. var result = _libraryManager.GetItemsResult(query);
  834. return ToResult(result);
  835. }
  836. private QueryResult<ServerItem> GetGenres(BaseItem parent, User user, InternalItemsQuery query)
  837. {
  838. var genresResult = _libraryManager.GetGenres(new InternalItemsQuery(user)
  839. {
  840. AncestorIds = new[] { parent.Id },
  841. StartIndex = query.StartIndex,
  842. Limit = query.Limit
  843. });
  844. var result = new QueryResult<BaseItem>
  845. {
  846. TotalRecordCount = genresResult.TotalRecordCount,
  847. Items = genresResult.Items.Select(i => i.Item1).ToArray()
  848. };
  849. return ToResult(result);
  850. }
  851. private QueryResult<ServerItem> GetMusicGenres(BaseItem parent, User user, InternalItemsQuery query)
  852. {
  853. var genresResult = _libraryManager.GetMusicGenres(new InternalItemsQuery(user)
  854. {
  855. AncestorIds = new[] { parent.Id },
  856. StartIndex = query.StartIndex,
  857. Limit = query.Limit
  858. });
  859. var result = new QueryResult<BaseItem>
  860. {
  861. TotalRecordCount = genresResult.TotalRecordCount,
  862. Items = genresResult.Items.Select(i => i.Item1).ToArray()
  863. };
  864. return ToResult(result);
  865. }
  866. private QueryResult<ServerItem> GetMusicAlbumArtists(BaseItem parent, User user, InternalItemsQuery query)
  867. {
  868. var artists = _libraryManager.GetAlbumArtists(new InternalItemsQuery(user)
  869. {
  870. AncestorIds = new[] { parent.Id },
  871. StartIndex = query.StartIndex,
  872. Limit = query.Limit
  873. });
  874. var result = new QueryResult<BaseItem>
  875. {
  876. TotalRecordCount = artists.TotalRecordCount,
  877. Items = artists.Items.Select(i => i.Item1).ToArray()
  878. };
  879. return ToResult(result);
  880. }
  881. private QueryResult<ServerItem> GetMusicArtists(BaseItem parent, User user, InternalItemsQuery query)
  882. {
  883. var artists = _libraryManager.GetArtists(new InternalItemsQuery(user)
  884. {
  885. AncestorIds = new[] { parent.Id },
  886. StartIndex = query.StartIndex,
  887. Limit = query.Limit
  888. });
  889. var result = new QueryResult<BaseItem>
  890. {
  891. TotalRecordCount = artists.TotalRecordCount,
  892. Items = artists.Items.Select(i => i.Item1).ToArray()
  893. };
  894. return ToResult(result);
  895. }
  896. private QueryResult<ServerItem> GetFavoriteArtists(BaseItem parent, User user, InternalItemsQuery query)
  897. {
  898. var artists = _libraryManager.GetArtists(new InternalItemsQuery(user)
  899. {
  900. AncestorIds = new[] { parent.Id },
  901. StartIndex = query.StartIndex,
  902. Limit = query.Limit,
  903. IsFavorite = true
  904. });
  905. var result = new QueryResult<BaseItem>
  906. {
  907. TotalRecordCount = artists.TotalRecordCount,
  908. Items = artists.Items.Select(i => i.Item1).ToArray()
  909. };
  910. return ToResult(result);
  911. }
  912. private QueryResult<ServerItem> GetMusicPlaylists(User user, InternalItemsQuery query)
  913. {
  914. query.Parent = null;
  915. query.IncludeItemTypes = new[] { nameof(Playlist) };
  916. query.SetUser(user);
  917. query.Recursive = true;
  918. var result = _libraryManager.GetItemsResult(query);
  919. return ToResult(result);
  920. }
  921. private QueryResult<ServerItem> GetMusicLatest(BaseItem parent, User user, InternalItemsQuery query)
  922. {
  923. query.OrderBy = Array.Empty<(string, SortOrder)>();
  924. var items = _userViewManager.GetLatestItems(new LatestItemsQuery
  925. {
  926. UserId = user.Id,
  927. Limit = 50,
  928. IncludeItemTypes = new[] { nameof(Audio) },
  929. ParentId = parent?.Id ?? Guid.Empty,
  930. GroupItems = true
  931. }, query.DtoOptions).Select(i => i.Item1 ?? i.Item2.FirstOrDefault()).Where(i => i != null).ToArray();
  932. return ToResult(items);
  933. }
  934. private QueryResult<ServerItem> GetNextUp(BaseItem parent, InternalItemsQuery query)
  935. {
  936. query.OrderBy = Array.Empty<(string, SortOrder)>();
  937. var result = _tvSeriesManager.GetNextUp(new NextUpQuery
  938. {
  939. Limit = query.Limit,
  940. StartIndex = query.StartIndex,
  941. UserId = query.User.Id
  942. }, new[] { parent }, query.DtoOptions);
  943. return ToResult(result);
  944. }
  945. private QueryResult<ServerItem> GetTvLatest(BaseItem parent, User user, InternalItemsQuery query)
  946. {
  947. query.OrderBy = Array.Empty<(string, SortOrder)>();
  948. var items = _userViewManager.GetLatestItems(new LatestItemsQuery
  949. {
  950. UserId = user.Id,
  951. Limit = 50,
  952. IncludeItemTypes = new[] { typeof(Episode).Name },
  953. ParentId = parent == null ? Guid.Empty : parent.Id,
  954. GroupItems = false
  955. }, query.DtoOptions).Select(i => i.Item1 ?? i.Item2.FirstOrDefault()).Where(i => i != null).ToArray();
  956. return ToResult(items);
  957. }
  958. private QueryResult<ServerItem> GetMovieLatest(BaseItem parent, User user, InternalItemsQuery query)
  959. {
  960. query.OrderBy = Array.Empty<(string, SortOrder)>();
  961. var items = _userViewManager.GetLatestItems(
  962. new LatestItemsQuery
  963. {
  964. UserId = user.Id,
  965. Limit = 50,
  966. IncludeItemTypes = new[] { nameof(Movie) },
  967. ParentId = parent?.Id ?? Guid.Empty,
  968. GroupItems = true
  969. }, query.DtoOptions).Select(i => i.Item1 ?? i.Item2.FirstOrDefault()).Where(i => i != null).ToArray();
  970. return ToResult(items);
  971. }
  972. private QueryResult<ServerItem> GetMusicArtistItems(BaseItem item, Guid parentId, User user, SortCriteria sort, int? startIndex, int? limit)
  973. {
  974. var query = new InternalItemsQuery(user)
  975. {
  976. Recursive = true,
  977. ParentId = parentId,
  978. ArtistIds = new[] { item.Id },
  979. IncludeItemTypes = new[] { typeof(MusicAlbum).Name },
  980. Limit = limit,
  981. StartIndex = startIndex,
  982. DtoOptions = GetDtoOptions()
  983. };
  984. SetSorting(query, sort, false);
  985. var result = _libraryManager.GetItemsResult(query);
  986. return ToResult(result);
  987. }
  988. private QueryResult<ServerItem> GetGenreItems(BaseItem item, Guid parentId, User user, SortCriteria sort, int? startIndex, int? limit)
  989. {
  990. var query = new InternalItemsQuery(user)
  991. {
  992. Recursive = true,
  993. ParentId = parentId,
  994. GenreIds = new[] { item.Id },
  995. IncludeItemTypes = new[]
  996. {
  997. nameof(Movie),
  998. nameof(Series)
  999. },
  1000. Limit = limit,
  1001. StartIndex = startIndex,
  1002. DtoOptions = GetDtoOptions()
  1003. };
  1004. SetSorting(query, sort, false);
  1005. var result = _libraryManager.GetItemsResult(query);
  1006. return ToResult(result);
  1007. }
  1008. private QueryResult<ServerItem> GetMusicGenreItems(BaseItem item, Guid parentId, User user, SortCriteria sort, int? startIndex, int? limit)
  1009. {
  1010. var query = new InternalItemsQuery(user)
  1011. {
  1012. Recursive = true,
  1013. ParentId = parentId,
  1014. GenreIds = new[] { item.Id },
  1015. IncludeItemTypes = new[] { typeof(MusicAlbum).Name },
  1016. Limit = limit,
  1017. StartIndex = startIndex,
  1018. DtoOptions = GetDtoOptions()
  1019. };
  1020. SetSorting(query, sort, false);
  1021. var result = _libraryManager.GetItemsResult(query);
  1022. return ToResult(result);
  1023. }
  1024. private QueryResult<ServerItem> ToResult(BaseItem[] result)
  1025. {
  1026. var serverItems = result
  1027. .Select(i => new ServerItem(i))
  1028. .ToArray();
  1029. return new QueryResult<ServerItem>
  1030. {
  1031. TotalRecordCount = result.Length,
  1032. Items = serverItems
  1033. };
  1034. }
  1035. private QueryResult<ServerItem> ToResult(QueryResult<BaseItem> result)
  1036. {
  1037. var serverItems = result
  1038. .Items
  1039. .Select(i => new ServerItem(i))
  1040. .ToArray();
  1041. return new QueryResult<ServerItem>
  1042. {
  1043. TotalRecordCount = result.TotalRecordCount,
  1044. Items = serverItems
  1045. };
  1046. }
  1047. private void SetSorting(InternalItemsQuery query, SortCriteria sort, bool isPreSorted)
  1048. {
  1049. if (isPreSorted)
  1050. {
  1051. query.OrderBy = Array.Empty<(string, SortOrder)>();
  1052. }
  1053. else
  1054. {
  1055. query.OrderBy = new[] { (ItemSortBy.SortName, sort.SortOrder) };
  1056. }
  1057. }
  1058. private QueryResult<ServerItem> ApplyPaging(QueryResult<ServerItem> result, int? startIndex, int? limit)
  1059. {
  1060. result.Items = result.Items.Skip(startIndex ?? 0).Take(limit ?? int.MaxValue).ToArray();
  1061. return result;
  1062. }
  1063. private ServerItem GetItemFromObjectId(string id)
  1064. {
  1065. return DidlBuilder.IsIdRoot(id)
  1066. ? new ServerItem(_libraryManager.GetUserRootFolder())
  1067. : ParseItemId(id);
  1068. }
  1069. private ServerItem ParseItemId(string id)
  1070. {
  1071. StubType? stubType = null;
  1072. // After using PlayTo, MediaMonkey sends a request to the server trying to get item info
  1073. const string ParamsSrch = "Params=";
  1074. var paramsIndex = id.IndexOf(ParamsSrch, StringComparison.OrdinalIgnoreCase);
  1075. if (paramsIndex != -1)
  1076. {
  1077. id = id.Substring(paramsIndex + ParamsSrch.Length);
  1078. var parts = id.Split(';');
  1079. id = parts[23];
  1080. }
  1081. var enumNames = Enum.GetNames(typeof(StubType));
  1082. foreach (var name in enumNames)
  1083. {
  1084. if (id.StartsWith(name + "_", StringComparison.OrdinalIgnoreCase))
  1085. {
  1086. stubType = (StubType)Enum.Parse(typeof(StubType), name, true);
  1087. id = id.Split(new[] { '_' }, 2)[1];
  1088. break;
  1089. }
  1090. }
  1091. if (Guid.TryParse(id, out var itemId))
  1092. {
  1093. var item = _libraryManager.GetItemById(itemId);
  1094. return new ServerItem(item)
  1095. {
  1096. StubType = stubType
  1097. };
  1098. }
  1099. Logger.LogError("Error parsing item Id: {id}. Returning user root folder.", id);
  1100. return new ServerItem(_libraryManager.GetUserRootFolder());
  1101. }
  1102. }
  1103. internal class ServerItem
  1104. {
  1105. public BaseItem Item { get; set; }
  1106. public StubType? StubType { get; set; }
  1107. public ServerItem(BaseItem item)
  1108. {
  1109. Item = item;
  1110. if (item is IItemByName && !(item is Folder))
  1111. {
  1112. StubType = Dlna.ContentDirectory.StubType.Folder;
  1113. }
  1114. }
  1115. }
  1116. public enum StubType
  1117. {
  1118. Folder = 0,
  1119. Latest = 2,
  1120. Playlists = 3,
  1121. Albums = 4,
  1122. AlbumArtists = 5,
  1123. Artists = 6,
  1124. Songs = 7,
  1125. Genres = 8,
  1126. FavoriteSongs = 9,
  1127. FavoriteArtists = 10,
  1128. FavoriteAlbums = 11,
  1129. ContinueWatching = 12,
  1130. Movies = 13,
  1131. Collections = 14,
  1132. Favorites = 15,
  1133. NextUp = 16,
  1134. Series = 17,
  1135. FavoriteSeries = 18,
  1136. FavoriteEpisodes = 19
  1137. }
  1138. }