private static DownloadFileTask ExecuteFileActionIfAvailable(DownloadFileTask task) { if (task.DownloadState.IsNewVersionAvailable && task.PostdownloadAction != null) { _logger.Info($"[{Path.GetFileName(task.DownloadState.Url.LocalPath)}] Action found. Triggering..."); task.PostdownloadAction.Trigger(task.DownloadState.DownloadPath); _logger.Info($"[{Path.GetFileName(task.DownloadState.Url.LocalPath)}] Action finished."); } return(task); }
static DownloadFileTask DownloadIfAvailable(DownloadFileTask task) { try { if (task.DownloadState.IsNewVersionAvailable) { _logger.Info($"[{Path.GetFileName(task.DownloadState.Url.LocalPath)}] Update found. Downloading..."); var downloadRequest = WebRequest.CreateDefault(task.DownloadState.Url); downloadRequest.Method = WebRequestMethods.Http.Get; using (WebResponse downloadResponse = downloadRequest.GetResponse()) { task.DownloadState.DownloadPath = GetTargetFilePath(downloadRequest.RequestUri); if (File.Exists(task.DownloadState.DownloadPath)) { File.Delete(task.DownloadState.DownloadPath); } using (WebResponse downloadingResponse = downloadRequest.GetResponse()) using (Stream downloadStream = downloadingResponse.GetResponseStream()) using (var fileStream = new FileStream(task.DownloadState.DownloadPath, FileMode.CreateNew, FileAccess.Write, FileShare.None, bufferSize: /*1 Mb*/ 1024 * 1024)) { int totalReadBytes = 0; while (true) { var readBuffer = new byte[4096]; int readBytes = downloadStream.Read(readBuffer, 0, readBuffer.Length); if (readBytes == 0) { break; } fileStream.Write(readBuffer, 0, readBytes); totalReadBytes += readBytes; } fileStream.Flush(); } } _logger.Info($"[{Path.GetFileName(task.DownloadState.Url.LocalPath)}] File downloaded."); } else { _logger.Info($"[{Path.GetFileName(task.DownloadState.Url.LocalPath)}] No updates."); } } catch (Exception ex) { _logger.Error(ex, $"[{Path.GetFileName(task.DownloadState.Url.LocalPath)}] Downloading failed with error: {ex.Message}."); } return(task); }
private static Uri ResolveUrl(DownloadFileTask task) { if (string.IsNullOrEmpty(task.XPath)) { return(task.Url); } var webPage = new HtmlWeb(); var html = webPage.Load(task.Url); var urlText = html.DocumentNode.SelectSingleNode(task.XPath)?.GetAttributeValue("href", null); if (urlText == null) { _logger.Error($"The '{task.XPath}' XPath fetches nothing for the '{task.Url}' URL."); return(null); } var url = Helper.CreateUrl(urlText); return(url.IsAbsoluteUri ? url : new Uri(task.Url, url)); }