private void IDownloader_DownloadStatusChanged(object sender, DownloadEventArgs e)
        {
            UpdateActiveDownloads();
            if (e.Target.Progress.Status == DownloadStatus.Completed)
            {
                downloads.Remove(e.Target);
                PostProcessors.PostProcessorManager.Instance.Run(e.Target);
            }
            else if (e.Target.Progress.Status == DownloadStatus.Failed)
            {
                Log.WriteLine($"Download failed: {e.Target.DownloadUri}");
                if (File.Exists(e.Target.GetDownloadingPath()))
                {
                    File.Delete(e.Target.GetDownloadingPath());
                }
            }

            if (e.Target.Progress.Status == DownloadStatus.Canceled)
            {
                downloads.Remove(e.Target);
            }

            DownloadStatusChanged?.Invoke(this, e);
            SaveDownloads();
        }
 public void Start()
 {
     if (cancellationToken == null)
     {
         StartWorkerAsync();
         CurrentDownloadItem.Progress.Status = DownloadStatus.Downloading;
         DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
     }
 }
 public void Cancel()
 {
     if (cancellationToken != null)
     {
         cancellationToken.Cancel();
     }
     CurrentDownloadItem.Progress.Status = DownloadStatus.Canceled;
     DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
 }
        public virtual void Resume()
        {
            if (CurrentDownloadItem.Progress.Status != DownloadStatus.Paused)
            {
                throw new InvalidOperationException();
            }

            downloader.Resume();
            CurrentDownloadItem.Progress.Status = DownloadStatus.Downloading;
            DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
        }
 protected void Downloader_StatusChanged(object sender, EventArgs e)
 {
     if (downloader.IsDone())
     {
         CurrentDownloadItem.Progress.Status = DownloadStatus.Completed;
         DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
     }
     if (downloader.HasFailed())
     {
         CurrentDownloadItem.Progress.Status = DownloadStatus.Failed;
         DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
     }
 }
        public void Pause()
        {
            if (cancellationToken != null)
            {
                if (CurrentDownloadItem.Progress.Status != DownloadStatus.Downloading && CurrentDownloadItem.Progress.Status != DownloadStatus.Pending)
                {
                    throw new InvalidOperationException();
                }

                cancellationToken.Cancel();

                CurrentDownloadItem.Progress.Status = DownloadStatus.Paused;
                DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
            }
        }
        public void SetItem(IDownloadItem item)
        {
            // I keep making this mistake - the ParseFrom method can return a new IDownloadItem, so then referencing `item` after
            // that references the old one, and so setting the values on that doesn't help anything...
            CurrentSSDownloadItem = SoulseekDownloadItem.ParseFrom(item);
            CurrentSSDownloadItem.DownloadingFilename = HttpUtility.UrlDecode(CurrentSSDownloadItem.DownloadUri.Segments.Last());
            CurrentSSDownloadItem.CompletedFilename   = CurrentSSDownloadItem.DownloadingFilename;

            CurrentSSDownloadItem.Progress = new DownloadProgress
            {
                BytesTotal      = 0,
                BytesDownloaded = 0,
                Status          = DownloadStatus.Pending
            };

            DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
        }
        protected virtual void SetItem(IDownloadItem item, Uri actualDownloadUri)
        {
            CurrentDownloadItem      = item;
            item.DownloadingFilename = HttpUtility.UrlDecode(actualDownloadUri.Segments.Last());
            item.CompletedFilename   = item.DownloadingFilename;

            downloader = new PausableEventedDownloader(actualDownloadUri, item.GetDownloadingPath());
            downloader.ProgressChanged          += Downloader_ProgressChanged;
            downloader.StatusChanged            += Downloader_StatusChanged;
            downloader.ReceivedResponseFilename += Downloader_ReceivedResponseFilename;
            item.Progress = new DownloadProgress
            {
                BytesTotal      = 0,
                BytesDownloaded = 0,
                Status          = DownloadStatus.Pending
            };

            // Now it's ready, invoke the handler to get download manager to start (or update)
            DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
        }
Exemple #9
0
 private void DownloadHandler_DownloadUpdated(object sender, EventArgs e)
 {
     DownloadStatusChanged?.Invoke(this, e);
 }
 private void ClearDownloadStatus(int id)
 {
     DownloadStatusChanged?.Invoke(id, "", "", -1);
 }
 private void ChangeDownloadStatus(int id, string episodeName, float fraction)
 {
     DownloadStatusChanged?.Invoke(id, episodeName, "", fraction);
 }
 private void ChangeDownloadStatus(int id, string episodeName, string status)
 {
     DownloadStatusChanged?.Invoke(id, episodeName, status, -1);
 }
 public virtual void Cancel()
 {
     downloader.Stop();
     CurrentDownloadItem.Progress.Status = DownloadStatus.Canceled;
     DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
 }
        private async void StartWorkerAsync()
        {
            if (cancellationToken != null)
            {
                throw new InvalidOperationException("Cannot start download when cancellation token already exists");
            }

            cancellationToken = new CancellationTokenSource();
            CurrentDownloadItem.Progress.Status = DownloadStatus.Downloading;

            FileStream fileStream      = null;
            Exception  thrownException = null;

            try
            {
                FileMode mode = FileMode.CreateNew;

                long progress = 0;
                if (System.IO.File.Exists(CurrentDownloadItem.GetDownloadingPath()))
                {
                    mode     = FileMode.Append;
                    progress = new FileInfo(CurrentDownloadItem.GetDownloadingPath()).Length;
                }

                fileStream = new FileStream(CurrentDownloadItem.GetDownloadingPath(), mode);

                var task = SoulseekWrapper.Instance.GetClient()?.DownloadAsync(
                    username: CurrentSSDownloadItem.SoulseekUsername,
                    filename: CurrentSSDownloadItem.SoulseekFilename,
                    outputStream: fileStream,
                    startOffset: progress,
                    cancellationToken: cancellationToken.Token,
                    options: new TransferOptions(
                        stateChanged: (e) =>
                {
                    switch (e.Transfer.State)
                    {
                    case TransferStates.TimedOut:
                    case TransferStates.Rejected:
                    case TransferStates.Errored:
                    case TransferStates.Cancelled:
                        CurrentDownloadItem.Progress.Status = DownloadStatus.Failed;
                        DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
                        break;

                    case TransferStates.Completed:
                        Log.WriteLine($"Finished {CurrentDownloadItem.DownloadUri}");
                        break;

                    default:
                        break;
                    }
                },
                        progressUpdated: (e) =>
                {
                    CurrentDownloadItem.Progress.BytesDownloaded = e.Transfer.BytesTransferred;
                    CurrentDownloadItem.Progress.BytesTotal      = e.Transfer.Size;
                    CurrentDownloadItem.Progress.SpeedTracker.SetProgress(e.Transfer.BytesTransferred);
                    DownloadProgressChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
                }
                        )
                    );
                if (task == null)
                {
                    // TODO...
                    Log.WriteLine("Did not create task - assuming Soulseek authentication has not been set");
                    throw new InvalidOperationException("Unable to create download task");
                }
                await task;
            }
            catch (Exception ex)
            {
                Log.WriteLine($"Exception occured during soulseek download for {CurrentSSDownloadItem.SoulseekFilename}: {ex.Message}\n--> Stack trace: {ex.StackTrace}");
                thrownException = ex;
            }
            finally
            {
                // Dispose of resources
                fileStream?.Dispose();
                cancellationToken = null;

                if (thrownException != null)
                {
                    // Failed
                    CurrentDownloadItem.Progress.Status = DownloadStatus.Failed;
                    DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
                }
                else
                {
                    // Success
                    CurrentDownloadItem.Progress.Status = DownloadStatus.Completed;
                    DownloadStatusChanged?.Invoke(this, new DownloadEventArgs(CurrentDownloadItem));
                }
            }
        }