private async Task<string> DownloadLatestPatcher() { Label = DownloadingLabel; try { if (CancelEvent.WaitOne(0)) return null; Downloader downloader = new Downloader(CancelEvent); downloader.DownloadProgress += OnDownloadProgress; HttpFileInfo latest = await GetLatestPatcherUrl(downloader); if (latest == null) throw new Exception("Не удалось найти свежую версию программы установки. Проверьте файл конфигурации."); Maximum = latest.ContentLength; string filePath = Path.GetTempFileName(); await downloader.Download(latest.Url, filePath); return filePath; } finally { Label = InstallLabel; } }
protected override async Task DoAction() { Label = DownloadingLabel; try { if (CancelEvent.WaitOne(0)) { return; } Downloader downloader = new Downloader(CancelEvent); downloader.DownloadProgress += OnDownloadProgress; HttpFileInfo fileInfo = _latestTranslationInfo.Value; Maximum = fileInfo.ContentLength; await downloader.Download(fileInfo.Url, PatcherService.ArchiveFileName); if (fileInfo.LastModified != null) { File.SetLastWriteTime(PatcherService.ArchiveFileName, fileInfo.LastModified.Value); } SubLabel = null; } finally { Label = DownloadLabel; } }
private HttpFileInfo GetLatestTranslationInfo() { Downloader downloader = new Downloader(CancelEvent); LocalizatorEnvironmentInfo info = InteractionService.LocalizatorEnvironment.Provide(); HttpFileInfo latest = null; foreach (string url in info.TranslationUrls) { try { HttpFileInfo fileInfo = downloader.GetRemoteFileInfo(url).ContinueWith(t => t.Result).Result; if (latest == null || latest.LastModified < fileInfo.LastModified) { latest = fileInfo; } } catch (Exception ex) { Log.Warning(ex, "Не удалось получить информацию о файле: [{0}]", url); } } return(latest); }
private void GetLatest() { try { HttpFileInfo value = _latestTranslationInfo.Value; Dispatcher.Invoke(() => { if (value == null) { IsEnabled = false; SubLabel = "Сервер недоступен"; return; } string server = value.Url; int index = server.IndexOf("//"); if (index > -1) { server = server.Substring(index + 2); } index = server.IndexOf('/'); if (index > -1) { server = server.Substring(0, index); } StringBuilder sb = new StringBuilder(128); sb.Append(server); sb.Append(" ("); sb.Append(UiHelper.FormatBytes(value.ContentLength)); if (value.LastModified != null) { sb.Append(", "); sb.Append(value.LastModified); } sb.Append(')'); SubLabel = sb.ToString(); if (!File.Exists(PatcherService.ArchiveFileName) || File.GetLastWriteTime(PatcherService.ArchiveFileName) < value.LastModified) { SubLabelColor = Brushes.LightGreen; } }); } catch (Exception ex) { Log.Error(ex); } }
public async Task<HttpFileInfo> GetRemoteFileInfo(string url) { HttpFileInfo result = new HttpFileInfo(); if (_cancelEvent.WaitOne(0)) return result; WebRequest request = WebRequest.Create(url); request.Method = "HEAD"; using (WebResponse resp = await request.GetResponseAsync()) { if (_cancelEvent.WaitOne(0)) return result; result.ReadFromResponse(url, resp); return result; } }
private async Task<HttpFileInfo> GetLatestPatcherUrl(Downloader downloader) { LocalizatorEnvironmentInfo info = InteractionService.LocalizatorEnvironment.Provide(); HttpFileInfo latest = null; foreach (string url in info.PatherUrls) { try { HttpFileInfo fileInfo = await downloader.GetRemoteFileInfo(url); if (latest == null || latest.LastModified < fileInfo.LastModified) latest = fileInfo; } catch (Exception ex) { Log.Warning(ex, "Не удалось получить информацию о файле: [{0}]", url); } } return latest; }
public async Task <HttpFileInfo> GetRemoteFileInfo(string url) { HttpFileInfo result = new HttpFileInfo(); if (_cancelEvent.WaitOne(0)) { return(result); } WebRequest request = WebRequest.Create(url); request.Method = "HEAD"; using (WebResponse resp = await request.GetResponseAsync()) { if (_cancelEvent.WaitOne(0)) { return(result); } result.ReadFromResponse(url, resp); return(result); } }