private EnvelopeType GetOvfEnvelope(out string error)
        {
            error = string.Empty;
            string path      = m_textBoxFile.Text;
            string extension = Path.GetExtension(path).ToLower();

            if (extension == ".ovf")
            {
                EnvelopeType env = null;

                try
                {
                    env = OVF.Load(path);
                }
                catch (OutOfMemoryException ex)
                {
                    log.ErrorFormat("Failed to load OVF {0} as we ran out of memory: {1}", path, ex.Message);
                    env = null;
                }

                if (env == null)
                {
                    error = Messages.INVALID_OVF;
                    return(null);
                }

                return(env);
            }

            if (extension == ".ova")
            {
                if (!CheckSourceIsWritable(out error))
                {
                    return(null);
                }

                try
                {
                    string x   = OVF.ExtractOVFXml(path);
                    var    env = Tools.DeserializeOvfXml(x);
                    if (env == null)
                    {
                        error = Messages.IMPORT_SELECT_APPLIANCE_PAGE_ERROR_CORRUPT_OVA;
                        return(null);
                    }
                    return(env);
                }
                catch (Exception)
                {
                    error = Messages.IMPORT_SELECT_APPLIANCE_PAGE_ERROR_CORRUPT_OVA;
                    return(null);
                }
            }

            return(null);
        }
        private void webclient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)             //failure
            {
                Program.Invoke(this, () =>
                {
                    DownloadedPath        = null;
                    m_tlpProgress.Visible = false;
                    var appfile           = (ApplianceFile)e.UserState;
                    ShowDownloadError(string.Format(Messages.IMPORT_DOWNLOAD_ERROR, appfile.RemoteUri, e.Error.Message));
                });
            }
            else if (e.Cancelled)             //user cancelled
            {
                Program.Invoke(this, () =>
                {
                    DownloadedPath = null;
                    DialogResult   = DialogResult.Cancel;
                });
            }
            else             //success
            {
                Program.Invoke(this, () =>
                {
                    var appfile = (ApplianceFile)e.UserState;

                    if (m_filesToDownload.Peek() == appfile)
                    {
                        m_filesToDownload.Dequeue();
                    }

                    if (appfile.LocalPath == DownloadedPath && DownloadedPath.ToLower().EndsWith(".ovf"))
                    {
                        var envType = OVF.Load(DownloadedPath);

                        if (envType != null)
                        {
                            int index     = m_uri.OriginalString.LastIndexOf('/') + 1;
                            var remoteDir = m_uri.OriginalString.Substring(0, index);

                            foreach (var file in envType.References.File)
                            {
                                var remoteUri = new Uri(remoteDir + file.href);
                                var localPath = Path.Combine(m_textBoxWorkspace.Text, file.href);
                                m_filesToDownload.Enqueue(new ApplianceFile(remoteUri, localPath));
                            }
                        }
                    }

                    if (m_filesToDownload.Count == 0)
                    {
                        m_progressBar.Value = 100;
                        DialogResult        = DialogResult.Yes;
                    }
                    else
                    {
                        var file = m_filesToDownload.Peek();
                        m_webClient.DownloadFileAsync(file.RemoteUri, file.LocalPath, file);
                    }
                });
            }
        }
示例#3
0
        private void webclient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            Program.Invoke(this, () =>
            {
                var appfile = (ApplianceFile)e.UserState;

                if (e.Cancelled)
                {
                    longProcessInProgress = false;
                }
                else if (e.Error != null) // failure
                {
                    progressBar1.Visible  = false;
                    labelProgress.Visible = false;
                    _labelError.Text      = string.Format(Messages.IMPORT_WIZARD_FAILED_DOWNLOAD,
                                                          Path.GetFileName(appfile.LocalPath));
                    m_tlpError.Visible = true;
                    log.Error(string.Format("Failed to download file {0}.", appfile), e.Error);
                    longProcessInProgress = false;
                }
                else // success
                {
                    if (_filesToDownload.Peek() == appfile)
                    {
                        _filesToDownload.Dequeue();
                    }

                    if (appfile.LocalPath.ToLower().EndsWith(".ovf"))
                    {
                        var envType = OVF.Load(appfile.LocalPath);

                        if (envType != null)
                        {
                            int index     = _uri.OriginalString.LastIndexOf('/') + 1;
                            var remoteDir = _uri.OriginalString.Substring(0, index);

                            foreach (var file in envType.References.File)
                            {
                                var remoteUri = new Uri(remoteDir + file.href);
                                var localPath = Path.Combine(_downloadFolder, file.href);
                                _filesToDownload.Enqueue(new ApplianceFile(remoteUri, localPath));
                            }
                        }
                    }

                    if (_filesToDownload.Count == 0)
                    {
                        progressBar1.Value    = 100;
                        progressBar1.Visible  = false;
                        labelProgress.Visible = false;
                        m_textBoxFile.Text    = Path.Combine(_downloadFolder, Path.GetFileName(_uri.AbsolutePath));
                        longProcessInProgress = false;
                    }
                    else
                    {
                        progressBar1.Value = 0;
                        var file           = _filesToDownload.Peek();
                        labelProgress.Text = string.Format(Messages.IMPORT_WIZARD_DOWNLOADING,
                                                           Path.GetFileName(file.LocalPath).Ellipsise(50));
                        _webClient.DownloadFileAsync(file.RemoteUri, file.LocalPath, file);
                    }
                }
            });
        }