コード例 #1
0
        private void HandleSearch(XmlWriter xmlWriter, IDictionary <string, string> sparams, string deviceId)
        {
            var searchCriteria = new SearchCriteria(GetValueOrDefault(sparams, "SearchCriteria", ""));
            var sortCriteria   = new SortCriteria(GetValueOrDefault(sparams, "SortCriteria", ""));
            var filter         = new Filter(GetValueOrDefault(sparams, "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;

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

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

            QueryResult <BaseItem> childrenResult;

            using (StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8))
            {
                var settings = new XmlWriterSettings()
                {
                    Encoding           = Encoding.UTF8,
                    CloseOutput        = false,
                    OmitXmlDeclaration = true,
                    ConformanceLevel   = ConformanceLevel.Fragment
                };

                using (var writer = XmlWriter.Create(builder, settings))
                {
                    writer.WriteStartElement(string.Empty, "DIDL-Lite", NS_DIDL);

                    writer.WriteAttributeString("xmlns", "dc", null, NS_DC);
                    writer.WriteAttributeString("xmlns", "dlna", null, NS_DLNA);
                    writer.WriteAttributeString("xmlns", "upnp", null, NS_UPNP);

                    DidlBuilder.WriteXmlRootAttributes(_profile, writer);

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

                    var item = serverItem.Item;

                    childrenResult = GetChildrenSorted(item, _user, searchCriteria, sortCriteria, start, requestedCount);

                    var dlnaOptions = _config.GetDlnaConfiguration();

                    foreach (var i in childrenResult.Items)
                    {
                        if (i.IsDisplayedAsFolder)
                        {
                            var childCount = GetChildrenSorted(i, _user, searchCriteria, sortCriteria, null, 0)
                                             .TotalRecordCount;

                            _didlBuilder.WriteFolderElement(writer, i, null, item, childCount, filter);
                        }
                        else
                        {
                            _didlBuilder.WriteItemElement(writer, i, _user, item, serverItem.StubType, deviceId, filter);
                        }
                    }

                    writer.WriteFullEndElement();
                }

                xmlWriter.WriteElementString("Result", builder.ToString());
            }

            xmlWriter.WriteElementString("NumberReturned", childrenResult.Items.Count.ToString(CultureInfo.InvariantCulture));
            xmlWriter.WriteElementString("TotalMatches", childrenResult.TotalRecordCount.ToString(CultureInfo.InvariantCulture));
            xmlWriter.WriteElementString("UpdateID", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
        }
コード例 #2
0
        private IEnumerable <KeyValuePair <string, string> > HandleSearch(IDictionary <string, string> sparams, User user, string deviceId)
        {
            var searchCriteria = new SearchCriteria(GetValueOrDefault(sparams, "SearchCriteria", ""));
            var sortCriteria   = new SortCriteria(GetValueOrDefault(sparams, "SortCriteria", ""));
            var filter         = new Filter(GetValueOrDefault(sparams, "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 settings = new XmlWriterSettings
            {
                Encoding           = Encoding.UTF8,
                CloseOutput        = false,
                OmitXmlDeclaration = true,
                ConformanceLevel   = ConformanceLevel.Fragment
            };

            StringWriter builder    = new StringWriterWithEncoding(Encoding.UTF8);
            int          totalCount = 0;
            int          provided   = 0;

            using (XmlWriter writer = XmlWriter.Create(builder, settings))
            {
                //writer.WriteStartDocument();

                writer.WriteStartElement(string.Empty, "DIDL-Lite", NS_DIDL);

                writer.WriteAttributeString("xmlns", "dc", null, NS_DC);
                writer.WriteAttributeString("xmlns", "dlna", null, NS_DLNA);
                writer.WriteAttributeString("xmlns", "upnp", null, NS_UPNP);
                //didl.SetAttribute("xmlns:sec", NS_SEC);

                DidlBuilder.WriteXmlRootAttributes(_profile, writer);

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

                var item = serverItem.Item;

                var childrenResult = (GetChildrenSorted(item, user, searchCriteria, sortCriteria, start, requestedCount));

                totalCount = childrenResult.TotalRecordCount;

                provided = childrenResult.Items.Length;

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

                        _didlBuilder.WriteFolderElement(writer, i, null, item, childCount, filter);
                    }
                    else
                    {
                        _didlBuilder.WriteItemElement(_config.GetDlnaConfiguration(), writer, i, user, item, serverItem.StubType, deviceId, filter);
                    }
                }

                writer.WriteFullEndElement();
                //writer.WriteEndDocument();
            }

            var resXML = builder.ToString();

            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))
            });
        }
コード例 #3
0
        private void HandleBrowse(XmlWriter xmlWriter, IDictionary <string, string> sparams, string deviceId)
        {
            var id           = sparams["ObjectID"];
            var flag         = sparams["BrowseFlag"];
            var filter       = new Filter(GetValueOrDefault(sparams, "Filter", "*"));
            var sortCriteria = new SortCriteria(GetValueOrDefault(sparams, "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;

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

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

            int totalCount;

            using (StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8))
            {
                var settings = new XmlWriterSettings()
                {
                    Encoding           = Encoding.UTF8,
                    CloseOutput        = false,
                    OmitXmlDeclaration = true,
                    ConformanceLevel   = ConformanceLevel.Fragment
                };

                using (var writer = XmlWriter.Create(builder, settings))
                {
                    writer.WriteStartElement(string.Empty, "DIDL-Lite", NS_DIDL);

                    writer.WriteAttributeString("xmlns", "dc", null, NS_DC);
                    writer.WriteAttributeString("xmlns", "dlna", null, NS_DLNA);
                    writer.WriteAttributeString("xmlns", "upnp", null, NS_UPNP);

                    DidlBuilder.WriteXmlRootAttributes(_profile, writer);

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


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

                        if (item.IsDisplayedAsFolder || serverItem.StubType.HasValue)
                        {
                            var childrenResult = GetUserItems(item, serverItem.StubType, _user, sortCriteria, start, requestedCount);

                            _didlBuilder.WriteFolderElement(writer, item, serverItem.StubType, null, childrenResult.TotalRecordCount, filter, id);
                        }
                        else
                        {
                            var dlnaOptions = _config.GetDlnaConfiguration();
                            _didlBuilder.WriteItemElement(writer, item, _user, null, null, deviceId, filter);
                        }

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

                        provided = childrenResult.Items.Count;

                        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
                            {
                                _didlBuilder.WriteItemElement(writer, childItem, _user, item, serverItem.StubType, deviceId, filter);
                            }
                        }
                    }

                    writer.WriteFullEndElement();
                }

                xmlWriter.WriteElementString("Result", builder.ToString());
            }

            xmlWriter.WriteElementString("NumberReturned", provided.ToString(CultureInfo.InvariantCulture));
            xmlWriter.WriteElementString("TotalMatches", totalCount.ToString(CultureInfo.InvariantCulture));
            xmlWriter.WriteElementString("UpdateID", _systemUpdateId.ToString(CultureInfo.InvariantCulture));
        }