private void AbortDownload()
 {
     if (_wfr != null)
     {
         _wfr.Dispose();
         _wfr = null;
     }
 }
        protected override bool ProcessInternal()
        {
            try
            {
                DateTime fileTimeStamp = DateTime.MinValue;
                string   filePath      = string.Empty;
                string   fileName      = "~" + _descName + ".frtmp";
                _pendingDownloadFile = Path.Combine(_repositoryPath, fileName);

                using (WebFileRetriever downloader =
                           new WebFileRetriever(_ns, _downloadUrl, _pendingDownloadFile, false))
                {
                }

                string ext = PathUtils.GetExtension(_downloadUrl);

                if (!AreFilesIdentical(GetLatestFileInRepository(), _pendingDownloadFile))
                {
                    string newFileName = StringUtils.GetUniqueFileName();
                    string newFilePath = Path.Combine(_repositoryPath, newFileName + ext);
                    File.Move(_pendingDownloadFile, newFilePath);

                    if (NewFileRetrieved != null)
                    {
                        NewFileRetrieved(newFilePath, true, string.Empty);
                    }
                }

                DeleteFile(_pendingDownloadFile);
                _pendingDownloadFile = string.Empty;
            }
            catch (ThreadAbortException)
            {
                if (!string.IsNullOrEmpty(_pendingDownloadFile) && File.Exists(_pendingDownloadFile))
                {
                    EventDispatch.DispatchEvent(EventNames.ShowMessageBox, "Download cancelled.", _descName, MessageBoxIcon.Warning);

                    _retriever.Dispose();
                    _retriever = null;

                    DeleteFile(_pendingDownloadFile);
                    _pendingDownloadFile = string.Empty;
                }

                return(false);
            }
            catch (Exception ex)
            {
                ErrorDispatcher.DispatchError(ex, false);
            }

            // DownloadInterval is in seconds
            Thread.Sleep(_downloadInterval * 1000);

            return(true);
        }
        private void StartDownload()
        {
            string file     = $"{Constants.SuiteName} {_version}.exe";
            string fileUri  = AppConfig.DownloadUriBase + "/" + file;
            string tempFile = Path.Combine(Path.GetTempPath(), file);

            Logger.LogInfo("Downloading update file from {0} ...", fileUri);

            _wfr = new WebFileRetriever(AppConfig.ProxySettings, fileUri, tempFile, true);
            _wfr.FileRetrieveComplete += new FileRetrieveCompleteEventHandler(OnDownloadComplete);
        }
Exemple #4
0
        void OnBackgroundDetect(object sender, DoWorkEventArgs e)
        {
            string versionFile     = "/Versions_build.txt";
            string versionFileUri  = AppConfig.DownloadUriBase + versionFile;
            string tempVersionFile = Path.GetTempFileName();
            bool   detectOnDemand  = (bool)e.Argument;

            WebFileRetriever retriever = null;

            try
            {
                retriever = new WebFileRetriever(AppConfig.ProxySettings, versionFileUri, tempVersionFile, false);
                StringBuilder sb = new StringBuilder();

                if (Kernel32.GetPrivateProfileString(Constants.SuiteName, "Version", "1.0.0.0", sb, 255, tempVersionFile) > 0)
                {
                    Version current   = new Version(SuiteVersion.Version);
                    Version available = new Version(sb.ToString());

                    if (available.CompareTo(current) > 0)
                    {
                        Logger.LogInfo("Current version: {0}, available on server: {1}. Update is required.",
                                       current, available);

                        EventDispatch.DispatchEvent(EventNames.NewVersionAvailable, sb.ToString());
                    }
                    else
                    {
                        Logger.LogInfo("Current version: {0}, available on server: {1}. Update is NOT required.",
                                       current, available);

                        if (detectOnDemand)
                        {
                            EventDispatch.DispatchEvent(EventNames.ShowMessageBox, "TXT_NOUPDATEREQUIRED", "TXT_APP_NAME", MessageBoxIcon.Information);
                        }
                    }
                }
            }
            finally
            {
                if (retriever != null)
                {
                    retriever.Dispose();
                }
            }
        }
        protected override string DoDownloadSubtitle(string fileName, SubtitleInfo subtitle)
        {
            string destPath     = Path.ChangeExtension(fileName, subtitle.SubFormat);
            string downloadPath = Path.ChangeExtension(fileName, "gz");

            bool downloaded = false;

            using (WebFileRetriever wfr = new WebFileRetriever(AppConfig.ProxySettings,
                                                               subtitle.SubDownloadLink, downloadPath, false))
            {
                downloaded = true;
            }

            if (downloaded)
            {
                using (FileStream compressedSubtitle = new FileStream(downloadPath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                {
                    using (GZipStream str = new GZipStream(compressedSubtitle, CompressionMode.Decompress, false))
                    {
                        using (FileStream outputSubtitle = new FileStream(destPath, FileMode.Create, FileAccess.Write, FileShare.Read))
                        {
                            byte[] buffer = new byte[65536];
                            int    read   = 0;
                            do
                            {
                                read = str.Read(buffer, 0, buffer.Length);
                                if (read > 0)
                                {
                                    outputSubtitle.Write(buffer, 0, read);
                                }
                            }while (read > 0);
                        }
                    }
                }

                File.Delete(downloadPath);

                return(destPath);
            }

            return(string.Empty);
        }