public async Task <ResourceLoadStatus> FetchChildResourcesAsync(ResourceItem parent, CancellationToken cancellationToken, int maxLevel, int level)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(ResourceLoadStatus.OperationCanceled);
            }

            if (level > maxLevel)
            {
                return(ResourceLoadStatus.OperationCanceled);
            }

            Debug.WriteLine("path=[" + parent.FullPath + "]");

            var result = await _client.Propfind(parent.FullPath, new PropfindParameters { CancellationToken = cancellationToken });

            if (result.StatusCode == (int)HttpStatusCode.Unauthorized)
            {
                return(ResourceLoadStatus.Unauthorized);
            }

            if (result.Resources != null)
            {
                var tasks = result.Resources.Skip(1)
                            .Where(r => !r.DisplayName.StartsWith("."))
                            .Select(async r =>
                {
                    Uri fullPath     = OnlinePathBuilder.Combine(_connectionSettings.StorageUri, r.Uri);
                    var resourceItem = new ResourceItem
                    {
                        Id            = Guid.NewGuid(),
                        DisplayName   = r.DisplayName,
                        Extension     = new FileInfo(r.DisplayName).Extension.ToLowerInvariant(),
                        IsCollection  = r.IsCollection,
                        FullPath      = fullPath,
                        ContentLength = r.ContentLength,
                        Parent        = parent
                    };

                    if (r.IsCollection && level < maxLevel)
                    {
                        await FetchChildResourcesAsync(resourceItem, cancellationToken, maxLevel, level + 1);
                    }

                    return(resourceItem);
                });

                var items = await Task.WhenAll(tasks);

                parent.Items = items.OrderBy(r => r.DisplayName).ToList();

                return(ResourceLoadStatus.Ok);
            }

            return(ResourceLoadStatus.NoResourcesFound);
        }
예제 #2
0
        public async Task <ResourceLoadStatus> FetchChildResourcesAsync([NotNull] ResourceItem parent, CancellationToken cancellationToken, int maxLevel, int level)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(ResourceLoadStatus.OperationCanceled);
            }

            if (level > maxLevel)
            {
                return(ResourceLoadStatus.OperationCanceled);
            }

            Debug.WriteLine("path=[" + parent.FullPath + "]");

            var result = await _client.Propfind(parent.FullPath, new PropfindParameters { CancellationToken = cancellationToken });

            if (result.Resources != null)
            {
                var tasks = result.Resources.Skip(1)
                            .Where(r => IsAudioFile(r) || IsFolder(r))
                            .Select(async r =>
                {
                    Uri fullPath     = OnlinePathBuilder.Combine(_connectionSettings.StorageUri, r.Uri);
                    var resourceItem = new ResourceItem
                    {
                        DisplayName   = r.DisplayName,
                        IsCollection  = r.IsCollection,
                        FullPath      = fullPath,
                        ContentLength = r.ContentLength,
                        Parent        = parent
                    };

                    if (r.IsCollection && level < maxLevel)
                    {
                        await FetchChildResourcesAsync(resourceItem, cancellationToken, maxLevel, level + 1);
                    }

                    return(resourceItem);
                });

                var items = await Task.WhenAll(tasks);

                parent.Items = items.OrderBy(r => r.DisplayName).ToList();

                return(ResourceLoadStatus.Ok);
            }

            return(ResourceLoadStatus.NoResourcesFound);
        }