コード例 #1
0
        private async Task <IEnumerable <CatalogItemModel> > ReadAsync(string path)
        {
            List <CatalogItemModel> items = null;

            try
            {
                if (_skyDrive == null)
                {
                    _skyDrive = await _liveLogin.Login();
                }

                if (_skyDrive == null)
                {
                    throw new CatalogAuthorizationException(CatalogType.SkyDrive, path);
                }

                ChangeAccessToCatalog();

                _cancelSource = new CancellationTokenSource();
                var e = await _skyDrive.GetAsync(path, _cancelSource.Token);

                var skyDriveItems = new List <SkyDriveItem>();
                var data          = (List <object>)e.Result["data"];
                foreach (IDictionary <string, object> content in data)
                {
                    var          type = (string)content["type"];
                    SkyDriveItem item;
                    if (type == "folder")
                    {
                        item = new SkyDriveFolder
                        {
                            Id   = (string)content["id"],
                            Name = (string)content["name"]
                        };
                    }
                    else if (type == "file")
                    {
                        var name = (string)content["name"];

                        if (string.IsNullOrEmpty(_downloadController.GetBookType(name)))
                        {
                            continue;
                        }

                        item = new SkyDriveFile
                        {
                            Id   = (string)content["id"],
                            Name = name
                        };
                    }
                    else
                    {
                        continue;
                    }

                    skyDriveItems.Add(item);
                }

                var folders = skyDriveItems
                              .OfType <SkyDriveFolder>()
                              .Select(i => new CatalogItemModel
                {
                    Title   = i.Name,
                    OpdsUrl = i.Id
                });

                var files = skyDriveItems
                            .OfType <SkyDriveFile>()
                            .Select(file => new CatalogBookItemModel
                {
                    Title   = file.Name,
                    OpdsUrl = file.Id,
                    Links   = new List <BookDownloadLinkModel>
                    {
                        new BookDownloadLinkModel
                        {
                            Url  = file.Id,
                            Type = file.Name
                        }
                    },
                    Id = file.Id
                });

                items = Enumerable.Union(folders, files).ToList();
            }
            catch (OperationCanceledException)
            {
            }
            catch (LiveAuthException e)
            {
                if (e.ErrorCode == "access_denied")
                {
                    throw new CatalogAuthorizationException(e.Message, e, CatalogType.SkyDrive);
                }
                throw new ReadCatalogException(e.Message, e);
            }
            catch (Exception e)
            {
                throw new ReadCatalogException(e.Message, e);
            }

            return(items ?? new List <CatalogItemModel>());
        }