Example #1
0
        internal string DownloadFile(IProgressMonitor monitor, string url)
        {
            if (url.StartsWith("file://", StringComparison.Ordinal))
            {
                string tmpfile = Path.GetTempFileName();
                string path    = new Uri(url).LocalPath;
                File.Delete(tmpfile);
                File.Copy(path, tmpfile);
                return(tmpfile);
            }

            string     file = null;
            FileStream fs   = null;
            Stream     s    = null;

            try {
                monitor.BeginTask("Requesting " + url, 2);
                var task = DownloadFileRequest.DownloadFile(url, noCache: true);
                task.Wait();

                using (var request = task.Result) {
                    monitor.Step(1);
                    monitor.BeginTask("Downloading " + url, (int)request.ContentLength);

                    file = Path.GetTempFileName();
                    fs   = new FileStream(file, FileMode.Create, FileAccess.Write);
                    s    = request.Stream;
                    byte[] buffer = new byte [4096];

                    int n;
                    while ((n = s.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        monitor.Step(n);
                        fs.Write(buffer, 0, n);
                        if (monitor.IsCancelRequested)
                        {
                            throw new InstallException("Installation cancelled.");
                        }
                    }
                    fs.Close();
                    s.Close();
                    return(file);
                }
            } catch {
                if (fs != null)
                {
                    fs.Close();
                }
                if (s != null)
                {
                    s.Close();
                }
                if (file != null)
                {
                    File.Delete(file);
                }
                throw;
            } finally {
                monitor.EndTask();
            }
        }
Example #2
0
        public IAsyncResult BeginDownloadSupportFile(string name, AsyncCallback cb, object state)
        {
            FileAsyncResult res = new FileAsyncResult();

            res.AsyncState = state;
            res.Callback   = cb;

            string cachedFile = Path.Combine(CachedFilesDir, Path.GetFileName(name));

            if (File.Exists(cachedFile))
            {
                res.FilePath = cachedFile;
                res.CompletedSynchronously = true;
                res.SetDone();
                return(res);
            }

            Uri u = new Uri(new Uri(Url), name);

            if (u.Scheme == "file")
            {
                res.FilePath = u.AbsolutePath;
                res.CompletedSynchronously = true;
                res.SetDone();
                return(res);
            }

            res.FilePath = cachedFile;
            var request = DownloadFileRequest.DownloadFile(u.ToString(), false).ContinueWith(t => {
                try {
                    using (var resp = t.Result) {
                        string dir = Path.GetDirectoryName(res.FilePath);
                        lock (this) {
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }
                        }
                        if (File.Exists(res.FilePath))
                        {
                            res.SetDone();
                            return;
                        }
                        byte [] buffer = new byte [8092];
                        using (var s = resp.Stream) {
                            using (var f = File.OpenWrite(res.FilePath)) {
                                int nr = 0;
                                while ((nr = s.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    f.Write(buffer, 0, nr);
                                }
                            }
                        }
                        res.SetDone();
                    }
                } catch (Exception ex) {
                    res.Error = ex;
                }
            });

            return(res);
        }