コード例 #1
0
        public void SaveData(YandexDiskData data)
        {
            if (!_map.TryGetValue(data.UserId, out _))
            {
                data.Started = DateTimeOffset.UtcNow;
                _map.Add(data.UserId, data);
            }

            var values = _map.Values.ToList();

            var fileContents = JsonConvert.SerializeObject(values);

            File.WriteAllText(FileName, fileContents);

            _logger.LogInformation("Directory: " + Directory.GetCurrentDirectory());
            _logger.LogInformation("Saved");
        }
コード例 #2
0
        public async IAsyncEnumerable <YandexDiskFileInfo> DownloadFilesAsync(
            IUserIdentifier userIdentifier,
            string accessToken,
            [EnumeratorCancellation] CancellationToken cancellationToken,
            StoppingAction stoppingAction)
        {
            var apiClient = new ApiClient(accessToken, Client);

            _data = _yandexDiskDownloadStateService.GetData(userIdentifier.UserId);
            if (_data == null)
            {
                _data = new YandexDiskData {
                    UserId = userIdentifier.UserId, YandexDiskAccessToken = accessToken
                };
            }
            else
            {
                _currentOffset = _data.CurrentIndex;
            }

            var disk = await WrapApiCallAsync(() => apiClient.GetDiskAsync(cancellationToken));

            bool firstIteration = true;

            while (_currentOffset <= _totalFiles || firstIteration)
            {
                var resource =
                    await WrapApiCallAsync(() =>
                                           apiClient.GetResourceAsync(disk.SystemFolders.Photostream, cancellationToken, _currentOffset, Limit));

                _totalFiles = resource.Embedded.Total;

                var items = resource.Embedded.Items;
                if (items != null && items.Length > 0)
                {
                    foreach (var item in items)
                    {
                        if (cancellationToken.IsCancellationRequested || stoppingAction.IsStopRequested)
                        {
                            _logger.LogInformation("Cancellation requested.");
                            yield break;
                        }

                        if (item.MediaType == "video")
                        {
                            _currentOffset++;
                            continue;
                        }

                        var entity = await DownloadAsync(item, disk);

                        if (entity == null)
                        {
                            yield break;
                        }

                        _currentOffset++;

                        _progressReporter.Report(userIdentifier, _currentOffset, _totalFiles);

                        yield return(entity);
                    }
                }

                firstIteration = false;
            }
        }