jellyfin/MediaBrowser.Dlna/ContentDirectory/ControlHandler.cs

692 lines
26 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Extensions;
2014-12-01 12:43:34 +00:00
using MediaBrowser.Controller.Channels;
2014-05-01 03:24:55 +00:00
using MediaBrowser.Controller.Configuration;
2014-04-18 05:03:01 +00:00
using MediaBrowser.Controller.Drawing;
2014-05-21 00:56:24 +00:00
using MediaBrowser.Controller.Entities;
2014-10-22 04:42:26 +00:00
using MediaBrowser.Controller.Entities.Movies;
2014-12-01 12:43:34 +00:00
using MediaBrowser.Controller.Entities.TV;
2014-04-16 05:08:12 +00:00
using MediaBrowser.Controller.Library;
2014-09-24 01:44:05 +00:00
using MediaBrowser.Controller.Localization;
2014-04-23 16:29:21 +00:00
using MediaBrowser.Dlna.Didl;
2014-05-21 00:56:24 +00:00
using MediaBrowser.Dlna.Server;
using MediaBrowser.Dlna.Service;
2014-12-01 12:43:34 +00:00
using MediaBrowser.Model.Channels;
2015-03-05 04:13:08 +00:00
using MediaBrowser.Model.Configuration;
2014-04-16 05:08:12 +00:00
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Entities;
2014-04-10 15:06:54 +00:00
using MediaBrowser.Model.Logging;
2014-04-18 05:03:01 +00:00
using MediaBrowser.Model.Querying;
2014-04-10 15:06:54 +00:00
using System;
using System.Collections.Generic;
2014-04-16 05:08:12 +00:00
using System.Globalization;
using System.Linq;
2014-04-10 15:06:54 +00:00
using System.Text;
2014-04-18 05:03:01 +00:00
using System.Threading;
2014-07-30 03:31:35 +00:00
using System.Threading.Tasks;
2014-04-10 15:06:54 +00:00
using System.Xml;
2014-05-21 00:56:24 +00:00
namespace MediaBrowser.Dlna.ContentDirectory
2014-04-10 15:06:54 +00:00
{
2014-05-21 00:56:24 +00:00
public class ControlHandler : BaseControlHandler
2014-04-10 15:06:54 +00:00
{
2014-04-16 05:08:12 +00:00
private readonly ILibraryManager _libraryManager;
2014-12-01 12:43:34 +00:00
private readonly IChannelManager _channelManager;
2014-04-18 05:03:01 +00:00
private readonly IUserDataManager _userDataManager;
2015-03-07 22:43:53 +00:00
private readonly IServerConfigurationManager _config;
2014-04-20 05:21:08 +00:00
private readonly User _user;
2014-04-18 05:03:01 +00:00
2014-04-10 15:06:54 +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/";
2014-04-21 16:02:30 +00:00
private readonly int _systemUpdateId;
2014-04-16 05:08:12 +00:00
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
2014-04-10 15:06:54 +00:00
2014-04-23 16:29:21 +00:00
private readonly DidlBuilder _didlBuilder;
2014-04-24 05:08:10 +00:00
private readonly DeviceProfile _profile;
2015-03-08 04:44:31 +00:00
public ControlHandler(ILogger logger, ILibraryManager libraryManager, DeviceProfile profile, string serverAddress, string accessToken, IImageProcessor imageProcessor, IUserDataManager userDataManager, User user, int systemUpdateId, IServerConfigurationManager config, ILocalizationManager localization, IChannelManager channelManager, IMediaSourceManager mediaSourceManager)
2014-05-21 00:56:24 +00:00
: base(config, logger)
2014-04-10 15:06:54 +00:00
{
2014-04-16 05:08:12 +00:00
_libraryManager = libraryManager;
2014-04-18 05:03:01 +00:00
_userDataManager = userDataManager;
2014-04-20 05:21:08 +00:00
_user = user;
2014-04-21 16:02:30 +00:00
_systemUpdateId = systemUpdateId;
2014-12-01 12:43:34 +00:00
_channelManager = channelManager;
2014-04-24 05:08:10 +00:00
_profile = profile;
2015-03-05 04:13:08 +00:00
_config = config;
2014-04-23 16:29:21 +00:00
2015-04-16 14:59:39 +00:00
_didlBuilder = new DidlBuilder(profile, user, imageProcessor, serverAddress, accessToken, userDataManager, localization, mediaSourceManager, Logger);
2014-04-10 15:06:54 +00:00
}
2014-05-21 00:56:24 +00:00
protected override IEnumerable<KeyValuePair<string, string>> GetResult(string methodName, Headers methodParams)
2014-04-16 05:08:12 +00:00
{
2014-05-21 00:56:24 +00:00
var deviceId = "test";
2014-05-01 03:24:55 +00:00
2014-05-21 00:56:24 +00:00
var user = _user;
2014-05-01 03:24:55 +00:00
2014-05-21 00:56:24 +00:00
if (string.Equals(methodName, "GetSearchCapabilities", StringComparison.OrdinalIgnoreCase))
return HandleGetSearchCapabilities();
2014-05-01 03:24:55 +00:00
2014-05-21 00:56:24 +00:00
if (string.Equals(methodName, "GetSortCapabilities", StringComparison.OrdinalIgnoreCase))
return HandleGetSortCapabilities();
2014-04-10 15:06:54 +00:00
2014-11-06 01:32:36 +00:00
if (string.Equals(methodName, "GetSortExtensionCapabilities", StringComparison.OrdinalIgnoreCase))
return HandleGetSortExtensionCapabilities();
2014-05-21 00:56:24 +00:00
if (string.Equals(methodName, "GetSystemUpdateID", StringComparison.OrdinalIgnoreCase))
return HandleGetSystemUpdateID();
2014-04-18 17:16:25 +00:00
2014-05-21 00:56:24 +00:00
if (string.Equals(methodName, "Browse", StringComparison.OrdinalIgnoreCase))
2014-07-30 03:31:35 +00:00
return HandleBrowse(methodParams, user, deviceId).Result;
2014-04-16 05:08:12 +00:00
2014-05-21 00:56:24 +00:00
if (string.Equals(methodName, "X_GetFeatureList", StringComparison.OrdinalIgnoreCase))
return HandleXGetFeatureList();
2014-04-16 05:08:12 +00:00
2014-11-06 01:32:36 +00:00
if (string.Equals(methodName, "GetFeatureList", StringComparison.OrdinalIgnoreCase))
return HandleGetFeatureList();
2014-05-21 00:56:24 +00:00
if (string.Equals(methodName, "X_SetBookmark", StringComparison.OrdinalIgnoreCase))
return HandleXSetBookmark(methodParams, user);
2014-04-20 05:21:08 +00:00
2014-05-21 00:56:24 +00:00
if (string.Equals(methodName, "Search", StringComparison.OrdinalIgnoreCase))
2014-07-30 03:31:35 +00:00
return HandleSearch(methodParams, user, deviceId).Result;
2014-04-10 15:06:54 +00:00
2014-05-21 00:56:24 +00:00
throw new ResourceNotFoundException("Unexpected control request name: " + methodName);
2014-04-16 05:08:12 +00:00
}
private IEnumerable<KeyValuePair<string, string>> HandleXSetBookmark(IDictionary<string, string> sparams, User user)
2014-04-10 15:06:54 +00:00
{
var id = sparams["ObjectID"];
2014-04-16 05:08:12 +00:00
2014-10-22 04:42:26 +00:00
var serverItem = GetItemFromObjectId(id, user);
var item = serverItem.Item;
2014-04-18 05:03:01 +00:00
var newbookmark = int.Parse(sparams["PosSecond"], _usCulture);
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
userdata.PlaybackPositionTicks = TimeSpan.FromSeconds(newbookmark).Ticks;
_userDataManager.SaveUserData(user.Id, item, userdata, UserDataSaveReason.TogglePlayed,
CancellationToken.None);
2014-04-16 05:08:12 +00:00
2014-04-10 15:06:54 +00:00
return new Headers();
}
2014-04-16 05:08:12 +00:00
private IEnumerable<KeyValuePair<string, string>> HandleGetSearchCapabilities()
2014-04-10 15:06:54 +00:00
{
2014-08-17 05:38:13 +00:00
return new Headers(true) { { "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" } };
2014-04-10 15:06:54 +00:00
}
2014-04-16 05:08:12 +00:00
private IEnumerable<KeyValuePair<string, string>> HandleGetSortCapabilities()
2014-04-10 15:06:54 +00:00
{
2014-11-06 01:32:36 +00:00
return new Headers(true)
{
{ "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" }
};
}
private IEnumerable<KeyValuePair<string, string>> HandleGetSortExtensionCapabilities()
{
return new Headers(true)
{
{ "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" }
};
2014-04-10 15:06:54 +00:00
}
2014-04-16 05:08:12 +00:00
private IEnumerable<KeyValuePair<string, string>> HandleGetSystemUpdateID()
2014-04-10 15:06:54 +00:00
{
2014-05-12 16:47:22 +00:00
var headers = new Headers(true);
headers.Add("Id", _systemUpdateId.ToString(_usCulture));
2014-05-21 00:56:24 +00:00
return headers;
2014-04-10 15:06:54 +00:00
}
2014-11-06 01:32:36 +00:00
private IEnumerable<KeyValuePair<string, string>> HandleGetFeatureList()
{
return new Headers(true)
{
{ "FeatureList", GetFeatureListXml() }
};
}
2014-04-16 05:08:12 +00:00
private IEnumerable<KeyValuePair<string, string>> HandleXGetFeatureList()
2014-04-10 15:06:54 +00:00
{
2014-11-06 01:32:36 +00:00
return new Headers(true)
{
{ "FeatureList", GetFeatureListXml() }
};
2014-04-10 15:06:54 +00:00
}
private string GetFeatureListXml()
{
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();
}
2014-07-30 03:31:35 +00:00
private async Task<IEnumerable<KeyValuePair<string, string>>> HandleBrowse(Headers sparams, User user, string deviceId)
2014-04-10 15:06:54 +00:00
{
var id = sparams["ObjectID"];
var flag = sparams["BrowseFlag"];
2014-04-20 05:21:08 +00:00
var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", ""));
2014-04-10 15:06:54 +00:00
var provided = 0;
2015-02-01 03:04:42 +00:00
// Default to null instead of 0
// Upnp inspector sends 0 as requestedCount when it wants everything
int? requestedCount = null;
2014-07-30 03:31:35 +00:00
int? start = 0;
int requestedVal;
if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requestedVal) && requestedVal > 0)
2014-04-10 15:06:54 +00:00
{
2015-02-01 03:04:42 +00:00
requestedCount = requestedVal;
2014-04-10 15:06:54 +00:00
}
2014-07-30 03:31:35 +00:00
int startVal;
if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out startVal) && startVal > 0)
2014-04-10 15:06:54 +00:00
{
2014-07-30 03:31:35 +00:00
start = startVal;
2014-04-10 15:06:54 +00:00
}
//var root = GetItem(id) as IMediaFolder;
var result = new XmlDocument();
var didl = result.CreateElement(string.Empty, "DIDL-Lite", NS_DIDL);
didl.SetAttribute("xmlns:dc", NS_DC);
didl.SetAttribute("xmlns:dlna", NS_DLNA);
didl.SetAttribute("xmlns:upnp", NS_UPNP);
2014-04-22 17:25:54 +00:00
//didl.SetAttribute("xmlns:sec", NS_SEC);
2014-04-10 15:06:54 +00:00
result.AppendChild(didl);
2014-10-22 04:42:26 +00:00
var serverItem = GetItemFromObjectId(id, user);
var item = serverItem.Item;
2014-04-16 05:08:12 +00:00
2014-12-04 05:24:41 +00:00
int totalCount;
2014-04-16 05:08:12 +00:00
if (string.Equals(flag, "BrowseMetadata"))
{
2014-12-04 05:24:41 +00:00
totalCount = 1;
2015-04-05 15:01:57 +00:00
2014-10-22 04:42:26 +00:00
if (item.IsFolder || serverItem.StubType.HasValue)
2014-08-06 02:26:12 +00:00
{
2015-02-01 03:04:42 +00:00
var childrenResult = (await GetUserItems(item, serverItem.StubType, user, sortCriteria, start, requestedCount).ConfigureAwait(false));
2014-10-22 04:42:26 +00:00
2014-11-21 14:28:30 +00:00
result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, item, serverItem.StubType, null, childrenResult.TotalRecordCount, filter, id));
2014-08-06 02:26:12 +00:00
}
else
{
2014-10-22 04:42:26 +00:00
result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, item, null, null, deviceId, filter));
2014-08-06 02:26:12 +00:00
}
2014-10-22 04:42:26 +00:00
2014-04-16 05:08:12 +00:00
provided++;
}
else
{
2015-02-01 03:04:42 +00:00
var childrenResult = (await GetUserItems(item, serverItem.StubType, user, sortCriteria, start, requestedCount).ConfigureAwait(false));
2014-08-06 02:26:12 +00:00
totalCount = childrenResult.TotalRecordCount;
2014-07-30 03:31:35 +00:00
provided = childrenResult.Items.Length;
2014-04-18 05:03:01 +00:00
2014-07-30 03:31:35 +00:00
foreach (var i in childrenResult.Items)
2014-04-16 05:08:12 +00:00
{
2014-11-29 22:19:02 +00:00
var childItem = i.Item;
2014-11-30 19:01:33 +00:00
var displayStubType = i.StubType;
2014-10-22 04:42:26 +00:00
2014-11-29 22:19:02 +00:00
if (childItem.IsFolder || displayStubType.HasValue)
2014-04-16 05:08:12 +00:00
{
2014-11-29 22:19:02 +00:00
var childCount = (await GetUserItems(childItem, displayStubType, user, sortCriteria, null, 0).ConfigureAwait(false))
2014-07-30 03:31:35 +00:00
.TotalRecordCount;
2014-04-16 05:08:12 +00:00
2014-11-29 22:19:02 +00:00
result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, childItem, displayStubType, item, childCount, filter));
2014-04-18 05:03:01 +00:00
}
else
{
2014-11-29 22:19:02 +00:00
result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, childItem, item, serverItem.StubType, deviceId, filter));
2014-04-16 05:08:12 +00:00
}
}
}
var resXML = result.OuterXml;
return new List<KeyValuePair<string, string>>
{
new KeyValuePair<string,string>("Result", resXML),
new KeyValuePair<string,string>("NumberReturned", provided.ToString(_usCulture)),
new KeyValuePair<string,string>("TotalMatches", totalCount.ToString(_usCulture)),
2014-04-21 16:02:30 +00:00
new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
2014-04-16 05:08:12 +00:00
};
}
2014-07-30 03:31:35 +00:00
private async Task<IEnumerable<KeyValuePair<string, string>>> HandleSearch(Headers sparams, User user, string deviceId)
2014-04-18 05:03:01 +00:00
{
2014-04-20 05:21:08 +00:00
var searchCriteria = new SearchCriteria(sparams.GetValueOrDefault("SearchCriteria", ""));
var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", ""));
var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
// sort example: dc:title, dc:date
2015-02-01 03:04:42 +00:00
// Default to null instead of 0
// Upnp inspector sends 0 as requestedCount when it wants everything
int? requestedCount = null;
2014-07-30 03:31:35 +00:00
int? start = 0;
2014-04-20 05:21:08 +00:00
2014-07-30 03:31:35 +00:00
int requestedVal;
if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requestedVal) && requestedVal > 0)
2014-04-20 05:21:08 +00:00
{
2015-02-01 03:04:42 +00:00
requestedCount = requestedVal;
2014-04-20 05:21:08 +00:00
}
2014-07-30 03:31:35 +00:00
int startVal;
if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out startVal) && startVal > 0)
2014-04-20 05:21:08 +00:00
{
2014-07-30 03:31:35 +00:00
start = startVal;
2014-04-20 05:21:08 +00:00
}
//var root = GetItem(id) as IMediaFolder;
var result = new XmlDocument();
var didl = result.CreateElement(string.Empty, "DIDL-Lite", NS_DIDL);
didl.SetAttribute("xmlns:dc", NS_DC);
didl.SetAttribute("xmlns:dlna", NS_DLNA);
didl.SetAttribute("xmlns:upnp", NS_UPNP);
2014-04-24 05:08:10 +00:00
2014-04-24 14:11:05 +00:00
foreach (var att in _profile.XmlRootAttributes)
2014-04-24 05:08:10 +00:00
{
didl.SetAttribute(att.Name, att.Value);
}
2014-04-20 05:21:08 +00:00
result.AppendChild(didl);
2014-10-22 04:42:26 +00:00
var serverItem = GetItemFromObjectId(sparams["ContainerID"], user);
var item = serverItem.Item;
2014-04-20 05:21:08 +00:00
2015-02-01 03:04:42 +00:00
var childrenResult = (await GetChildrenSorted(item, user, searchCriteria, sortCriteria, start, requestedCount).ConfigureAwait(false));
2014-04-20 05:21:08 +00:00
2014-07-30 03:31:35 +00:00
var totalCount = childrenResult.TotalRecordCount;
2014-04-20 05:21:08 +00:00
2014-07-30 03:31:35 +00:00
var provided = childrenResult.Items.Length;
2014-04-20 05:21:08 +00:00
2014-07-30 03:31:35 +00:00
foreach (var i in childrenResult.Items)
2014-04-20 05:21:08 +00:00
{
if (i.IsFolder)
{
2014-10-22 04:42:26 +00:00
var childCount = (await GetChildrenSorted(i, user, searchCriteria, sortCriteria, null, 0).ConfigureAwait(false))
2014-07-30 03:31:35 +00:00
.TotalRecordCount;
2014-04-20 05:21:08 +00:00
2014-10-22 04:42:26 +00:00
result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, i, null, item, childCount, filter));
2014-04-20 05:21:08 +00:00
}
else
{
2014-10-22 04:42:26 +00:00
result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, i, item, serverItem.StubType, deviceId, filter));
2014-04-20 05:21:08 +00:00
}
}
var resXML = result.OuterXml;
return new List<KeyValuePair<string, string>>
{
new KeyValuePair<string,string>("Result", resXML),
new KeyValuePair<string,string>("NumberReturned", provided.ToString(_usCulture)),
new KeyValuePair<string,string>("TotalMatches", totalCount.ToString(_usCulture)),
2014-04-21 16:02:30 +00:00
new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
2014-04-20 05:21:08 +00:00
};
}
2015-04-05 15:01:57 +00:00
private Task<QueryResult<BaseItem>> GetChildrenSorted(BaseItem item, User user, SearchCriteria search, SortCriteria sort, int? startIndex, int? limit)
2014-04-20 05:21:08 +00:00
{
2014-10-22 04:42:26 +00:00
var folder = (Folder)item;
var sortOrders = new List<string>();
if (!folder.IsPreSorted)
{
sortOrders.Add(ItemSortBy.SortName);
}
2014-07-30 03:31:35 +00:00
var mediaTypes = new List<string>();
bool? isFolder = null;
2014-04-20 05:21:08 +00:00
if (search.SearchType == SearchType.Audio)
{
mediaTypes.Add(MediaType.Audio);
isFolder = false;
2014-04-20 05:21:08 +00:00
}
else if (search.SearchType == SearchType.Video)
{
mediaTypes.Add(MediaType.Video);
isFolder = false;
2014-04-20 05:21:08 +00:00
}
else if (search.SearchType == SearchType.Image)
{
mediaTypes.Add(MediaType.Photo);
isFolder = false;
2014-04-20 05:21:08 +00:00
}
else if (search.SearchType == SearchType.Playlist)
{
//items = items.OfType<Playlist>();
isFolder = true;
2014-04-20 05:21:08 +00:00
}
2014-08-17 05:38:13 +00:00
else if (search.SearchType == SearchType.MusicAlbum)
{
//items = items.OfType<MusicAlbum>();
isFolder = true;
2014-08-17 05:38:13 +00:00
}
2014-10-22 04:42:26 +00:00
2015-04-05 15:01:57 +00:00
return folder.GetItems(new InternalItemsQuery
{
Limit = limit,
StartIndex = startIndex,
SortBy = sortOrders.ToArray(),
SortOrder = sort.SortOrder,
User = user,
Recursive = true,
Filter = FilterUnsupportedContent,
IsFolder = isFolder,
MediaTypes = mediaTypes.ToArray()
2015-04-05 15:01:57 +00:00
});
2014-07-30 03:31:35 +00:00
}
2014-11-29 22:19:02 +00:00
private async Task<QueryResult<ServerItem>> GetUserItems(BaseItem item, StubType? stubType, User user, SortCriteria sort, int? startIndex, int? limit)
2014-07-30 03:31:35 +00:00
{
2014-10-22 04:42:26 +00:00
if (stubType.HasValue)
{
2014-11-30 19:01:33 +00:00
if (stubType.Value == StubType.People)
{
var items = item.People.Select(i =>
{
try
{
return _libraryManager.GetPerson(i.Name);
}
catch
{
return null;
}
}).Where(i => i != null).ToArray();
var result = new QueryResult<ServerItem>
{
2014-12-01 12:43:34 +00:00
Items = items.Select(i => new ServerItem { Item = i, StubType = StubType.Folder }).ToArray(),
2014-11-30 19:01:33 +00:00
TotalRecordCount = items.Length
};
2014-10-22 04:42:26 +00:00
2014-11-30 19:01:33 +00:00
return ApplyPaging(result, startIndex, limit);
}
if (stubType.Value == StubType.Folder)
2014-10-22 04:42:26 +00:00
{
2014-11-30 19:01:33 +00:00
var movie = item as Movie;
if (movie != null)
{
return ApplyPaging(await GetMovieItems(movie).ConfigureAwait(false), startIndex, limit);
}
2014-10-22 04:42:26 +00:00
}
2014-12-01 12:43:34 +00:00
var person = item as Person;
if (person != null)
{
return await GetItemsFromPerson(person, user, startIndex, limit).ConfigureAwait(false);
}
return ApplyPaging(new QueryResult<ServerItem>(), startIndex, limit);
2014-10-22 04:42:26 +00:00
}
var folder = (Folder)item;
var sortOrders = new List<string>();
if (!folder.IsPreSorted)
2014-07-30 03:31:35 +00:00
{
sortOrders.Add(ItemSortBy.SortName);
2014-07-30 03:31:35 +00:00
}
2014-11-29 22:19:02 +00:00
var queryResult = await folder.GetItems(new InternalItemsQuery
2014-07-30 03:31:35 +00:00
{
Limit = limit,
StartIndex = startIndex,
SortBy = sortOrders.ToArray(),
SortOrder = sort.SortOrder,
User = user,
Filter = FilterUnsupportedContent
2014-07-30 03:31:35 +00:00
}).ConfigureAwait(false);
2014-11-29 22:19:02 +00:00
2015-03-05 04:13:08 +00:00
var options = _config.GetDlnaConfiguration();
2014-11-30 19:01:33 +00:00
var serverItems = queryResult
.Items
.Select(i => new ServerItem
{
Item = i,
2015-03-05 04:13:08 +00:00
StubType = GetDisplayStubType(i, item, options)
2014-11-30 19:01:33 +00:00
})
.ToArray();
2014-11-29 22:19:02 +00:00
return new QueryResult<ServerItem>
{
TotalRecordCount = queryResult.TotalRecordCount,
2014-11-30 19:01:33 +00:00
Items = serverItems
2014-11-29 22:19:02 +00:00
};
2014-07-30 03:31:35 +00:00
}
2014-12-01 12:43:34 +00:00
private async Task<QueryResult<ServerItem>> GetItemsFromPerson(Person person, User user, int? startIndex, int? limit)
{
2015-01-25 06:34:50 +00:00
var items = user.RootFolder.GetRecursiveChildren(user, i => i is Movie || i is Series && i.ContainsPerson(person.Name))
2014-12-01 12:43:34 +00:00
.ToList();
var trailerResult = await _channelManager.GetAllMediaInternal(new AllChannelMediaQuery
{
ContentTypes = new[] { ChannelMediaContentType.MovieExtra },
ExtraTypes = new[] { ExtraType.Trailer },
UserId = user.Id.ToString("N")
}, CancellationToken.None).ConfigureAwait(false);
2014-12-11 06:20:28 +00:00
var currentIds = items.Select(i => i.GetProviderId(MetadataProviders.Imdb))
.ToList();
var trailersToAdd = trailerResult.Items
.Where(i => i.ContainsPerson(person.Name))
.Where(i =>
{
// Try to filter out dupes using imdb id
var imdb = i.GetProviderId(MetadataProviders.Imdb);
if (!string.IsNullOrWhiteSpace(imdb) &&
currentIds.Contains(imdb, StringComparer.OrdinalIgnoreCase))
{
return false;
}
return true;
});
items.AddRange(trailersToAdd);
2014-12-01 12:43:34 +00:00
items = _libraryManager.Sort(items, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending)
.Skip(startIndex ?? 0)
.Take(limit ?? int.MaxValue)
.ToList();
var serverItems = items.Select(i => new ServerItem
{
Item = i,
StubType = null
})
.ToArray();
return new QueryResult<ServerItem>
{
TotalRecordCount = serverItems.Length,
Items = serverItems
};
}
2014-11-30 19:01:33 +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;
}
2015-03-05 04:13:08 +00:00
private StubType? GetDisplayStubType(BaseItem item, BaseItem context, DlnaOptions options)
2014-11-30 19:01:33 +00:00
{
if (context == null || context.IsFolder)
{
var movie = item as Movie;
2015-03-05 04:13:08 +00:00
if (movie != null && options.EnableEnhancedMovies)
2014-11-30 19:01:33 +00:00
{
2014-12-11 06:20:28 +00:00
if (movie.GetTrailerIds().Count > 0 ||
2014-11-30 19:01:33 +00:00
movie.SpecialFeatureIds.Count > 0)
{
return StubType.Folder;
}
2015-03-05 04:13:08 +00:00
if (EnablePeopleDisplay(item))
{
return StubType.Folder;
}
2014-11-30 19:01:33 +00:00
}
}
return null;
}
2014-12-01 12:43:34 +00:00
private bool EnablePeopleDisplay(BaseItem item)
{
if (item.People.Count > 0)
{
return item is Movie;
}
return false;
}
2014-11-29 22:19:02 +00:00
private Task<QueryResult<ServerItem>> GetMovieItems(Movie item)
2014-10-22 04:42:26 +00:00
{
var list = new List<BaseItem>();
list.Add(item);
2014-12-11 06:20:28 +00:00
list.AddRange(item.GetTrailerIds().Select(i => _libraryManager.GetItemById(i)).Where(i => i != null));
2014-10-22 04:42:26 +00:00
list.AddRange(item.SpecialFeatureIds.Select(i => _libraryManager.GetItemById(i)).Where(i => i != null));
2014-11-29 22:19:02 +00:00
var serverItems = list.Select(i => new ServerItem { Item = i, StubType = null })
.ToList();
2014-11-30 19:01:33 +00:00
serverItems.Add(new ServerItem
{
Item = item,
StubType = StubType.People
});
2014-11-29 22:19:02 +00:00
return Task.FromResult(new QueryResult<ServerItem>
2014-10-22 04:42:26 +00:00
{
2014-11-29 22:19:02 +00:00
Items = serverItems.ToArray(),
TotalRecordCount = serverItems.Count
2014-10-22 04:42:26 +00:00
});
}
2015-01-25 06:34:50 +00:00
private bool FilterUnsupportedContent(BaseItem i)
2014-07-30 03:31:35 +00:00
{
// Unplayable
if (i.LocationType == LocationType.Virtual && !i.IsFolder)
2014-07-30 03:31:35 +00:00
{
return false;
2014-07-30 03:31:35 +00:00
}
// Unplayable
var supportsPlaceHolder = i as ISupportsPlaceHolders;
if (supportsPlaceHolder != null && supportsPlaceHolder.IsPlaceHolder)
2014-07-30 03:31:35 +00:00
{
return false;
2014-07-30 03:31:35 +00:00
}
if (i is Game || i is Book)
2014-07-30 03:31:35 +00:00
{
//return false;
2014-07-30 03:31:35 +00:00
}
2014-04-20 05:21:08 +00:00
return true;
2014-04-18 05:03:01 +00:00
}
2014-10-22 04:42:26 +00:00
private ServerItem GetItemFromObjectId(string id, User user)
2014-04-19 17:43:12 +00:00
{
2014-08-30 21:45:04 +00:00
return DidlBuilder.IsIdRoot(id)
2014-04-19 17:43:12 +00:00
2014-10-22 04:42:26 +00:00
? new ServerItem { Item = user.RootFolder }
2014-06-22 05:52:31 +00:00
: ParseItemId(id, user);
}
2014-10-22 04:42:26 +00:00
private ServerItem ParseItemId(string id, User user)
2014-06-22 05:52:31 +00:00
{
Guid itemId;
2014-10-22 04:42:26 +00:00
StubType? stubType = null;
2015-04-24 20:06:37 +00:00
// 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);
if (paramsIndex != -1)
{
id = id.Substring(paramsIndex + paramsSrch.Length);
var parts = id.Split(';');
id = parts[24];
}
2014-10-22 04:42:26 +00:00
if (id.StartsWith("folder_", StringComparison.OrdinalIgnoreCase))
{
stubType = StubType.Folder;
id = id.Split(new[] { '_' }, 2)[1];
}
2014-11-30 19:01:33 +00:00
else if (id.StartsWith("people_", StringComparison.OrdinalIgnoreCase))
{
stubType = StubType.People;
id = id.Split(new[] { '_' }, 2)[1];
}
2014-06-22 05:52:31 +00:00
if (Guid.TryParse(id, out itemId))
{
2014-10-22 04:42:26 +00:00
var item = _libraryManager.GetItemById(itemId);
return new ServerItem
{
Item = item,
StubType = stubType
};
2014-06-22 05:52:31 +00:00
}
Logger.Error("Error parsing item Id: {0}. Returning user root folder.", id);
2014-10-22 04:42:26 +00:00
return new ServerItem { Item = user.RootFolder };
2014-04-19 17:43:12 +00:00
}
2014-04-10 15:06:54 +00:00
}
2014-10-22 04:42:26 +00:00
internal class ServerItem
{
public BaseItem Item { get; set; }
public StubType? StubType { get; set; }
}
public enum StubType
{
2014-11-30 19:01:33 +00:00
Folder = 0,
People = 1
2014-10-22 04:42:26 +00:00
}
2014-04-10 15:06:54 +00:00
}