Пример #1
0
        public static UpdateWindow.ErrorState DownloadFile(string url, Uri serverAddress,
                                                           out Stream str, DownloadProgressChangedEventHandler wc_DownloadProgressChanged)
        {
            ensureSensibleCacheFolderExists();
            UpdateWindow.ErrorState er = UpdateWindow.ErrorState.Successful;
            string updateCache         = MeGUISettings.MeGUIUpdateCache;

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

            if (File.Exists(localFilename) && (finfo.Length == 0))
            {
                try
                {
                    finfo.Delete();
                }
                catch (IOException) { }
            }
            else if (File.Exists(localFilename))
            {
                goto gotLocalFile;
            }

            WebClient        wc  = new WebClient();
            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
            {
                File.SetLastWriteTime(localFilename, DateTime.Now);
            }
            catch (IOException) { }

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

            return(er);
        }
Пример #2
0
        /// <summary>
        /// Checks the local update cache file
        /// </summary>
        /// <param name="file">full file name</param>
        /// <param name="err">reference to the error result</param>
        /// <returns>true if file is ok, false if not</returns>
        public static bool VerifyLocalCacheFile(string file, ref UpdateWindow.ErrorState err)
        {
            if (err == UpdateWindow.ErrorState.Successful)
            {
                err = UpdateWindow.ErrorState.CouldNotDownloadFile;
            }

            if (!File.Exists(file))
            {
                if (!File.Exists(Path.Combine(Path.GetDirectoryName(file), "_obsolete_" + Path.GetFileName(file))))
                {
                    return(false);
                }
                File.Move(Path.Combine(Path.GetDirectoryName(file), "_obsolete_" + Path.GetFileName(file)), file);
            }

            FileInfo finfo = new FileInfo(file);

            if (finfo.Length == 0)
            {
                DeleteCacheFile(file);
                return(false);
            }

            if (file.ToLowerInvariant().EndsWith(".7z"))
            {
                // check the 7-zip file
                err = UpdateWindow.ErrorState.CouldNotExtract;
                bool bOK = true;
                try
                {
                    using (SevenZipExtractor oArchive = new SevenZipExtractor(file))
                    {
                        if (oArchive.Check() == false)
                        {
                            bOK = false;
                        }
                    }
                }
                catch { bOK = false; }
                if (!bOK)
                {
                    MainForm.Instance.UpdateHandler.AddTextToLog("Could not extract " + file + ". Deleting file.", ImageType.Error, true);
                    DeleteCacheFile(file);
                    return(false);
                }
            }
            else if (file.ToLowerInvariant().EndsWith(".zip"))
            {
                // check the zip file
                err = UpdateWindow.ErrorState.CouldNotExtract;
                bool bOK = true;
                try
                {
                    using (ZipFile zipFile = new ZipFile(file))
                    {
                        if (zipFile.TestArchive(true) == false)
                        {
                            bOK = false;
                        }
                    }
                }
                catch { bOK = false; }
                if (!bOK)
                {
                    MainForm.Instance.UpdateHandler.AddTextToLog("Could not extract " + file + ". Deleting file.", ImageType.Error, true);
                    DeleteCacheFile(file);
                    return(false);
                }
            }
            else if (file.ToLowerInvariant().EndsWith(".xml"))
            {
                // check the xml file
                err = UpdateWindow.ErrorState.InvalidXML;
                System.Xml.XmlDocument upgradeXml = new System.Xml.XmlDocument();
                try
                {
                    upgradeXml.Load(file);
                }
                catch
                {
                    DeleteCacheFile(file);
                    return(false);
                }
            }

            err = UpdateWindow.ErrorState.Successful;
            return(true);
        }
Пример #3
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);
        }