Exemplo n.º 1
0
        public async Task <StorageSearchResult> Resolve(HttpResponseMessage response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            if (response.Content.Headers.ContentLength == null || response.Content.Headers.ContentLength < 1)
            {
                return(null);
            }

            StorageSearchResult result = null;
            var contentStream          = await response.Content.ReadAsStreamAsync();

            this.ResolveCore(contentStream, (bucketName, pattern, marker) =>
            {
                result = new StorageSearchResult(bucketName, pattern, marker, this);
                return(result);
            });

            return(result);
        }
Exemplo n.º 2
0
        public string Reload(StorageSearchResult owner, HttpResponseMessage response)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            if (response.Content.Headers.ContentLength == null || response.Content.Headers.ContentLength < 1)
            {
                return(null);
            }

            return(this.ResolveCore(response.Content.ReadAsStreamAsync().Result, (_, __, ___) => owner));
        }
Exemplo n.º 3
0
        private string ResolveCore(Stream stream, Func <string, string, string, StorageSearchResult> thunk)
        {
            if (stream == null)
            {
                return(null);
            }

            var settings = new XmlReaderSettings()
            {
                IgnoreComments = true,
                IgnoreProcessingInstructions = true,
                IgnoreWhitespace             = true,
            };

            StorageSearchResult owner = null;
            string name = null, pattern = null, marker = null;

            using (var reader = XmlReader.Create(stream, settings))
            {
                if (reader.MoveToContent() != XmlNodeType.Element)
                {
                    return(null);
                }

                while (reader.Read())
                {
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }

                    switch (reader.LocalName)
                    {
                    case "Name":
                        name = Utility.Xml.ReadContentAsString(reader);
                        break;

                    case "Prefix":
                        pattern = Utility.Xml.ReadContentAsString(reader);
                        break;

                    case "NextMarker":
                        marker = Utility.Xml.ReadContentAsString(reader);
                        break;

                    case "Contents":
                        var dictionary = this.ResolveContent(reader);

                        if (dictionary != null && dictionary.Count > 0)
                        {
                            if (owner == null)
                            {
                                owner = thunk(name, pattern, marker);
                            }

                            owner.Append(dictionary["Key"],
                                         Zongsoft.Common.Convert.ConvertValue <long>(dictionary["Size"]),
                                         Zongsoft.Common.Convert.ConvertValue <DateTimeOffset>(dictionary["LastModified"]));
                        }

                        break;

                    case "CommonPrefixes":
                        var prefix = this.ResolveCommonPrefix(reader);

                        if (!string.IsNullOrWhiteSpace(prefix))
                        {
                            if (owner == null)
                            {
                                owner = thunk(name, pattern, marker);
                            }

                            owner.Append(prefix, 0, DateTimeOffset.MinValue);
                        }

                        break;
                    }
                }
            }

            return(marker);
        }