示例#1
0
        private Task<QueryResult<BaseItem>> GetChildrenSorted(BaseItem item, User user, SearchCriteria search, SortCriteria sort, int? startIndex, int? limit)
        {
            var folder = (Folder)item;

            var sortOrders = new List<string>();
            if (!folder.IsPreSorted)
            {
                sortOrders.Add(ItemSortBy.SortName);
            }

            var mediaTypes = new List<string>();
            bool? isFolder = null;

            if (search.SearchType == SearchType.Audio)
            {
                mediaTypes.Add(MediaType.Audio);
                isFolder = false;
            }
            else if (search.SearchType == SearchType.Video)
            {
                mediaTypes.Add(MediaType.Video);
                isFolder = false;
            }
            else if (search.SearchType == SearchType.Image)
            {
                mediaTypes.Add(MediaType.Photo);
                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,
                SortBy = sortOrders.ToArray(),
                SortOrder = sort.SortOrder,
                User = user,
                Recursive = true,
                Filter = FilterUnsupportedContent,
                IsFolder = isFolder,
                MediaTypes = mediaTypes.ToArray()

            });
        }
示例#2
0
        private async Task<QueryResult<ServerItem>> GetUserItems(BaseItem item, StubType? stubType, User user, SortCriteria sort, int? startIndex, int? limit)
        {
            if (stubType.HasValue)
            {
                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>
                    {
                        Items = items.Select(i => new ServerItem { Item = i, StubType = StubType.Folder }).ToArray(),
                        TotalRecordCount = items.Length
                    };

                    return ApplyPaging(result, startIndex, limit);
                }
                if (stubType.Value == StubType.Folder)
                {
                    var movie = item as Movie;
                    if (movie != null)
                    {
                        return ApplyPaging(await GetMovieItems(movie).ConfigureAwait(false), startIndex, limit);
                    }
                }

                var person = item as Person;
                if (person != null)
                {
                    return await GetItemsFromPerson(person, user, startIndex, limit).ConfigureAwait(false);
                }

                return ApplyPaging(new QueryResult<ServerItem>(), startIndex, limit);
            }

            var folder = (Folder)item;

            var sortOrders = new List<string>();
            if (!folder.IsPreSorted)
            {
                sortOrders.Add(ItemSortBy.SortName);
            }

            var queryResult = await folder.GetItems(new InternalItemsQuery
            {
                Limit = limit,
                StartIndex = startIndex,
                SortBy = sortOrders.ToArray(),
                SortOrder = sort.SortOrder,
                User = user,
                Filter = FilterUnsupportedContent

            }).ConfigureAwait(false);

            var options = _config.GetDlnaConfiguration();

            var serverItems = queryResult
                .Items
                .Select(i => new ServerItem
                {
                    Item = i,
                    StubType = GetDisplayStubType(i, item, options)
                })
                .ToArray();

            return new QueryResult<ServerItem>
            {
                TotalRecordCount = queryResult.TotalRecordCount,
                Items = serverItems
            };
        }
示例#3
0
        private async Task<IEnumerable<KeyValuePair<string, string>>> HandleBrowse(Headers sparams, User user, string deviceId)
        {
            var id = sparams["ObjectID"];
            var flag = sparams["BrowseFlag"];
            var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
            var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", ""));

            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;

            int requestedVal;
            if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requestedVal) && requestedVal > 0)
            {
                requestedCount = requestedVal;
            }

            int startVal;
            if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out startVal) && startVal > 0)
            {
                start = startVal;
            }

            //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);
            //didl.SetAttribute("xmlns:sec", NS_SEC);
            result.AppendChild(didl);

            var serverItem = GetItemFromObjectId(id, user);
            var item = serverItem.Item;

            int totalCount;

            if (string.Equals(flag, "BrowseMetadata"))
            {
                totalCount = 1;

                if (item.IsFolder || serverItem.StubType.HasValue)
                {
                    var childrenResult = (await GetUserItems(item, serverItem.StubType, user, sortCriteria, start, requestedCount).ConfigureAwait(false));

                    result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, item, serverItem.StubType, null, childrenResult.TotalRecordCount, filter, id));
                }
                else
                {
                    result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, item, null, null, deviceId, filter));
                }

                provided++;
            }
            else
            {
                var childrenResult = (await GetUserItems(item, serverItem.StubType, user, sortCriteria, start, requestedCount).ConfigureAwait(false));
                totalCount = childrenResult.TotalRecordCount;

                provided = childrenResult.Items.Length;

                foreach (var i in childrenResult.Items)
                {
                    var childItem = i.Item;
                    var displayStubType = i.StubType;

                    if (childItem.IsFolder || displayStubType.HasValue)
                    {
                        var childCount = (await GetUserItems(childItem, displayStubType, user, sortCriteria, null, 0).ConfigureAwait(false))
                            .TotalRecordCount;

                        result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, childItem, displayStubType, item, childCount, filter));
                    }
                    else
                    {
                        result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, childItem, item, serverItem.StubType, deviceId, filter));
                    }
                }
            }

            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)),
                new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
            };
        }
示例#4
0
        private async Task<IEnumerable<KeyValuePair<string, string>>> HandleSearch(Headers sparams, User user, string deviceId)
        {
            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

            // Default to null instead of 0
            // Upnp inspector sends 0 as requestedCount when it wants everything
            int? requestedCount = null;
            int? start = 0;

            int requestedVal;
            if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requestedVal) && requestedVal > 0)
            {
                requestedCount = requestedVal;
            }

            int startVal;
            if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out startVal) && startVal > 0)
            {
                start = startVal;
            }

            //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);

            foreach (var att in _profile.XmlRootAttributes)
            {
                didl.SetAttribute(att.Name, att.Value);
            }

            result.AppendChild(didl);

            var serverItem = GetItemFromObjectId(sparams["ContainerID"], user);

            var item = serverItem.Item;

            var childrenResult = (await GetChildrenSorted(item, user, searchCriteria, sortCriteria, start, requestedCount).ConfigureAwait(false));

            var totalCount = childrenResult.TotalRecordCount;

            var provided = childrenResult.Items.Length;

            foreach (var i in childrenResult.Items)
            {
                if (i.IsFolder)
                {
                    var childCount = (await GetChildrenSorted(i, user, searchCriteria, sortCriteria, null, 0).ConfigureAwait(false))
                        .TotalRecordCount;

                    result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, i, null, item, childCount, filter));
                }
                else
                {
                    result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, i, item, serverItem.StubType, deviceId, filter));
                }
            }

            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)),
                new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
            };
        }
示例#5
0
        private async Task<QueryResult<ServerItem>> GetUserItems(BaseItem item, StubType? stubType, User user, SortCriteria sort, int? startIndex, int? limit)
        {
            if (stubType.HasValue)
            {
                if (stubType.Value == StubType.People)
                {
                    var items = _libraryManager.GetPeopleItems(new InternalPeopleQuery
                    {
                        ItemId = item.Id

                    }).ToArray();

                    var result = new QueryResult<ServerItem>
                    {
                        Items = items.Select(i => new ServerItem { Item = i, StubType = StubType.Folder }).ToArray(),
                        TotalRecordCount = items.Length
                    };

                    return ApplyPaging(result, startIndex, limit);
                }
                if (stubType.Value == StubType.Folder)
                {
                    var movie = item as Movie;
                    if (movie != null)
                    {
                        return ApplyPaging(await GetMovieItems(movie).ConfigureAwait(false), startIndex, limit);
                    }
                }

                var person = item as Person;
                if (person != null)
                {
                    return GetItemsFromPerson(person, user, startIndex, limit);
                }

                return ApplyPaging(new QueryResult<ServerItem>(), startIndex, limit);
            }

            var folder = (Folder)item;

            var sortOrders = new List<string>();
            if (!folder.IsPreSorted)
            {
                sortOrders.Add(ItemSortBy.SortName);
            }

            var queryResult = await folder.GetItems(new InternalItemsQuery
            {
                Limit = limit,
                StartIndex = startIndex,
                SortBy = sortOrders.ToArray(),
                SortOrder = sort.SortOrder,
                User = user,
                IsMissing = false,
                PresetViews = new[] { CollectionType.Movies, CollectionType.TvShows, CollectionType.Music },
                ExcludeItemTypes = new[] { typeof(Game).Name, typeof(Book).Name },
                IsPlaceHolder = false

            }).ConfigureAwait(false);

            var options = _config.GetDlnaConfiguration();

            var serverItems = queryResult
                .Items
                .Select(i => new ServerItem
                {
                    Item = i,
                    StubType = GetDisplayStubType(i, item, options)
                })
                .ToArray();

            return new QueryResult<ServerItem>
            {
                TotalRecordCount = queryResult.TotalRecordCount,
                Items = serverItems
            };
        }
示例#6
0
        private async Task<QueryResult<BaseItem>> GetUserItems(Folder folder, User user, SortCriteria sort, int? startIndex, int? limit)
        {
            var sortOrders = new List<string>();
            if (!folder.IsPreSorted)
            {
                sortOrders.Add(ItemSortBy.SortName);
            }

            return await folder.GetUserItems(new UserItemsQuery
            {
                Limit = limit,
                StartIndex = startIndex,
                SortBy = sortOrders.ToArray(),
                SortOrder = sort.SortOrder,
                User = user,
                Filter = FilterUnsupportedContent

            }).ConfigureAwait(false);
        }
示例#7
0
        private IEnumerable<BaseItem> GetChildrenSorted(Folder folder, User user, SortCriteria sort)
        {
            var items = folder.GetChildren(user, true);

            items = FilterUnsupportedContent(items);

            if (folder is Series || folder is Season || folder is BoxSet)
            {
                return items;
            }

            return SortItems(items, user, sort);
        }
示例#8
0
 private IEnumerable<BaseItem> SortItems(IEnumerable<BaseItem> items, User user, SortCriteria sort)
 {
     return _libraryManager.Sort(items, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
 }
示例#9
0
        private IEnumerable<KeyValuePair<string, string>> HandleSearch(Headers sparams, User user, string deviceId)
        {
            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

            var provided = 0;
            var requested = 0;
            var start = 0;

            if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requested) && requested <= 0)
            {
                requested = 0;
            }
            if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out start) && start <= 0)
            {
                start = 0;
            }

            //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);
            didl.SetAttribute("xmlns:sec", NS_SEC);
            result.AppendChild(didl);

            var folder = (Folder)GetItemFromObjectId(sparams["ContainerID"], user);

            var children = GetChildrenSorted(folder, user, searchCriteria, sortCriteria).ToList();

            var totalCount = children.Count;

            if (start > 0)
            {
                children = children.Skip(start).ToList();
            }
            if (requested > 0)
            {
                children = children.Take(requested).ToList();
            }

            provided = children.Count;

            foreach (var i in children)
            {
                if (i.IsFolder)
                {
                    var f = (Folder)i;
                    var childCount = GetChildrenSorted(f, user, searchCriteria, sortCriteria).Count();

                    Browse_AddFolder(result, f, childCount, filter);
                }
                else
                {
                    Browse_AddItem(result, i, user, deviceId, filter);
                }
            }

            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)),
                new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
            };
        }
示例#10
0
        private IEnumerable<BaseItem> GetChildrenSorted(Folder folder, User user, SearchCriteria search, SortCriteria sort)
        {
            if (search.SearchType == SearchType.Unknown)
            {
                return GetChildrenSorted(folder, user, sort);
            }

            var items = folder.GetRecursiveChildren(user);
            items = FilterUnsupportedContent(items);

            if (search.SearchType == SearchType.Audio)
            {
                items = items.OfType<Audio>();
            }
            else if (search.SearchType == SearchType.Video)
            {
                items = items.OfType<Video>();
            }
            else if (search.SearchType == SearchType.Image)
            {
                items = items.OfType<Photo>();
            }
            else if (search.SearchType == SearchType.Playlist)
            {
            }

            return SortItems(items, user, sort);
        }
示例#11
0
        private IEnumerable<BaseItem> GetPlainFolderChildrenSorted(Folder folder, User user, SortCriteria sort)
        {
            var items = folder.GetChildren(user, true);

            items = FilterUnsupportedContent(items);

            if (folder.IsPreSorted)
            {
                return items;
            }

            return SortItems(items, user, sort);
        }
示例#12
0
        private async Task<IEnumerable<BaseItem>> GetUserViewChildren(UserView folder, User user, SortCriteria sort)
        {
            if (string.Equals(folder.ViewType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase))
            {
                return new List<BaseItem>();
            }
            if (string.Equals(folder.ViewType, CollectionType.Channels, StringComparison.OrdinalIgnoreCase))
            {
                var result = await _channelManager.GetChannelsInternal(new ChannelQuery()
                {
                    UserId = user.Id.ToString("N")

                }, CancellationToken.None).ConfigureAwait(false);

                return result.Items;
            }
            if (string.Equals(folder.ViewType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
            {
                return SortItems(folder.GetChildren(user, true).OfType<Series>(), user, sort);
            }
            if (string.Equals(folder.ViewType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
            {
                return SortItems(folder.GetRecursiveChildren(user, true).OfType<Movie>(), user, sort);
            }
            if (string.Equals(folder.ViewType, CollectionType.Music, StringComparison.OrdinalIgnoreCase))
            {
                return SortItems(folder.GetChildren(user, true).OfType<MusicArtist>(), user, sort);
            }

            return GetPlainFolderChildrenSorted(folder, user, sort);
        }
示例#13
0
        private async Task<QueryResult<BaseItem>> GetChildrenSorted(Folder folder, User user, SortCriteria sort, int? startIndex, int? limit)
        {
            if (folder is UserRootFolder)
            {
                var result = await _userViewManager.GetUserViews(new UserViewQuery
                {
                    UserId = user.Id.ToString("N")

                }, CancellationToken.None).ConfigureAwait(false);

                return ToResult(result, startIndex, limit);
            }

            var view = folder as UserView;

            if (view != null)
            {
                var result = await GetUserViewChildren(view, user, sort).ConfigureAwait(false);

                return ToResult(result, startIndex, limit);
            }

            var channel = folder as Channel;

            if (channel != null)
            {
                try
                {
                    // Don't blow up here because it could cause parent screens with other content to fail
                    return await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
                    {
                        ChannelId = channel.Id.ToString("N"),
                        Limit = limit,
                        StartIndex = startIndex,
                        UserId = user.Id.ToString("N")

                    }, CancellationToken.None);
                }
                catch
                {
                    // Already logged at lower levels
                }
            }

            var channelFolderItem = folder as ChannelFolderItem;

            if (channelFolderItem != null)
            {
                try
                {
                    // Don't blow up here because it could cause parent screens with other content to fail
                    return await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
                    {
                        ChannelId = channelFolderItem.ChannelId,
                        FolderId = channelFolderItem.Id.ToString("N"),
                        Limit = limit,
                        StartIndex = startIndex,
                        UserId = user.Id.ToString("N")

                    }, CancellationToken.None);
                }
                catch
                {
                    // Already logged at lower levels
                }
            }

            return ToResult(GetPlainFolderChildrenSorted(folder, user, sort), startIndex, limit);
        }
示例#14
0
        private async Task<QueryResult<BaseItem>> GetChildrenSorted(Folder folder, User user, SearchCriteria search, SortCriteria sort, int? startIndex, int? limit)
        {
            if (search.SearchType == SearchType.Unknown)
            {
                return await GetChildrenSorted(folder, user, sort, startIndex, limit).ConfigureAwait(false);
            }

            var result = await GetChildrenSorted(folder, user, sort, null, null).ConfigureAwait(false);

            var items = FilterUnsupportedContent(result.Items);

            if (search.SearchType == SearchType.Audio)
            {
                items = items.OfType<Audio>();
            }
            else if (search.SearchType == SearchType.Video)
            {
                items = items.OfType<Video>();
            }
            else if (search.SearchType == SearchType.Image)
            {
                items = items.OfType<Photo>();
            }
            else if (search.SearchType == SearchType.Playlist)
            {
                items = items.OfType<Playlist>();
            }

            items = SortItems(items, user, sort);

            return ToResult(items, startIndex, limit);
        }
示例#15
0
        private async Task<QueryResult<ServerItem>> GetUserItems(BaseItem item, StubType? stubType, User user, SortCriteria sort, int? startIndex, int? limit)
        {
            if (stubType.HasValue)
            {
                if (stubType.Value == StubType.People)
                {
                    var items = _libraryManager.GetPeopleItems(new InternalPeopleQuery
                    {
                        ItemId = item.Id

                    }).ToArray();

                    var result = new QueryResult<ServerItem>
                    {
                        Items = items.Select(i => new ServerItem { Item = i, StubType = StubType.Folder }).ToArray(),
                        TotalRecordCount = items.Length
                    };

                    return ApplyPaging(result, startIndex, limit);
                }
                if (stubType.Value == StubType.Folder)
                {
                    var movie = item as Movie;
                    if (movie != null)
                    {
                        return ApplyPaging(await GetMovieItems(movie).ConfigureAwait(false), startIndex, limit);
                    }
                }

                var person = item as Person;
                if (person != null)
                {
                    return GetItemsFromPerson(person, user, startIndex, limit);
                }

                return ApplyPaging(new QueryResult<ServerItem>(), startIndex, limit);
            }

            var folder = (Folder)item;

            var sortOrders = new List<string>();
            if (!folder.IsPreSorted)
            {
                sortOrders.Add(ItemSortBy.SortName);
            }

            QueryResult<BaseItem> queryResult;

            if (folder is UserRootFolder)
            {
                var views = await _userViewManager.GetUserViews(new UserViewQuery { UserId = user.Id.ToString("N"), PresetViews = new[] { CollectionType.Movies, CollectionType.TvShows, CollectionType.Music } }, CancellationToken.None)
                            .ConfigureAwait(false);

                queryResult = new QueryResult<BaseItem>
                {
                    Items = views.Cast<BaseItem>().ToArray()
                };
                queryResult.TotalRecordCount = queryResult.Items.Length;
            }
            else
            {
                queryResult = await folder.GetItems(new InternalItemsQuery
               {
                   Limit = limit,
                   StartIndex = startIndex,
                   SortBy = sortOrders.ToArray(),
                   SortOrder = sort.SortOrder,
                   User = user,
                   Filter = FilterUnsupportedContent

               }).ConfigureAwait(false);
            }

            var options = _config.GetDlnaConfiguration();

            var serverItems = queryResult
                .Items
                .Select(i => new ServerItem
                {
                    Item = i,
                    StubType = GetDisplayStubType(i, item, options)
                })
                .ToArray();

            return new QueryResult<ServerItem>
            {
                TotalRecordCount = queryResult.TotalRecordCount,
                Items = serverItems
            };
        }