jellyfin/Emby.Dlna/ContentDirectory/ControlHandler.cs

1396 lines
48 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
2016-10-29 22:22:20 +00:00
using System.Collections.Generic;
using System.Globalization;
2020-01-28 12:19:25 +00:00
using System.IO;
2016-10-29 22:22:20 +00:00
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml;
2019-01-13 19:16:19 +00:00
using Emby.Dlna.Didl;
using Emby.Dlna.Service;
2020-05-20 17:07:53 +00:00
using Jellyfin.Data.Entities;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Drawing;
2017-05-21 07:25:49 +00:00
using MediaBrowser.Controller.Dto;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Controller.Entities;
2016-12-20 07:31:30 +00:00
using MediaBrowser.Controller.Entities.Audio;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
2016-10-29 22:22:20 +00:00
using MediaBrowser.Controller.MediaEncoding;
2017-07-22 23:00:48 +00:00
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Controller.TV;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Entities;
2016-10-29 22:22:20 +00:00
using MediaBrowser.Model.Globalization;
2019-01-13 19:16:19 +00:00
using MediaBrowser.Model.Querying;
using Microsoft.Extensions.Logging;
2020-05-20 17:07:53 +00:00
using Book = MediaBrowser.Controller.Entities.Book;
using Episode = MediaBrowser.Controller.Entities.TV.Episode;
using Genre = MediaBrowser.Controller.Entities.Genre;
using Movie = MediaBrowser.Controller.Entities.Movies.Movie;
using MusicAlbum = MediaBrowser.Controller.Entities.Audio.MusicAlbum;
using Series = MediaBrowser.Controller.Entities.TV.Series;
2016-10-29 22:22:20 +00:00
2016-10-29 22:34:54 +00:00
namespace Emby.Dlna.ContentDirectory
2016-10-29 22:22:20 +00:00
{
public class ControlHandler : BaseControlHandler
{
private readonly ILibraryManager _libraryManager;
private readonly IUserDataManager _userDataManager;
private readonly IServerConfigurationManager _config;
2020-05-20 17:07:53 +00:00
private readonly User _user;
2016-10-29 22:22:20 +00:00
private readonly IUserViewManager _userViewManager;
private readonly ITVSeriesManager _tvSeriesManager;
2016-10-29 22:22:20 +00:00
private const string NS_DC = "http://purl.org/dc/elements/1.1/";
private const string NS_DIDL = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
private const string NS_DLNA = "urn:schemas-dlna-org:metadata-1-0/";
private const string NS_UPNP = "urn:schemas-upnp-org:metadata-1-0/upnp/";
private readonly int _systemUpdateId;
private readonly DidlBuilder _didlBuilder;
private readonly DeviceProfile _profile;
public ControlHandler(
ILogger logger,
ILibraryManager libraryManager,
DeviceProfile profile,
string serverAddress,
string accessToken,
IImageProcessor imageProcessor,
IUserDataManager userDataManager,
2020-05-20 17:07:53 +00:00
User user,
2020-01-28 20:54:51 +00:00
int systemUpdateId,
IServerConfigurationManager config,
ILocalizationManager localization,
IMediaSourceManager mediaSourceManager,
IUserViewManager userViewManager,
IMediaEncoder mediaEncoder,
ITVSeriesManager tvSeriesManager)
: base(config, logger)
2016-10-29 22:22:20 +00:00
{
_libraryManager = libraryManager;
_userDataManager = userDataManager;
_user = user;
_systemUpdateId = systemUpdateId;
_userViewManager = userViewManager;
_tvSeriesManager = tvSeriesManager;
2016-10-29 22:22:20 +00:00
_profile = profile;
_config = config;
2020-04-02 14:49:58 +00:00
_didlBuilder = new DidlBuilder(
profile,
user,
imageProcessor,
serverAddress,
accessToken,
userDataManager,
localization,
mediaSourceManager,
Logger,
mediaEncoder,
libraryManager);
2016-10-29 22:22:20 +00:00
}
/// <inheritdoc />
protected override void WriteResult(string methodName, IDictionary<string, string> methodParams, XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
const string DeviceId = "test";
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "GetSearchCapabilities", StringComparison.OrdinalIgnoreCase))
{
HandleGetSearchCapabilities(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "GetSortCapabilities", StringComparison.OrdinalIgnoreCase))
{
HandleGetSortCapabilities(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "GetSortExtensionCapabilities", StringComparison.OrdinalIgnoreCase))
{
HandleGetSortExtensionCapabilities(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "GetSystemUpdateID", StringComparison.OrdinalIgnoreCase))
{
HandleGetSystemUpdateID(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "Browse", StringComparison.OrdinalIgnoreCase))
{
HandleBrowse(xmlWriter, methodParams, DeviceId);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "X_GetFeatureList", StringComparison.OrdinalIgnoreCase))
{
HandleXGetFeatureList(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "GetFeatureList", StringComparison.OrdinalIgnoreCase))
{
HandleGetFeatureList(xmlWriter);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "X_SetBookmark", StringComparison.OrdinalIgnoreCase))
{
HandleXSetBookmark(methodParams);
return;
}
2016-10-29 22:22:20 +00:00
if (string.Equals(methodName, "Search", StringComparison.OrdinalIgnoreCase))
{
HandleSearch(xmlWriter, methodParams, DeviceId);
return;
}
2016-10-29 22:22:20 +00:00
2016-12-14 20:58:55 +00:00
if (string.Equals(methodName, "X_BrowseByLetter", StringComparison.OrdinalIgnoreCase))
{
HandleXBrowseByLetter(xmlWriter, methodParams, DeviceId);
return;
}
2016-12-14 20:58:55 +00:00
2016-10-29 22:22:20 +00:00
throw new ResourceNotFoundException("Unexpected control request name: " + methodName);
}
private void HandleXSetBookmark(IDictionary<string, string> sparams)
2016-10-29 22:22:20 +00:00
{
var id = sparams["ObjectID"];
2020-04-02 14:49:58 +00:00
var serverItem = GetItemFromObjectId(id);
2016-10-29 22:22:20 +00:00
var item = serverItem.Item;
2020-01-26 16:37:47 +00:00
var newbookmark = int.Parse(sparams["PosSecond"], CultureInfo.InvariantCulture);
2016-10-29 22:22:20 +00:00
var userdata = _userDataManager.GetUserData(_user, item);
2016-10-29 22:22:20 +00:00
userdata.PlaybackPositionTicks = TimeSpan.FromSeconds(newbookmark).Ticks;
_userDataManager.SaveUserData(_user, item, userdata, UserDataSaveReason.TogglePlayed,
2016-10-29 22:22:20 +00:00
CancellationToken.None);
}
private void HandleGetSearchCapabilities(XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
xmlWriter.WriteElementString(
"SearchCaps",
"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");
2016-10-29 22:22:20 +00:00
}
private void HandleGetSortCapabilities(XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
xmlWriter.WriteElementString(
"SortCaps",
"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");
2016-10-29 22:22:20 +00:00
}
private void HandleGetSortExtensionCapabilities(XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
xmlWriter.WriteElementString(
"SortExtensionCaps",
"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");
2016-10-29 22:22:20 +00:00
}
private void HandleGetSystemUpdateID(XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
xmlWriter.WriteElementString("Id", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
2016-10-29 22:22:20 +00:00
}
private void HandleGetFeatureList(XmlWriter xmlWriter)
2016-10-29 22:22:20 +00:00
{
xmlWriter.WriteElementString("FeatureList", WriteFeatureListXml());
2016-10-29 22:22:20 +00:00
}
private void HandleXGetFeatureList(XmlWriter xmlWriter)
=> HandleGetFeatureList(xmlWriter);
2016-10-29 22:22:20 +00:00
private string WriteFeatureListXml()
2016-10-29 22:22:20 +00:00
{
// TODO: clean this up
2016-10-29 22:22:20 +00:00
var builder = new StringBuilder();
builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
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\">");
builder.Append("<Feature name=\"samsung.com_BASICVIEW\" version=\"1\">");
builder.Append("<container id=\"I\" type=\"object.item.imageItem\"/>");
builder.Append("<container id=\"A\" type=\"object.item.audioItem\"/>");
builder.Append("<container id=\"V\" type=\"object.item.videoItem\"/>");
builder.Append("</Feature>");
builder.Append("</Features>");
return builder.ToString();
}
2016-12-04 21:30:38 +00:00
public string GetValueOrDefault(IDictionary<string, string> sparams, string key, string defaultValue)
{
if (sparams.TryGetValue(key, out string val))
2016-12-04 21:30:38 +00:00
{
return val;
}
return defaultValue;
}
private void HandleBrowse(XmlWriter xmlWriter, IDictionary<string, string> sparams, string deviceId)
2016-10-29 22:22:20 +00:00
{
var id = sparams["ObjectID"];
var flag = sparams["BrowseFlag"];
2016-12-04 21:30:38 +00:00
var filter = new Filter(GetValueOrDefault(sparams, "Filter", "*"));
var sortCriteria = new SortCriteria(GetValueOrDefault(sparams, "SortCriteria", ""));
2016-10-29 22:22:20 +00:00
var provided = 0;
// Default to null instead of 0
// Upnp inspector sends 0 as requestedCount when it wants everything
int? requestedCount = null;
int? start = 0;
if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out var requestedVal) && requestedVal > 0)
2016-10-29 22:22:20 +00:00
{
requestedCount = requestedVal;
}
if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out var startVal) && startVal > 0)
2016-10-29 22:22:20 +00:00
{
start = startVal;
}
int totalCount;
2020-01-28 12:19:25 +00:00
using (StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8))
2016-10-29 22:22:20 +00:00
{
2020-01-29 16:30:27 +00:00
var settings = new XmlWriterSettings()
{
Encoding = Encoding.UTF8,
CloseOutput = false,
OmitXmlDeclaration = true,
ConformanceLevel = ConformanceLevel.Fragment
};
2016-10-29 22:22:20 +00:00
2020-01-29 16:30:27 +00:00
using (var writer = XmlWriter.Create(builder, settings))
2016-10-29 22:22:20 +00:00
{
2020-01-29 16:30:27 +00:00
writer.WriteStartElement(string.Empty, "DIDL-Lite", NS_DIDL);
2016-10-29 22:22:20 +00:00
2020-01-29 16:30:27 +00:00
writer.WriteAttributeString("xmlns", "dc", null, NS_DC);
writer.WriteAttributeString("xmlns", "dlna", null, NS_DLNA);
writer.WriteAttributeString("xmlns", "upnp", null, NS_UPNP);
2016-10-29 22:22:20 +00:00
2020-01-29 16:30:27 +00:00
DidlBuilder.WriteXmlRootAttributes(_profile, writer);
2020-01-28 12:19:25 +00:00
2020-04-02 14:49:58 +00:00
var serverItem = GetItemFromObjectId(id);
2020-01-29 16:30:27 +00:00
var item = serverItem.Item;
2016-10-29 22:22:20 +00:00
2020-01-28 12:19:25 +00:00
2020-01-29 16:30:27 +00:00
if (string.Equals(flag, "BrowseMetadata", StringComparison.Ordinal))
2020-01-28 12:19:25 +00:00
{
2020-01-29 16:30:27 +00:00
totalCount = 1;
2020-01-28 12:19:25 +00:00
2020-01-29 16:30:27 +00:00
if (item.IsDisplayedAsFolder || serverItem.StubType.HasValue)
2020-01-28 12:19:25 +00:00
{
2020-01-29 16:30:27 +00:00
var childrenResult = GetUserItems(item, serverItem.StubType, _user, sortCriteria, start, requestedCount);
2020-01-28 12:19:25 +00:00
2020-01-29 16:30:27 +00:00
_didlBuilder.WriteFolderElement(writer, item, serverItem.StubType, null, childrenResult.TotalRecordCount, filter, id);
2020-01-28 12:19:25 +00:00
}
else
{
2020-01-29 16:30:27 +00:00
var dlnaOptions = _config.GetDlnaConfiguration();
2020-04-02 14:49:58 +00:00
_didlBuilder.WriteItemElement(writer, item, _user, null, null, deviceId, filter);
2020-01-28 12:19:25 +00:00
}
2020-01-29 16:30:27 +00:00
provided++;
2020-01-28 12:19:25 +00:00
}
2020-01-29 16:30:27 +00:00
else
{
var childrenResult = GetUserItems(item, serverItem.StubType, _user, sortCriteria, start, requestedCount);
totalCount = childrenResult.TotalRecordCount;
provided = childrenResult.Items.Count;
2020-01-28 12:19:25 +00:00
2020-01-29 16:30:27 +00:00
var dlnaOptions = _config.GetDlnaConfiguration();
foreach (var i in childrenResult.Items)
{
var childItem = i.Item;
var displayStubType = i.StubType;
if (childItem.IsDisplayedAsFolder || displayStubType.HasValue)
{
var childCount = GetUserItems(childItem, displayStubType, _user, sortCriteria, null, 0)
.TotalRecordCount;
_didlBuilder.WriteFolderElement(writer, childItem, displayStubType, item, childCount, filter);
}
else
{
2020-04-02 14:49:58 +00:00
_didlBuilder.WriteItemElement(writer, childItem, _user, item, serverItem.StubType, deviceId, filter);
2020-01-29 16:30:27 +00:00
}
}
}
writer.WriteFullEndElement();
}
2020-01-28 12:19:25 +00:00
xmlWriter.WriteElementString("Result", builder.ToString());
}
2016-10-29 22:22:20 +00:00
2020-01-26 16:37:47 +00:00
xmlWriter.WriteElementString("NumberReturned", provided.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteElementString("TotalMatches", totalCount.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteElementString("UpdateID", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
2016-10-29 22:22:20 +00:00
}
private void HandleXBrowseByLetter(XmlWriter xmlWriter, IDictionary<string, string> sparams, string deviceId)
2016-12-14 20:58:55 +00:00
{
// TODO: Implement this method
HandleSearch(xmlWriter, sparams, deviceId);
2016-12-14 20:58:55 +00:00
}
private void HandleSearch(XmlWriter xmlWriter, IDictionary<string, string> sparams, string deviceId)
2016-10-29 22:22:20 +00:00
{
2016-12-04 21:30:38 +00:00
var searchCriteria = new SearchCriteria(GetValueOrDefault(sparams, "SearchCriteria", ""));
var sortCriteria = new SortCriteria(GetValueOrDefault(sparams, "SortCriteria", ""));
var filter = new Filter(GetValueOrDefault(sparams, "Filter", "*"));
2016-10-29 22:22:20 +00:00
// sort example: dc:title, dc:date
// Default to null instead of 0
// Upnp inspector sends 0 as requestedCount when it wants everything
int? requestedCount = null;
int? start = 0;
if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out var requestedVal) && requestedVal > 0)
2016-10-29 22:22:20 +00:00
{
requestedCount = requestedVal;
}
if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out var startVal) && startVal > 0)
2016-10-29 22:22:20 +00:00
{
start = startVal;
}
2020-01-28 12:19:25 +00:00
QueryResult<BaseItem> childrenResult;
2016-10-29 22:22:20 +00:00
2020-01-28 12:19:25 +00:00
using (StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8))
{
2020-01-29 16:30:27 +00:00
var settings = new XmlWriterSettings()
{
Encoding = Encoding.UTF8,
CloseOutput = false,
OmitXmlDeclaration = true,
ConformanceLevel = ConformanceLevel.Fragment
};
2016-10-29 22:22:20 +00:00
2020-01-29 16:30:27 +00:00
using (var writer = XmlWriter.Create(builder, settings))
{
writer.WriteStartElement(string.Empty, "DIDL-Lite", NS_DIDL);
2016-10-29 22:22:20 +00:00
2020-01-29 16:30:27 +00:00
writer.WriteAttributeString("xmlns", "dc", null, NS_DC);
writer.WriteAttributeString("xmlns", "dlna", null, NS_DLNA);
writer.WriteAttributeString("xmlns", "upnp", null, NS_UPNP);
2016-10-29 22:22:20 +00:00
2020-01-29 16:30:27 +00:00
DidlBuilder.WriteXmlRootAttributes(_profile, writer);
2016-10-29 22:22:20 +00:00
2020-04-02 14:49:58 +00:00
var serverItem = GetItemFromObjectId(sparams["ContainerID"]);
2016-10-29 22:22:20 +00:00
2020-01-29 16:30:27 +00:00
var item = serverItem.Item;
2016-11-04 08:31:05 +00:00
2020-01-29 16:30:27 +00:00
childrenResult = GetChildrenSorted(item, _user, searchCriteria, sortCriteria, start, requestedCount);
2018-09-12 17:26:21 +00:00
2020-01-29 16:30:27 +00:00
var dlnaOptions = _config.GetDlnaConfiguration();
2016-11-04 08:31:05 +00:00
2020-01-29 16:30:27 +00:00
foreach (var i in childrenResult.Items)
2020-01-28 12:19:25 +00:00
{
2020-01-29 16:30:27 +00:00
if (i.IsDisplayedAsFolder)
{
var childCount = GetChildrenSorted(i, _user, searchCriteria, sortCriteria, null, 0)
.TotalRecordCount;
_didlBuilder.WriteFolderElement(writer, i, null, item, childCount, filter);
}
else
{
2020-04-02 14:49:58 +00:00
_didlBuilder.WriteItemElement(writer, i, _user, item, serverItem.StubType, deviceId, filter);
2020-01-29 16:30:27 +00:00
}
2020-01-28 12:19:25 +00:00
}
2016-10-29 22:22:20 +00:00
2020-01-29 16:30:27 +00:00
writer.WriteFullEndElement();
}
2020-01-28 12:19:25 +00:00
xmlWriter.WriteElementString("Result", builder.ToString());
}
2016-10-29 22:22:20 +00:00
2020-01-26 16:37:47 +00:00
xmlWriter.WriteElementString("NumberReturned", childrenResult.Items.Count.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteElementString("TotalMatches", childrenResult.TotalRecordCount.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteElementString("UpdateID", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
2016-10-29 22:22:20 +00:00
}
2020-05-20 17:07:53 +00:00
private QueryResult<BaseItem> GetChildrenSorted(BaseItem item, User user, SearchCriteria search, SortCriteria sort, int? startIndex, int? limit)
2016-10-29 22:22:20 +00:00
{
var folder = (Folder)item;
2020-01-26 16:37:47 +00:00
var sortOrders = folder.IsPreSorted
? Array.Empty<(string, SortOrder)>()
: new[] { (ItemSortBy.SortName, sort.SortOrder) };
2016-10-29 22:22:20 +00:00
string[] mediaTypes = Array.Empty<string>();
2016-10-29 22:22:20 +00:00
bool? isFolder = null;
if (search.SearchType == SearchType.Audio)
{
mediaTypes = new[] { MediaType.Audio };
2016-10-29 22:22:20 +00:00
isFolder = false;
}
else if (search.SearchType == SearchType.Video)
{
mediaTypes = new[] { MediaType.Video };
2016-10-29 22:22:20 +00:00
isFolder = false;
}
else if (search.SearchType == SearchType.Image)
{
mediaTypes = new[] { MediaType.Photo };
2016-10-29 22:22:20 +00:00
isFolder = false;
}
else if (search.SearchType == SearchType.Playlist)
{
//items = items.OfType<Playlist>();
isFolder = true;
}
else if (search.SearchType == SearchType.MusicAlbum)
{
//items = items.OfType<MusicAlbum>();
isFolder = true;
}
return folder.GetItems(new InternalItemsQuery
{
Limit = limit,
StartIndex = startIndex,
2019-10-20 14:08:40 +00:00
OrderBy = sortOrders,
2016-10-29 22:22:20 +00:00
User = user,
Recursive = true,
IsMissing = false,
ExcludeItemTypes = new[] { typeof(Book).Name },
2016-10-29 22:22:20 +00:00
IsFolder = isFolder,
MediaTypes = mediaTypes,
2017-05-21 07:25:49 +00:00
DtoOptions = GetDtoOptions()
2016-10-29 22:22:20 +00:00
});
}
2017-05-21 07:25:49 +00:00
private DtoOptions GetDtoOptions()
{
return new DtoOptions(true);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetUserItems(BaseItem item, StubType? stubType, User user, SortCriteria sort, int? startIndex, int? limit)
2016-10-29 22:22:20 +00:00
{
2016-12-20 19:59:25 +00:00
if (item is MusicGenre)
{
2018-09-12 17:26:21 +00:00
return GetMusicGenreItems(item, Guid.Empty, user, sort, startIndex, limit);
2016-12-20 19:59:25 +00:00
}
if (item is MusicArtist)
{
2018-09-12 17:26:21 +00:00
return GetMusicArtistItems(item, Guid.Empty, user, sort, startIndex, limit);
2016-12-20 19:59:25 +00:00
}
2017-08-19 19:43:35 +00:00
if (item is Genre)
{
2018-09-12 17:26:21 +00:00
return GetGenreItems(item, Guid.Empty, user, sort, startIndex, limit);
2017-08-19 19:43:35 +00:00
}
2019-01-26 17:21:41 +00:00
if ((!stubType.HasValue || stubType.Value != StubType.Folder)
&& item is IHasCollectionType collectionFolder)
2017-07-22 23:00:48 +00:00
{
2019-01-26 17:21:41 +00:00
if (string.Equals(CollectionType.Music, collectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
2017-11-20 17:38:36 +00:00
{
return GetMusicFolders(item, user, stubType, sort, startIndex, limit);
}
2019-01-26 17:21:41 +00:00
else if (string.Equals(CollectionType.Movies, collectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
2017-11-20 17:38:36 +00:00
{
return GetMovieFolders(item, user, stubType, sort, startIndex, limit);
}
2019-01-26 17:21:41 +00:00
else if (string.Equals(CollectionType.TvShows, collectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
2017-11-20 17:38:36 +00:00
{
return GetTvFolders(item, user, stubType, sort, startIndex, limit);
}
2019-01-26 17:21:41 +00:00
else if (string.Equals(CollectionType.Folders, collectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
2017-11-20 17:38:36 +00:00
{
2020-04-02 14:49:58 +00:00
return GetFolders(user, startIndex, limit);
2017-11-20 17:38:36 +00:00
}
2019-01-26 17:21:41 +00:00
else if (string.Equals(CollectionType.LiveTv, collectionFolder.CollectionType, StringComparison.OrdinalIgnoreCase))
2018-09-12 17:26:21 +00:00
{
2020-04-02 14:49:58 +00:00
return GetLiveTvChannels(user, sort, startIndex, limit);
2018-09-12 17:26:21 +00:00
}
}
2017-07-22 23:00:48 +00:00
2016-10-29 22:22:20 +00:00
if (stubType.HasValue)
{
2017-11-20 17:38:36 +00:00
if (stubType.Value != StubType.Folder)
{
return ApplyPaging(new QueryResult<ServerItem>(), startIndex, limit);
}
2016-10-29 22:22:20 +00:00
}
2016-12-20 07:31:30 +00:00
var folder = (Folder)item;
2018-09-12 17:26:21 +00:00
var query = new InternalItemsQuery(user)
2016-10-29 22:22:20 +00:00
{
Limit = limit,
StartIndex = startIndex,
2017-08-13 02:09:07 +00:00
IsVirtualItem = false,
ExcludeItemTypes = new[] { typeof(Book).Name },
2017-05-21 07:25:49 +00:00
IsPlaceHolder = false,
DtoOptions = GetDtoOptions()
2016-12-20 07:31:30 +00:00
};
SetSorting(query, sort, folder.IsPreSorted);
2017-05-26 06:48:54 +00:00
var queryResult = folder.GetItems(query);
2016-10-29 22:22:20 +00:00
2016-12-20 07:31:30 +00:00
return ToResult(queryResult);
}
2016-10-29 22:22:20 +00:00
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetLiveTvChannels(User user, SortCriteria sort, int? startIndex, int? limit)
2018-09-12 17:26:21 +00:00
{
var query = new InternalItemsQuery(user)
{
StartIndex = startIndex,
Limit = limit,
};
query.IncludeItemTypes = new[] { typeof(LiveTvChannel).Name };
SetSorting(query, sort, false);
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicFolders(BaseItem item, User user, StubType? stubType, SortCriteria sort, int? startIndex, int? limit)
2017-07-22 23:00:48 +00:00
{
var query = new InternalItemsQuery(user)
{
StartIndex = startIndex,
Limit = limit
};
SetSorting(query, sort, false);
if (stubType.HasValue && stubType.Value == StubType.Latest)
{
return GetMusicLatest(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Playlists)
{
2020-04-02 14:49:58 +00:00
return GetMusicPlaylists(user, query);
2017-07-22 23:00:48 +00:00
}
if (stubType.HasValue && stubType.Value == StubType.Albums)
{
return GetMusicAlbums(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Artists)
{
return GetMusicArtists(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.AlbumArtists)
{
return GetMusicAlbumArtists(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.FavoriteAlbums)
{
return GetFavoriteAlbums(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.FavoriteArtists)
{
return GetFavoriteArtists(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.FavoriteSongs)
{
return GetFavoriteSongs(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Songs)
{
return GetMusicSongs(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Genres)
{
return GetMusicGenres(item, user, query);
}
var list = new List<ServerItem>();
list.Add(new ServerItem(item)
{
StubType = StubType.Latest
});
list.Add(new ServerItem(item)
{
StubType = StubType.Playlists
});
list.Add(new ServerItem(item)
{
StubType = StubType.Albums
});
list.Add(new ServerItem(item)
{
StubType = StubType.AlbumArtists
});
list.Add(new ServerItem(item)
{
StubType = StubType.Artists
});
list.Add(new ServerItem(item)
{
StubType = StubType.Songs
});
list.Add(new ServerItem(item)
{
StubType = StubType.Genres
});
list.Add(new ServerItem(item)
{
StubType = StubType.FavoriteArtists
});
list.Add(new ServerItem(item)
{
StubType = StubType.FavoriteAlbums
});
list.Add(new ServerItem(item)
{
StubType = StubType.FavoriteSongs
});
return new QueryResult<ServerItem>
{
Items = list,
2017-07-22 23:00:48 +00:00
TotalRecordCount = list.Count
};
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMovieFolders(BaseItem item, User user, StubType? stubType, SortCriteria sort, int? startIndex, int? limit)
{
var query = new InternalItemsQuery(user)
{
StartIndex = startIndex,
Limit = limit
};
SetSorting(query, sort, false);
if (stubType.HasValue && stubType.Value == StubType.ContinueWatching)
{
return GetMovieContinueWatching(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Latest)
{
return GetMovieLatest(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Movies)
{
return GetMovieMovies(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Collections)
{
2020-04-02 14:49:58 +00:00
return GetMovieCollections(user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Favorites)
{
return GetMovieFavorites(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Genres)
{
return GetGenres(item, user, query);
}
2020-05-13 02:10:35 +00:00
var array = new[]
{
2020-04-02 14:49:58 +00:00
new ServerItem(item)
{
StubType = StubType.ContinueWatching
},
new ServerItem(item)
{
StubType = StubType.Latest
},
new ServerItem(item)
{
StubType = StubType.Movies
},
new ServerItem(item)
{
StubType = StubType.Collections
},
new ServerItem(item)
{
StubType = StubType.Favorites
},
new ServerItem(item)
{
StubType = StubType.Genres
}
};
return new QueryResult<ServerItem>
{
2020-04-02 14:49:58 +00:00
Items = array,
TotalRecordCount = array.Length
};
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetFolders(User user, int? startIndex, int? limit)
2017-11-20 17:38:36 +00:00
{
2018-09-12 17:26:21 +00:00
var folders = _libraryManager.GetUserRootFolder().GetChildren(user, true)
2017-11-20 17:38:36 +00:00
.OrderBy(i => i.SortName)
.Select(i => new ServerItem(i)
{
StubType = StubType.Folder
})
.ToArray();
return ApplyPaging(new QueryResult<ServerItem>
2017-11-20 17:38:36 +00:00
{
Items = folders,
TotalRecordCount = folders.Length
}, startIndex, limit);
2017-11-20 17:38:36 +00:00
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetTvFolders(BaseItem item, User user, StubType? stubType, SortCriteria sort, int? startIndex, int? limit)
{
var query = new InternalItemsQuery(user)
{
StartIndex = startIndex,
Limit = limit
};
SetSorting(query, sort, false);
if (stubType.HasValue && stubType.Value == StubType.ContinueWatching)
{
return GetMovieContinueWatching(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.NextUp)
{
2020-04-02 14:49:58 +00:00
return GetNextUp(item, query);
}
if (stubType.HasValue && stubType.Value == StubType.Latest)
{
return GetTvLatest(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Series)
{
return GetSeries(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.FavoriteSeries)
{
return GetFavoriteSeries(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.FavoriteEpisodes)
{
return GetFavoriteEpisodes(item, user, query);
}
if (stubType.HasValue && stubType.Value == StubType.Genres)
{
return GetGenres(item, user, query);
}
var list = new List<ServerItem>();
list.Add(new ServerItem(item)
{
StubType = StubType.ContinueWatching
});
list.Add(new ServerItem(item)
{
StubType = StubType.NextUp
});
list.Add(new ServerItem(item)
{
StubType = StubType.Latest
});
list.Add(new ServerItem(item)
{
StubType = StubType.Series
});
list.Add(new ServerItem(item)
{
StubType = StubType.FavoriteSeries
});
list.Add(new ServerItem(item)
{
StubType = StubType.FavoriteEpisodes
});
list.Add(new ServerItem(item)
{
StubType = StubType.Genres
});
return new QueryResult<ServerItem>
{
Items = list,
TotalRecordCount = list.Count
};
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMovieContinueWatching(BaseItem parent, User user, InternalItemsQuery query)
{
query.Recursive = true;
query.Parent = parent;
query.SetUser(user);
2019-10-20 14:08:40 +00:00
query.OrderBy = new[]
{
2019-10-20 14:08:40 +00:00
(ItemSortBy.DatePlayed, SortOrder.Descending),
(ItemSortBy.SortName, SortOrder.Ascending)
};
query.IsResumable = true;
query.Limit = 10;
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetSeries(BaseItem parent, User user, InternalItemsQuery query)
{
query.Recursive = true;
query.Parent = parent;
query.SetUser(user);
query.IncludeItemTypes = new[] { typeof(Series).Name };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMovieMovies(BaseItem parent, User user, InternalItemsQuery query)
{
query.Recursive = true;
query.Parent = parent;
query.SetUser(user);
query.IncludeItemTypes = new[] { typeof(Movie).Name };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMovieCollections(User user, InternalItemsQuery query)
{
query.Recursive = true;
//query.Parent = parent;
query.SetUser(user);
query.IncludeItemTypes = new[] { typeof(BoxSet).Name };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicAlbums(BaseItem parent, User user, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
query.Recursive = true;
query.Parent = parent;
query.SetUser(user);
query.IncludeItemTypes = new[] { typeof(MusicAlbum).Name };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicSongs(BaseItem parent, User user, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
query.Recursive = true;
query.Parent = parent;
query.SetUser(user);
query.IncludeItemTypes = new[] { typeof(Audio).Name };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetFavoriteSongs(BaseItem parent, User user, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
query.Recursive = true;
query.Parent = parent;
query.SetUser(user);
query.IsFavorite = true;
query.IncludeItemTypes = new[] { typeof(Audio).Name };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetFavoriteSeries(BaseItem parent, User user, InternalItemsQuery query)
{
query.Recursive = true;
query.Parent = parent;
query.SetUser(user);
query.IsFavorite = true;
query.IncludeItemTypes = new[] { typeof(Series).Name };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetFavoriteEpisodes(BaseItem parent, User user, InternalItemsQuery query)
{
query.Recursive = true;
query.Parent = parent;
query.SetUser(user);
query.IsFavorite = true;
query.IncludeItemTypes = new[] { typeof(Episode).Name };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMovieFavorites(BaseItem parent, User user, InternalItemsQuery query)
{
query.Recursive = true;
query.Parent = parent;
query.SetUser(user);
query.IsFavorite = true;
query.IncludeItemTypes = new[] { typeof(Movie).Name };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetFavoriteAlbums(BaseItem parent, User user, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
query.Recursive = true;
query.Parent = parent;
query.SetUser(user);
query.IsFavorite = true;
query.IncludeItemTypes = new[] { typeof(MusicAlbum).Name };
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetGenres(BaseItem parent, User user, InternalItemsQuery query)
{
var genresResult = _libraryManager.GetGenres(new InternalItemsQuery(user)
{
2018-09-12 17:26:21 +00:00
AncestorIds = new[] { parent.Id },
StartIndex = query.StartIndex,
Limit = query.Limit
});
var result = new QueryResult<BaseItem>
{
TotalRecordCount = genresResult.TotalRecordCount,
2018-12-28 15:48:26 +00:00
Items = genresResult.Items.Select(i => i.Item1).ToArray()
};
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicGenres(BaseItem parent, User user, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
var genresResult = _libraryManager.GetMusicGenres(new InternalItemsQuery(user)
{
2018-09-12 17:26:21 +00:00
AncestorIds = new[] { parent.Id },
2017-07-22 23:00:48 +00:00
StartIndex = query.StartIndex,
Limit = query.Limit
});
var result = new QueryResult<BaseItem>
{
TotalRecordCount = genresResult.TotalRecordCount,
2018-12-28 15:48:26 +00:00
Items = genresResult.Items.Select(i => i.Item1).ToArray()
2017-07-22 23:00:48 +00:00
};
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicAlbumArtists(BaseItem parent, User user, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
var artists = _libraryManager.GetAlbumArtists(new InternalItemsQuery(user)
{
2018-09-12 17:26:21 +00:00
AncestorIds = new[] { parent.Id },
2017-07-22 23:00:48 +00:00
StartIndex = query.StartIndex,
Limit = query.Limit
});
var result = new QueryResult<BaseItem>
{
TotalRecordCount = artists.TotalRecordCount,
2018-12-28 15:48:26 +00:00
Items = artists.Items.Select(i => i.Item1).ToArray()
2017-07-22 23:00:48 +00:00
};
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicArtists(BaseItem parent, User user, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
var artists = _libraryManager.GetArtists(new InternalItemsQuery(user)
{
2018-09-12 17:26:21 +00:00
AncestorIds = new[] { parent.Id },
2017-07-22 23:00:48 +00:00
StartIndex = query.StartIndex,
Limit = query.Limit
});
var result = new QueryResult<BaseItem>
{
TotalRecordCount = artists.TotalRecordCount,
2018-12-28 15:48:26 +00:00
Items = artists.Items.Select(i => i.Item1).ToArray()
2017-07-22 23:00:48 +00:00
};
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetFavoriteArtists(BaseItem parent, User user, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
var artists = _libraryManager.GetArtists(new InternalItemsQuery(user)
{
2018-09-12 17:26:21 +00:00
AncestorIds = new[] { parent.Id },
2017-07-22 23:00:48 +00:00
StartIndex = query.StartIndex,
Limit = query.Limit,
IsFavorite = true
});
var result = new QueryResult<BaseItem>
{
TotalRecordCount = artists.TotalRecordCount,
2018-12-28 15:48:26 +00:00
Items = artists.Items.Select(i => i.Item1).ToArray()
2017-07-22 23:00:48 +00:00
};
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicPlaylists(User user, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
query.Parent = null;
2020-05-13 02:10:35 +00:00
query.IncludeItemTypes = new[] { nameof(Playlist) };
2017-07-22 23:00:48 +00:00
query.SetUser(user);
query.Recursive = true;
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicLatest(BaseItem parent, User user, InternalItemsQuery query)
2017-07-22 23:00:48 +00:00
{
2019-10-20 14:08:40 +00:00
query.OrderBy = Array.Empty<(string, SortOrder)>();
2017-07-22 23:00:48 +00:00
var items = _userViewManager.GetLatestItems(new LatestItemsQuery
{
2018-09-12 17:26:21 +00:00
UserId = user.Id,
2017-07-22 23:00:48 +00:00
Limit = 50,
2020-05-13 02:10:35 +00:00
IncludeItemTypes = new[] { nameof(Audio) },
ParentId = parent?.Id ?? Guid.Empty,
2017-07-22 23:00:48 +00:00
GroupItems = true
2018-09-12 17:26:21 +00:00
}, query.DtoOptions).Select(i => i.Item1 ?? i.Item2.FirstOrDefault()).Where(i => i != null).ToArray();
2017-07-22 23:00:48 +00:00
return ToResult(items);
}
2020-04-02 14:49:58 +00:00
private QueryResult<ServerItem> GetNextUp(BaseItem parent, InternalItemsQuery query)
{
2019-10-20 14:08:40 +00:00
query.OrderBy = Array.Empty<(string, SortOrder)>();
var result = _tvSeriesManager.GetNextUp(new NextUpQuery
{
Limit = query.Limit,
StartIndex = query.StartIndex,
2018-09-12 17:26:21 +00:00
UserId = query.User.Id
2019-01-13 19:16:19 +00:00
}, new[] { parent }, query.DtoOptions);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetTvLatest(BaseItem parent, User user, InternalItemsQuery query)
{
2019-10-20 14:08:40 +00:00
query.OrderBy = Array.Empty<(string, SortOrder)>();
var items = _userViewManager.GetLatestItems(new LatestItemsQuery
{
2018-09-12 17:26:21 +00:00
UserId = user.Id,
Limit = 50,
IncludeItemTypes = new[] { typeof(Episode).Name },
2018-09-12 17:26:21 +00:00
ParentId = parent == null ? Guid.Empty : parent.Id,
2017-08-13 02:09:07 +00:00
GroupItems = false
2018-09-12 17:26:21 +00:00
}, query.DtoOptions).Select(i => i.Item1 ?? i.Item2.FirstOrDefault()).Where(i => i != null).ToArray();
return ToResult(items);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMovieLatest(BaseItem parent, User user, InternalItemsQuery query)
{
2019-10-20 14:08:40 +00:00
query.OrderBy = Array.Empty<(string, SortOrder)>();
2020-05-13 02:10:35 +00:00
var items = _userViewManager.GetLatestItems(
new LatestItemsQuery
{
2018-09-12 17:26:21 +00:00
UserId = user.Id,
Limit = 50,
2020-05-13 02:10:35 +00:00
IncludeItemTypes = new[] { nameof(Movie) },
ParentId = parent?.Id ?? Guid.Empty,
GroupItems = true
2018-09-12 17:26:21 +00:00
}, query.DtoOptions).Select(i => i.Item1 ?? i.Item2.FirstOrDefault()).Where(i => i != null).ToArray();
return ToResult(items);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicArtistItems(BaseItem item, Guid parentId, User user, SortCriteria sort, int? startIndex, int? limit)
2016-12-20 19:59:25 +00:00
{
var query = new InternalItemsQuery(user)
{
Recursive = true,
ParentId = parentId,
2018-09-12 17:26:21 +00:00
ArtistIds = new[] { item.Id },
2016-12-20 19:59:25 +00:00
IncludeItemTypes = new[] { typeof(MusicAlbum).Name },
Limit = limit,
2017-05-21 07:25:49 +00:00
StartIndex = startIndex,
DtoOptions = GetDtoOptions()
2016-12-20 19:59:25 +00:00
};
SetSorting(query, sort, false);
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetGenreItems(BaseItem item, Guid parentId, User user, SortCriteria sort, int? startIndex, int? limit)
2017-08-19 19:43:35 +00:00
{
var query = new InternalItemsQuery(user)
{
Recursive = true,
ParentId = parentId,
2018-09-12 17:26:21 +00:00
GenreIds = new[] { item.Id },
2020-05-13 02:10:35 +00:00
IncludeItemTypes = new[]
{
nameof(Movie),
nameof(Series)
},
2017-08-19 19:43:35 +00:00
Limit = limit,
StartIndex = startIndex,
DtoOptions = GetDtoOptions()
};
SetSorting(query, sort, false);
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2020-05-20 17:07:53 +00:00
private QueryResult<ServerItem> GetMusicGenreItems(BaseItem item, Guid parentId, User user, SortCriteria sort, int? startIndex, int? limit)
2016-12-20 07:31:30 +00:00
{
var query = new InternalItemsQuery(user)
{
Recursive = true,
ParentId = parentId,
2018-09-12 17:26:21 +00:00
GenreIds = new[] { item.Id },
2017-05-26 06:48:54 +00:00
IncludeItemTypes = new[] { typeof(MusicAlbum).Name },
2016-12-20 07:31:30 +00:00
Limit = limit,
2017-05-21 07:25:49 +00:00
StartIndex = startIndex,
DtoOptions = GetDtoOptions()
2016-12-20 07:31:30 +00:00
};
SetSorting(query, sort, false);
var result = _libraryManager.GetItemsResult(query);
return ToResult(result);
}
2018-09-12 17:26:21 +00:00
private QueryResult<ServerItem> ToResult(BaseItem[] result)
2017-07-22 23:00:48 +00:00
{
var serverItems = result
.Select(i => new ServerItem(i))
2018-12-28 15:48:26 +00:00
.ToArray();
2017-07-22 23:00:48 +00:00
return new QueryResult<ServerItem>
{
2018-09-12 17:26:21 +00:00
TotalRecordCount = result.Length,
2017-07-22 23:00:48 +00:00
Items = serverItems
};
}
2016-12-20 07:31:30 +00:00
private QueryResult<ServerItem> ToResult(QueryResult<BaseItem> result)
{
var serverItems = result
2016-10-29 22:22:20 +00:00
.Items
2016-12-20 19:59:25 +00:00
.Select(i => new ServerItem(i))
2018-09-12 17:26:21 +00:00
.ToArray();
2016-10-29 22:22:20 +00:00
return new QueryResult<ServerItem>
{
2016-12-20 07:31:30 +00:00
TotalRecordCount = result.TotalRecordCount,
2016-10-29 22:22:20 +00:00
Items = serverItems
};
}
2016-12-20 07:31:30 +00:00
private void SetSorting(InternalItemsQuery query, SortCriteria sort, bool isPreSorted)
{
2019-10-20 14:08:40 +00:00
if (isPreSorted)
2016-12-20 07:31:30 +00:00
{
2019-10-20 14:08:40 +00:00
query.OrderBy = Array.Empty<(string, SortOrder)>();
}
2019-12-06 10:58:45 +00:00
else
2019-10-20 14:08:40 +00:00
{
query.OrderBy = new[] { (ItemSortBy.SortName, sort.SortOrder) };
2016-12-20 07:31:30 +00:00
}
2016-10-29 22:22:20 +00:00
}
private QueryResult<ServerItem> ApplyPaging(QueryResult<ServerItem> result, int? startIndex, int? limit)
{
result.Items = result.Items.Skip(startIndex ?? 0).Take(limit ?? int.MaxValue).ToArray();
return result;
}
2020-04-02 14:49:58 +00:00
private ServerItem GetItemFromObjectId(string id)
2016-10-29 22:22:20 +00:00
{
return DidlBuilder.IsIdRoot(id)
2018-09-12 17:26:21 +00:00
? new ServerItem(_libraryManager.GetUserRootFolder())
2020-04-02 14:49:58 +00:00
: ParseItemId(id);
2016-10-29 22:22:20 +00:00
}
2020-04-02 14:49:58 +00:00
private ServerItem ParseItemId(string id)
2016-10-29 22:22:20 +00:00
{
StubType? stubType = null;
// After using PlayTo, MediaMonkey sends a request to the server trying to get item info
const string ParamsSrch = "Params=";
var paramsIndex = id.IndexOf(ParamsSrch, StringComparison.OrdinalIgnoreCase);
2016-10-29 22:22:20 +00:00
if (paramsIndex != -1)
{
id = id.Substring(paramsIndex + ParamsSrch.Length);
2016-10-29 22:22:20 +00:00
var parts = id.Split(';');
id = parts[23];
}
var enumNames = Enum.GetNames(typeof(StubType));
foreach (var name in enumNames)
2016-10-29 22:22:20 +00:00
{
if (id.StartsWith(name + "_", StringComparison.OrdinalIgnoreCase))
{
stubType = (StubType)Enum.Parse(typeof(StubType), name, true);
id = id.Split(new[] { '_' }, 2)[1];
break;
}
2017-07-22 23:00:48 +00:00
}
2016-10-29 22:22:20 +00:00
if (Guid.TryParse(id, out var itemId))
2016-10-29 22:22:20 +00:00
{
var item = _libraryManager.GetItemById(itemId);
2016-12-20 19:59:25 +00:00
return new ServerItem(item)
2016-10-29 22:22:20 +00:00
{
StubType = stubType
};
}
2020-01-21 16:59:41 +00:00
Logger.LogError("Error parsing item Id: {id}. Returning user root folder.", id);
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
return new ServerItem(_libraryManager.GetUserRootFolder());
2016-10-29 22:22:20 +00:00
}
}
internal class ServerItem
{
public BaseItem Item { get; set; }
public StubType? StubType { get; set; }
2016-12-20 19:59:25 +00:00
public ServerItem(BaseItem item)
{
Item = item;
if (item is IItemByName && !(item is Folder))
{
StubType = Dlna.ContentDirectory.StubType.Folder;
}
}
2016-10-29 22:22:20 +00:00
}
public enum StubType
{
Folder = 0,
2017-07-22 23:00:48 +00:00
Latest = 2,
Playlists = 3,
Albums = 4,
AlbumArtists = 5,
Artists = 6,
Songs = 7,
Genres = 8,
FavoriteSongs = 9,
FavoriteArtists = 10,
FavoriteAlbums = 11,
ContinueWatching = 12,
Movies = 13,
Collections = 14,
Favorites = 15,
NextUp = 16,
Series = 17,
FavoriteSeries = 18,
FavoriteEpisodes = 19
2016-10-29 22:22:20 +00:00
}
}