Example #1
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;

            int?requested = 0;
            int?start     = 0;

            int requestedVal;

            if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requestedVal) && requestedVal > 0)
            {
                requested = 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 item = GetItemFromObjectId(id, user);

            var totalCount = 0;

            if (string.Equals(flag, "BrowseMetadata"))
            {
                var folder = item as Folder;

                if (folder == null)
                {
                    result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, item, deviceId, filter));
                }
                else
                {
                    var childrenResult = (await GetChildrenSorted(folder, user, sortCriteria, start, requested).ConfigureAwait(false));
                    totalCount = childrenResult.TotalRecordCount;

                    result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, folder, totalCount, filter));
                }
                provided++;
            }
            else
            {
                var folder = (Folder)item;

                var childrenResult = (await GetChildrenSorted(folder, user, sortCriteria, start, requested).ConfigureAwait(false));
                totalCount = childrenResult.TotalRecordCount;

                provided = childrenResult.Items.Length;

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

                        result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, f, childCount, filter));
                    }
                    else
                    {
                        result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, i, 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))
            });
        }
Example #2
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(_config.GetDlnaConfiguration(), 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(_config.GetDlnaConfiguration(), 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))
            });
        }