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);

            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 int DownloadingFileComparer(IDownloadingFile x, IDownloadingFile y)
 {
     return(DisplayStatusOrder.ContainsKey(x.DownloadInfo.Status) &&
            DisplayStatusOrder.ContainsKey(y.DownloadInfo.Status)
         ? DisplayStatusOrder[x.DownloadInfo.Status] - DisplayStatusOrder[y.DownloadInfo.Status]
         : x.DownloadInfo.Status - y.DownloadInfo.Status);
 }
 private LocalDiskFile(IDownloadingFile downloadingFile)
 {
     Type          = downloadingFile.File.Type;
     Path          = downloadingFile.DownloadInfo.Context.LocalPath;
     Size          = downloadingFile.DownloadInfo.Context.TotalSize;
     CompletedTime = DateTime.Now;
 }
示例#5
0
 public static string ToJsonString(this IDownloadingFile @this)
 {
     return(new SerializedData
     {
         File = @this.File,
         DownloadInfo = @this.DownloadInfo.ToJObject()
     }.ToJson());
 }
        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();
                }
            }
        }
 private void OperateTaskToken(IDownloadingFile item, Action <IManagedTransporterToken> operation, string errorMessage)
 {
     try
     {
         operation?.Invoke(item.Operations);
     }
     catch
     {
         GlobalMessageQueue.Enqueue(errorMessage);
     }
 }
 private void OperateTaskToken(IDownloadingFile item, Action <IManagedTransporterToken> operation, string errorMessage)
 {
     try
     {
         operation?.Invoke(item.Operations);
     }
     catch (Exception e)
     {
         GlobalMessageQueue.Enqueue(errorMessage);
         Logger.Error($"{errorMessage}: {item.DownloadInfo.Context.LocalPath}", e);
     }
 }
        public static ILocalDiskFile Create(IDownloadingFile file)
        {
            var hash = file.GetHashCode();

            if (_cache.TryGetValue(hash, out ILocalDiskFile result))
            {
                return(result);
            }

            result = new LocalDiskFile(file);
            if (_cache.TryAdd(hash, result) && _cache.Count > 50)
            {
                var firstKey = _cache.FirstOrDefault().Key;
                _cache.TryRemove(firstKey, out _);
            }

            return(result);
        }