private void SaveDownloadingFile(IDownloadingFile result) { _downloadingFiles.Add(result); ArddFilePaths.Add(result.ArddFilePath); result.DownloadInfo.Subscribe(_ => { }, OnCompleted); Subscribe(result.DownloadInfo.Where(item => item.Status == TransferStatus.Suspended || item.Status == TransferStatus.Faulted)); Subscribe(result.DownloadInfo.Sample(TimeSpan.FromMilliseconds(5000))); File.WriteAllText(result.ArddFilePath, result.ToJsonString()); if (result.DownloadInfo.Status == TransferStatus.Completed) { OnCompleted(); } void Subscribe(IObservable <TransferNotification> observable) { observable.Subscribe(_ => File.WriteAllText(result.ArddFilePath, result.ToJsonString())); } void OnCompleted() { if (File.Exists(result.ArddFilePath)) { File.Delete(result.ArddFilePath); } ArddFilePaths.Remove(result.ArddFilePath); var localDiskFile = LocalDiskFile.Create(result); _downloadingFiles.Remove(result); _localDiskFiles.Add(localDiskFile); } }
private void SaveDownloadingFile(IDownloadingFile result) { _downloadingFiles.Add(result); ArddFilePaths.Add(result.ArddFilePath); var disposable1 = Subscribe(result.DownloadInfo.Where(item => item.Status == TransferStatus.Suspended || item.Status == TransferStatus.Faulted)); var disposable2 = Subscribe(result.DownloadInfo.Sample(TimeSpan.FromMilliseconds(5000))); result.DownloadInfo .Where(item => item.Status == TransferStatus.Disposed) .Subscribe(async _ => { await ClearDownloadInfo(true); Logger.Info($"Download Cancelled: {result.DownloadInfo.Context.LocalPath}. "); }, OnCompleted); File.WriteAllText(result.ArddFilePath.EnsureFileFolder(), result.ToJsonString()); if (result.DownloadInfo.Status == TransferStatus.Completed) { OnCompleted(); } async void OnCompleted() { await ClearDownloadInfo(false); var localDiskFile = LocalDiskFile.Create(result); _localDiskFiles.Add(localDiskFile); Logger.Info($"Download Completed: {result.DownloadInfo.Context.LocalPath}. "); } IDisposable Subscribe(IObservable <TransferNotification> observable) { return(observable.Subscribe(_ => File.WriteAllText(result.ArddFilePath, result.ToJsonString()))); } async Task ClearDownloadInfo(bool isCancelled) { disposable1.Dispose(); disposable2.Dispose(); ArddFilePaths.Remove(result.ArddFilePath); _downloadingFiles.Remove(result); await result.ArddFilePath.TryDeleteAsync(); if (isCancelled) { await result.DownloadInfo.Context.LocalPath.TryDeleteAsync(); } } }
public override IReadOnlyList <IDownloadingFile> GetDownloadingFiles() { var downloadingFiles = ArddFilePaths .Where(item => item.EndsWith(ArddFileExtension)) .Where(File.Exists) .Where(item => !_downloadingFiles.Any(file => item.StartsWith(file.DownloadInfo.Context.LocalPath))) .Select(File.ReadAllText) .Select(item => item.ToDownloadingFile(builder => builder .UseSixCloudConfigure() .Configure <RemotePathProvider>(provider => { provider.Owner = this; return(provider); }), this)) .ToArray(); ArddFilePaths.Clear(); downloadingFiles.ForEach(SaveDownloadingFile); return(_downloadingFiles); }