Exemple #1
0
        /// <summary>
        /// Attempts to download a song to the specified <see cref="DownloadContainer"/>.
        /// </summary>
        /// <param name="downloadContainer">Target container for the download.</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <DownloadResult> DownloadSongAsync(DownloadContainer downloadContainer, CancellationToken cancellationToken)
        {
            string?stringForUri = null;
            Uri    downloadUri;

            try
            {
                if (SongHash != null && SongHash.Length > 0)
                {
                    stringForUri = BeatSaverHashDownloadUrlBase + SongHash.ToLower();
                }
                else if (SongKey != null && SongKey.Length > 0)
                {
                    stringForUri = BeatSaverKeyDownloadUrlBase + SongKey.ToLower();
                }
                else
                {
                    return(new DownloadResult(null, DownloadResultStatus.InvalidRequest, 0,
                                              "No SongHash or SongKey provided to the DownloadJob.",
                                              new InvalidOperationException($"No SongHash or SongKey provided to the DownloadJob")));
                }
                downloadUri = new Uri(stringForUri);
            }
            catch (FormatException ex)
            {
                return(new DownloadResult(null, DownloadResultStatus.InvalidRequest, 0, $"Could not create a valid Uri from '{stringForUri}'.", ex));
            }
            DownloadResult result = await FileIO.DownloadFileAsync(downloadUri, downloadContainer, cancellationToken).ConfigureAwait(false);

            return(result);
        }
Exemple #2
0
        public async Task <DownloadResult> RunAsync(CancellationToken cancellationToken)
        {
            _runCancellationToken = cancellationToken;
            Status = DownloadJobStatus.Downloading;

            try
            {
                cancellationToken.ThrowIfCancellationRequested();
                if (Paused)
                {
                    Status = DownloadJobStatus.Paused;
                    await WaitUntil(() => !Paused, cancellationToken).ConfigureAwait(false);

                    cancellationToken.ThrowIfCancellationRequested();
                    Status = DownloadJobStatus.Downloading;
                }
                EventHandler <DownloadJobStartedEventArgs> jobStartedHandler = JobStarted;
                jobStartedHandler?.Invoke(this, new DownloadJobStartedEventArgs(SongHash, SongKey, SongName, LevelAuthorName));

                _downloadResult = await DownloadSongAsync(_downloadContainer, cancellationToken).ConfigureAwait(false);

                if (_downloadResult.Exception != null)
                {
                    throw _downloadResult.Exception;
                }
            }
            catch (OperationCanceledException ex)
            {
                return(await FinishJob(ex).ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                string message = $"Error in DownloadJob.RunAsync: {ex.Message}";
                Logger.log?.Warn(message);
                Logger.log?.Debug(ex.StackTrace);
                if (_downloadResult == null)
                {
                    _downloadResult = new DownloadResult(null, DownloadResultStatus.Unknown, 0, message, ex);
                }
                return(await FinishJob(ex).ConfigureAwait(false));
            }
            // Finish
            return(await FinishJob().ConfigureAwait(false));
        }
Exemple #3
0
 public DownloadJobFinishedEventArgs(string songHash, DownloadResult jobResult)
 {
     SongHash          = songHash;
     DownloadResult    = jobResult.Status;
     DownloadContainer = jobResult?.DownloadContainer;
 }