public void SaveState(DropboxDownloadState downloadState) { if (!_map.TryGetValue(downloadState.AccountId, out _)) { downloadState.Started = DateTimeOffset.UtcNow; _map.Add(downloadState.AccountId, downloadState); } var values = _map.Values.ToList(); var fileContents = JsonConvert.SerializeObject(values); File.WriteAllText(FileName, fileContents); _logger.LogInformation("Directory: " + Directory.GetCurrentDirectory()); _logger.LogInformation("Saved"); }
public async IAsyncEnumerable <DropboxFileInfo> DownloadAsync( IUserIdentifier userIdentifier, string apiToken, StoppingAction stoppingAction, [EnumeratorCancellation] CancellationToken cancellationToken) { CreateClient(apiToken); var account = await WrapApiCallAsync(_dropboxClient.Users.GetCurrentAccountAsync); _state = _stateService.GetState(account.AccountId); if (_state != null) { _lastProcessedFileIndex = _state.LastProcessedFileIndex; } else { _state = new DropboxDownloadState { Started = DateTimeOffset.Now, AccountId = account.AccountId } }; bool firstIteration = true; var filesMetadata = new List <Metadata>(); var listFolderResult = await WrapApiCallAsync(() => _dropboxClient.Files.ListFolderAsync(CameraUploads, limit: Limit)); while (listFolderResult.HasMore || firstIteration) { if (!firstIteration) { listFolderResult = await WrapApiCallAsync(() => _dropboxClient.Files.ListFolderContinueAsync(listFolderResult.Cursor)); } filesMetadata.AddRange(listFolderResult.Entries.Where(a => a is FileMetadata)); firstIteration = false; } _state.TotalFiles = filesMetadata.Count; var index = _lastProcessedFileIndex > -1 ? _lastProcessedFileIndex : 0; for (int i = index; i < filesMetadata.Count; i++) { if (cancellationToken.IsCancellationRequested || stoppingAction.IsStopRequested) { _logger.LogInformation("Cancellation requested."); yield break; } var fileMetadata = filesMetadata[i]; var dropboxFile = await DownloadFileAsync(fileMetadata, account); if (dropboxFile == null) { yield break; } _state.LastProcessedFileIndex++; _state.LastProcessedFileId = dropboxFile.FileId; _progressReporter.Report(userIdentifier, _state.LastProcessedFileIndex, _state.TotalFiles); yield return(dropboxFile); } }