public void DownloadFile(string url, string fileName)
        {
            try
            {
                var fileInfo = new FileInfo(fileName);
                if (fileInfo.Directory != null && !fileInfo.Directory.Exists)
                {
                    fileInfo.Directory.Create();
                }

                _logger.Debug("Downloading [{0}] to [{1}]", url, fileName);

                var stopWatch = Stopwatch.StartNew();
                var uri       = new HttpUri(url);

                using (var webClient = new GZipWebClient())
                {
                    webClient.Headers.Add(HttpRequestHeader.UserAgent, _userAgentBuilder.GetUserAgent());
                    webClient.Proxy = GetProxy(uri);
                    webClient.DownloadFile(uri.FullUri, fileName);
                    stopWatch.Stop();
                    _logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds);
                }
            }
            catch (WebException e)
            {
                _logger.Warn("Failed to get response from: {0} {1}", url, e.Message);
                throw;
            }
            catch (Exception e)
            {
                _logger.Warn(e, "Failed to get response from: " + url);
                throw;
            }
        }
        public static bool downloadToFile(string url, string filepath, WebProxy wp = null)
        {
            filepath += ".tmp";
            if (File.Exists(filepath) && !removeFile(filepath))
            {
                return false;
            }

            Uri downloadurl = new Uri(url);
            using (var client = new GZipWebClient())
            {
                if (wp != null)
                {
                    client.Proxy = wp;
                }

                // Get the username and password if it has been set
                if (!string.IsNullOrEmpty(downloadurl.UserInfo)) {
                    var credInfo = downloadurl.UserInfo.Split(':');
                    if (credInfo.Length == 2)
                    {
                        client.Credentials = new System.Net.NetworkCredential(credInfo[0], credInfo[1]);
                    }

                    // Strip userinfo
                    downloadurl = new Uri(downloadurl.GetComponents(UriComponents.AbsoluteUri & ~UriComponents.UserInfo, UriFormat.UriEscaped));
                }

                try
                {
                    Log.Trace("Downloading {0} to {1}", downloadurl.ToString(), filepath);
                    client.DownloadFile(downloadurl, filepath);
                    Log.Debug("Downloaded {0} to {1}", downloadurl.ToString(), filepath);
                    return true;
                }
                 catch (WebException e)
                {
                    Log.Error("File '" + filepath + "' could not be downloaded from server.", e);
                    return false;
                }
            }
        }
Example #3
0
        public void Update(Action <UpdateResponse> callback, UpdateInfo info = null)
        {
            var leagueMd5 = Utility.Md5Checksum(Config.Instance.LeagueOfLegendsExePath);
            var coreMd5   = Utility.Md5Checksum(Directories.CoreFilePath);

            Task.Factory.StartNew(() =>
            {
                try
                {
                    using (var client = new GZipWebClient {
                        Timeout = 4000
                    })
                    {
                        if (info == null)
                        {
                            info = JsonConvert.DeserializeObject <UpdateInfo>(client.DownloadString(Url + leagueMd5));
                        }

                        if (info.Version != coreMd5 && WhiteList.Any(s => info.Url.StartsWith(s)))
                        {
                            Log.InfoFormat("Update Core from {0} to {1}", coreMd5, info.Version);
                            Messenger.Default.Send(new BalloonTipCoreUpdateMessage());
                            // TODO: implement balloontip service

                            try
                            {
                                client.DownloadFile(info.Url, UpdateZip);

                                using (var archive = ZipFile.OpenRead(UpdateZip))
                                {
                                    foreach (var entry in archive.Entries)
                                    {
                                        var file = Path.Combine(Directories.CoreDirectory, entry.FullName);
                                        Log.Info("Update " + file);
                                        entry.ExtractToFile(file, true);
                                    }
                                }

                                callback(new UpdateResponse {
                                    State = UpdateState.Updated
                                });
                            }
                            catch (Exception e)
                            {
                                Log.Warn("Core Download failed", e);
                                callback(new UpdateResponse {
                                    State = UpdateState.DownloadError
                                });
                            }
                            finally
                            {
                                if (File.Exists(UpdateZip))
                                {
                                    File.Delete(UpdateZip);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Warn(e);
                    callback(new UpdateResponse {
                        State = UpdateState.UpdateError
                    });
                }
            });
        }
Example #4
0
        public void Update(Action <UpdateResponse> callback, UpdateInfo info = null)
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
                    using (var client = new GZipWebClient {
                        Timeout = 4000
                    })
                    {
                        if (info == null)
                        {
                            info = JsonConvert.DeserializeObject <UpdateInfo>(client.DownloadString(Url));
                        }

                        var loaderVersion = System.Reflection.Assembly.GetEntryAssembly().GetName().Version;
                        var updateVersion = Version.Parse(info.Version);

                        if (updateVersion > loaderVersion && WhiteList.Any(s => info.Url.StartsWith(s)))
                        {
                            Log.InfoFormat("Update Loader from {0} to {1}", loaderVersion, updateVersion);
                            Messenger.Default.Send(new BalloonTipLoaderUpdateMessage());
                            // TODO: implement balloontip service

                            try
                            {
                                client.DownloadFile(info.Url, UpdateExe);

                                using (var archive = ZipFile.OpenRead(UpdateExe))
                                {
                                    foreach (var entry in archive.Entries)
                                    {
                                        var file = Path.Combine(Directories.CoreDirectory, entry.FullName);
                                        Log.Info("Update " + file);
                                        entry.ExtractToFile(file, true);
                                    }
                                }

                                callback(new UpdateResponse {
                                    State = UpdateState.Updated
                                });
                            }
                            catch (Exception e)
                            {
                                Log.Warn("Loader Download failed", e);
                                callback(new UpdateResponse {
                                    State = UpdateState.DownloadError
                                });
                            }
                            finally
                            {
                                if (File.Exists(UpdateExe))
                                {
                                    File.Delete(UpdateExe);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Warn(e);
                    callback(new UpdateResponse {
                        State = UpdateState.UpdateError
                    });
                }
            });
        }