Пример #1
0
        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 InitializeTransferItem(IDownloadingFile item)
        {
            var previousStatus = item.DownloadInfo.Status;

            item.DownloadInfo
            .Where(notification =>
            {
                var result     = notification.Status != previousStatus;
                previousStatus = notification.Status;
                return(result);
            })
            .ObserveOn(Dispatcher)
            .Subscribe(_ =>
            {
                TransferTasks.Remove(item);
                TransferTasks.Add(item);
            });

            item.DownloadInfo
            .Where(notification => notification.Status == TransferStatus.Disposed)
            .ObserveOn(Dispatcher)
            .Subscribe(
                _ => TransferTasks.Remove(item),
                () =>
            {
                TransferTasks.Remove(item);
                EventAggregator
                .GetEvent <TransferItemCompletedEvent>()
                .Publish(LocalDiskFile.Create(item));
            });

            TransferTasks.Add(item);
        }
        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 void OnLoaded()
        {
            if (TransferTasks == null)
            {
                TransferTasks = new ObservableSortedCollection <IDownloadingFile>(DownloadingFileComparer);

                AcceleriderUser
                .GetNetDiskUsers()
                .SelectMany(item => item.GetDownloadingFiles())
                .ForEach(item =>
                {
                    if (item.DownloadInfo.Status != TransferStatus.Completed)
                    {
                        InitializeTransferItem(item);
                    }
                    else
                    {
                        EventAggregator
                        .GetEvent <TransferItemCompletedEvent>()
                        .Publish(LocalDiskFile.Create(item));
                    }
                });
            }
        }