Ejemplo n.º 1
0
        public static UpdateWindow.ErrorState DownloadFile(string url, Uri serverAddress,
                                                           out Stream str, DownloadProgressChangedEventHandler wc_DownloadProgressChanged, UpdateWindow oUpdate)
        {
            ensureSensibleCacheFolderExists();
            UpdateWindow.ErrorState er = UpdateWindow.ErrorState.Successful;
            string updateCache         = MainForm.Instance.Settings.MeGUIUpdateCache;

            string localFilename = Path.Combine(updateCache, url);

            if (File.Exists(localFilename))
            {
                FileInfo finfo = new FileInfo(localFilename);
                if (finfo.Length == 0)
                {
                    oUpdate.AddTextToLog(localFilename + " is empty. Deleting file.", ImageType.Information);
                    UpdateCacher.FlushFile(localFilename, oUpdate);
                }

                // check the zip file
                if (localFilename.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".zip"))
                {
                    try
                    {
                        ZipFile zipFile = new ZipFile(localFilename);
                        if (zipFile.TestArchive(true) == false)
                        {
                            oUpdate.AddTextToLog("Could not unzip " + localFilename + ". Deleting file.", ImageType.Information);
                            UpdateCacher.FlushFile(localFilename, oUpdate);
                        }
                        else
                        {
                            goto gotLocalFile;
                        }
                    }
                    catch
                    {
                        oUpdate.AddTextToLog("Could not unzip " + localFilename + ". Deleting file.", ImageType.Error);
                        UpdateCacher.FlushFile(localFilename, oUpdate);
                    }
                }
                else if (localFilename.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".7z")) // check the 7-zip file
                {
                    try
                    {
                        SevenZipExtractor oArchive = new SevenZipExtractor(localFilename);
                        if (oArchive.Check() == false)
                        {
                            oUpdate.AddTextToLog("Could not extract " + localFilename + ". Deleting file.", ImageType.Information);
                            UpdateCacher.FlushFile(localFilename, oUpdate);
                        }
                        else
                        {
                            goto gotLocalFile;
                        }
                    }
                    catch
                    {
                        oUpdate.AddTextToLog("Could not extract " + localFilename + ". Deleting file.", ImageType.Error);
                        UpdateCacher.FlushFile(localFilename, oUpdate);
                    }
                }
                else
                {
                    goto gotLocalFile;
                }
            }

            WebClient wc = new WebClient();

            // check for proxy authentication...
            if (MainForm.Instance.Settings.UseHttpProxy == true)
            {
                WebProxy     wprox = null;
                ICredentials icred = null;

                if (MainForm.Instance.Settings.HttpProxyUid != null)
                {
                    icred = new NetworkCredential(MainForm.Instance.Settings.HttpProxyUid, MainForm.Instance.Settings.HttpProxyPwd);
                }

                wprox = new WebProxy(MainForm.Instance.Settings.HttpProxyAddress + ":" + MainForm.Instance.Settings.HttpProxyPort, true, null, icred);

                WebRequest.DefaultWebProxy = wprox;
                wc.Proxy = wprox;
            }
            else
            {
                wc.Proxy = null;
            }

            ManualResetEvent mre = new ManualResetEvent(false);

            wc.DownloadFileCompleted += delegate(object sender, AsyncCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    er = UpdateWindow.ErrorState.CouldNotDownloadFile;
                }

                mre.Set();
            };

            wc.DownloadProgressChanged += wc_DownloadProgressChanged;

            wc.DownloadFileAsync(new Uri(serverAddress, url), localFilename);
            mre.WaitOne();

gotLocalFile:
            try
            {
                str = File.OpenRead(localFilename);
            }
            catch (IOException)
            {
                str = null;
                return(UpdateWindow.ErrorState.CouldNotDownloadFile);
            }

            return(er);
        }