public DownloadResult(string path, DownloadResultStatus status, int httpStatus, string reason = null, Exception exception = null) { FilePath = path; Status = status; HttpStatusCode = httpStatus; Reason = reason; Exception = exception; }
public DownloadResult(DownloadContainer?container, DownloadResultStatus status, int httpStatus, string?reason = null, Exception?exception = null) { DownloadContainer = container; Status = status; HttpStatusCode = httpStatus; Reason = reason; Exception = exception; }
public static void ProcessFinishedJobs(IEnumerable <IJob> jobs, IEnumerable <IPlaylist> playlists, IEnumerable <IPlaylist> recentPlaylists) { TimeSpan offset = new TimeSpan(0, 0, 0, 0, 1); DateTime addedTime = DateTime.Now - offset; foreach (IJob?job in jobs.Where(j => j.DownloadResult != null && j.DownloadResult.Status != DownloadResultStatus.NetNotFound)) { foreach (IPlaylist?playlist in playlists) { IPlaylistSong?addedSong = playlist.Add(job.Song); if (addedSong != null) { addedSong.DateAdded = addedTime; } } DownloadResultStatus downloadStatus = job.Result?.DownloadResult?.Status ?? DownloadResultStatus.Unknown; if (downloadStatus == DownloadResultStatus.Success) { foreach (IPlaylist?playlist in recentPlaylists) { IPlaylistSong?addedSong = playlist.Add(job.Song); if (addedSong != null) { addedSong.DateAdded = addedTime; } } } } foreach (IPlaylist?playlist in playlists) { playlist.Sort(); playlist.RaisePlaylistChanged(); } foreach (IPlaylist?playlist in recentPlaylists) { playlist.Sort(); playlist.RaisePlaylistChanged(); } }
/// <summary> /// Gets the path to the provided playlist file name. TODO: This needs work /// </summary> /// <param name="fileName"></param> /// <returns></returns> //public static string GetPlaylistFilePath(string fileName, bool getDisabled = false) //{ // if (File.Exists(fileName)) // return Path.GetFullPath(fileName); // var path = Path.Combine(PlaylistManager.PlaylistPath, fileName); // if (File.Exists(path)) // return Path.GetFullPath(path); // else if (!getDisabled) // return null; // path = Path.Combine(PlaylistManager.DisabledPlaylistsPath, fileName); // if (string.IsNullOrEmpty(path)) // return null; // if (File.Exists(path)) // return path; // return null; //} /// <summary> /// Downloads a file from the specified URI to the specified <see cref="DownloadContainer"/>. /// All exceptions are stored in the DownloadResult. /// </summary> /// <param name="downloadUri"></param> /// <param name="downloadContainer"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public static async Task <DownloadResult> DownloadFileAsync(Uri downloadUri, DownloadContainer downloadContainer, CancellationToken cancellationToken) { int statusCode = 0; if (downloadUri == null) { return(new DownloadResult(null, DownloadResultStatus.InvalidRequest, 0)); } //if (!overwriteExisting && File.Exists(target)) // return new DownloadResult(null, DownloadResultStatus.IOFailed, 0); try { using (IWebResponseMessage? response = await SongFeedReaders.WebUtils.GetBeatSaverAsync(downloadUri, cancellationToken, 30, 2).ConfigureAwait(false)) { statusCode = response?.StatusCode ?? 0; if (response == null) { throw new WebClientException($"Response was null for '{downloadUri}'."); } if (response.Content == null) { throw new WebClientException($"Response's content was null for '{downloadUri}'."); } try { await downloadContainer.ReceiveDataAsync(response.Content, cancellationToken).ConfigureAwait(false); //actualPath = await response.Content.ReadAsFileAsync(target, overwriteExisting, cancellationToken).ConfigureAwait(false); } catch (IOException ex) { // Also catches DirectoryNotFoundException return(new DownloadResult(null, DownloadResultStatus.IOFailed, statusCode, response.ReasonPhrase, ex)); } catch (InvalidOperationException ex) { // File already exists and overwrite is false. return(new DownloadResult(null, DownloadResultStatus.IOFailed, statusCode, response.ReasonPhrase, ex)); } catch (OperationCanceledException) { throw; } catch (Exception ex) { return(new DownloadResult(null, DownloadResultStatus.NetFailed, statusCode, response?.ReasonPhrase, ex)); } } } catch (WebClientException ex) { int faultedCode = ex.Response?.StatusCode ?? 0; DownloadResultStatus downloadResultStatus = DownloadResultStatus.NetFailed; if (faultedCode == 404) { downloadResultStatus = DownloadResultStatus.NetNotFound; } return(new DownloadResult(null, downloadResultStatus, faultedCode, ex.Response?.ReasonPhrase, ex.Response?.Exception)); } catch (OperationCanceledException ex) { return(new DownloadResult(null, DownloadResultStatus.Canceled, 0, ex?.Message, ex)); } catch (Exception ex) { return(new DownloadResult(null, DownloadResultStatus.NetFailed, 0, ex?.Message, ex)); } return(new DownloadResult(downloadContainer, DownloadResultStatus.Success, statusCode)); }
public DownloadJobFinishedEventArgs(string songHash, DownloadResultStatus downloadResult, DownloadContainer?downloadContainer) { SongHash = songHash; DownloadResult = downloadResult; DownloadContainer = downloadContainer; }
/// <summary> /// Downloads a file from the specified URI to the specified path (path includes file name). /// Creates the target directory if it doesn't exist. All exceptions are stored in the DownloadResult. /// </summary> /// <param name="uri"></param> /// <param name="path"></param> /// <exception cref="OperationCanceledException"></exception> /// <returns></returns> public static async Task <DownloadResult> DownloadFileAsync(Uri uri, string path, CancellationToken cancellationToken, bool overwrite = true) { string actualPath = path; int statusCode = 0; if (uri == null) { return(new DownloadResult(null, DownloadResultStatus.InvalidRequest, 0)); } if (!overwrite && File.Exists(path)) { return(new DownloadResult(null, DownloadResultStatus.IOFailed, 0)); } try { using (IWebResponseMessage response = await SongFeedReaders.WebUtils.GetBeatSaverAsync(uri, cancellationToken, 30, 2).ConfigureAwait(false)) { statusCode = response?.StatusCode ?? 0; try { Directory.GetParent(path).Create(); actualPath = await response.Content.ReadAsFileAsync(path, overwrite, cancellationToken).ConfigureAwait(false); } catch (IOException ex) { // Also catches DirectoryNotFoundException return(new DownloadResult(null, DownloadResultStatus.IOFailed, statusCode, response.ReasonPhrase, ex)); } catch (InvalidOperationException ex) { // File already exists and overwrite is false. return(new DownloadResult(null, DownloadResultStatus.IOFailed, statusCode, response.ReasonPhrase, ex)); } catch (OperationCanceledException) { throw; } catch (Exception ex) { return(new DownloadResult(null, DownloadResultStatus.Unknown, statusCode, response?.ReasonPhrase, ex)); } } } catch (WebClientException ex) { int faultedCode = ex.Response?.StatusCode ?? 0; DownloadResultStatus downloadResultStatus = DownloadResultStatus.NetFailed; if (faultedCode == 404) { downloadResultStatus = DownloadResultStatus.NetNotFound; } return(new DownloadResult(null, downloadResultStatus, faultedCode, ex.Response?.ReasonPhrase, ex.Response?.Exception)); } catch (OperationCanceledException ex) { return(new DownloadResult(null, DownloadResultStatus.Canceled, 0, ex?.Message, ex)); } catch (Exception ex) { return(new DownloadResult(null, DownloadResultStatus.NetFailed, 0, ex?.Message, ex)); } return(new DownloadResult(actualPath, DownloadResultStatus.Success, statusCode)); }