示例#1
0
        public void SimpleDownload()
        {
            StartServer();

            new HttpStringDownloader()
            {
                Uri      = new Uri(server.BaseUrl),
                Finished = (d) => {
                    Assert.IsTrue(d.State.Success);
                    Assert.AreEqual(server.ResourceCount.ToString() + "\n", d.Content);
                }
            }.StartSync();

            var f = new HttpFileDownloader()
            {
                Uri = new Uri(server.BaseUrl + "/1")
            };

            f.FileFinished += (d) => {
                Assert.IsTrue(d.State.Success);
                var size = new FileInfo(d.LocalPath).Length;
                Assert.IsTrue(size <= server.MaxResourceSize);
                Assert.IsTrue(size >= server.MinResourceSize);
                System.IO.File.Delete(d.LocalPath);
            };
            f.StartSync();
        }
        public JamendoDownloadManager(string remote_uri, string mimetype)
        {
            this.mimetype = mimetype;
            job           = new DownloadManagerJob(this)
            {
                Title     = AddinManager.CurrentLocalizer.GetString("Jamendo Downloads"),
                Status    = AddinManager.CurrentLocalizer.GetString("Contacting..."),
                IconNames = new string [] { "jamendo" },
                CanCancel = true
            };
            job.Finished += delegate { ServiceManager.SourceManager.MusicLibrary.NotifyUser(); };

            ServiceManager.Get <JobScheduler> ().Add(job);

            import_manager = new LibraryImportManager(true)
            {
                KeepUserJobHidden = true,
                Debug             = true,
                Threaded          = false
            };

            var downloader = new HttpFileDownloader()
            {
                Uri           = new Uri(remote_uri),
                TempPathRoot  = Path.Combine(Path.GetTempPath(), "banshee-jamendo-downloader"),
                FileExtension = mimetype == "application/zip" ? "zip" : "mp3"
            };

            job.CancelRequested += delegate { downloader.Abort(); };
            QueueDownloader(downloader);
        }
示例#3
0
        public void CheckForUpdates(bool verbose)
        {
            this.verbose = verbose;

            HttpFileDownloader downloader = new HttpFileDownloader();

            downloader.Uri          = new Uri(download_url + doap_filename);
            downloader.TempPathRoot = Path.GetTempPath();
            downloader.Finished    += OnDoapDownloaderFinished;
            downloader.Start();

            temp_doap_path = downloader.LocalPath;
        }
示例#4
0
 private bool SingleDownload(string url, string dst)
 {
     try
     {
         using (HttpFileDownloader dl = new HttpFileDownloader())
         {
             dl.Download(url, dst, null);
         }
     }
     catch
     {
         return(false);
     }
     return(true);
 }
示例#5
0
        private void PrepareDownloads(FileMapSystem.FileMapSystem newMap, string versionStr)
        {
            if (_downloader != null)
            {
                _downloader.Release();
                _downloader = null;
            }

            _downloader = new HttpFileDownloader(new Uri(GetDownloadBaseURL(versionStr)),
                                                 AssetBundlePathResolver.BundleSaveDirName);
            var currentMap = AssetBundleManager.Instance.GetFileMapSystem();
            var misses     = currentMap.GetMissFileMaps(newMap);

            _currentDownloadingGroupDesc = misses;
            CommonLog.Log(MAuthor.WY, $"{misses.Count} files miss in current file map");

            foreach (var fileMapGroupDescIter in misses)
            {
                var fileName     = fileMapGroupDescIter.Key;
                var desc         = fileMapGroupDescIter.Value;
                var fileSavePath = _savePath + fileName;

                if (File.Exists(fileSavePath))
                {
                    var bytes = FileUtils.ReadAllBytes(fileSavePath);
                    if (bytes != null && bytes.Length > 0)
                    {
                        var md5 = MD5Creater.Md5Struct(bytes);
                        if (md5.MD51 == desc.Md51 && md5.MD52 == desc.Md52)
                        {
                            CommonLog.Log(MAuthor.WY, $"file {fileSavePath} already exist, skip");
                            continue;
                        }
                    }
                }

                _downloader.AddDownLoad(new WWWFileDownloader.DownloadFileInfo
                {
                    FileName           = fileName,
                    FileSize           = desc.Len,
                    MapedFileName_MD51 = desc.Md51,
                    MapedFileName_MD52 = desc.Md52,
                });
            }
        }
示例#6
0
文件: Tests.cs 项目: Yetangitu/f-spot
        public void SimpleDownload ()
        {
            StartServer ();

            new HttpStringDownloader () {
                Uri = new Uri (server.BaseUrl),
                Finished = (d) => {
                    Assert.IsTrue (d.State.Success);
                    Assert.AreEqual (server.ResourceCount.ToString () + "\n", d.Content);
                }
            }.StartSync ();

            var f = new HttpFileDownloader () { Uri = new Uri (server.BaseUrl + "/1") };
            f.FileFinished += (d) => {
                Assert.IsTrue (d.State.Success);
                var size = new System.IO.FileInfo (d.LocalPath).Length;
                Assert.IsTrue (size <= server.MaxResourceSize);
                Assert.IsTrue (size >= server.MinResourceSize);
                System.IO.File.Delete (d.LocalPath);
            };
            f.StartSync ();
        }
示例#7
0
        public void DisplayUpdateAvailableDialog()
        {
            bool update;

            using (var message_dialog = new MessageDialog(ServiceManager.Get <GtkElementsService> ().PrimaryWindow, 0,
                                                          MessageType.Question, ButtonsType.YesNo, String.Format(
                                                              Catalog.GetString("A new version of Banshee ({0}) is available.{1}Do you want to update?"), unstable_version, Environment.NewLine))) {
                message_dialog.WindowPosition = WindowPosition.CenterOnParent;
                message_dialog.Title          = Catalog.GetString("Banshee update available");
                update = (message_dialog.Run() == (int)ResponseType.Yes);
                message_dialog.Destroy();
            }

            if (update)
            {
                string downloadUrl = String.Format("{0}/Banshee-{1}.msi", installer_url, unstable_version);

                var downloader = new HttpFileDownloader()
                {
                    Uri           = new Uri(downloadUrl),
                    TempPathRoot  = Path.GetTempPath(),
                    FileExtension = "msi"
                };
                downloader.Progress += OnInstallerDownloaderProgress;
                downloader.Finished += OnInstallerDownloaderFinished;
                downloader.Start();

                temp_installer_path = downloader.LocalPath;

                job = new DownloadManagerJob(this)
                {
                    // Translators: {0} is the filename, eg Banshee-1.9.5.msi
                    Title     = String.Format(Catalog.GetString("Downloading {0}"), String.Format("Banshee-{0}.msi", unstable_version)),
                    CanCancel = false
                };

                ServiceManager.Get <JobScheduler> ().Add(job);
            }
        }
示例#8
0
 private void OnApplicationQuit()
 {
     _downloader?.Release();
     _downloader = null;
 }
示例#9
0
        public static List <LogInfo> WebGet(EngineState s, CodeCommand cmd)
        { // WebGet,<URL>,<DestPath>[<HashType>=<HashDigest>][,TimeOut=<Int>][,Referer=<URL>][,UserAgent=<Agent>][,NOERR]
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_WebGet info = cmd.Info.Cast <CodeInfo_WebGet>();

            string url      = StringEscaper.Preprocess(s, info.URL);
            string destPath = StringEscaper.Preprocess(s, info.DestPath);
            int    timeOut  = 10;

            if (info.TimeOut != null)
            {
                string timeOutStr = StringEscaper.Preprocess(s, info.TimeOut);
                if (!NumberHelper.ParseInt32(timeOutStr, out timeOut))
                {
                    return(LogInfo.LogErrorMessage(logs, $"TimeOut [{timeOutStr}] is not a valid positive integer"));
                }
                if (timeOut <= 0)
                {
                    return(LogInfo.LogErrorMessage(logs, $"TimeOut [{timeOutStr}] is not a valid positive integer"));
                }
            }

            string refererUrl = null;

            if (info.Referer != null)
            {
                refererUrl = StringEscaper.Preprocess(s, info.Referer);
            }

            // Check PathSecurity in destPath
            if (!StringEscaper.PathSecurityCheck(destPath, out string pathErrorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, pathErrorMsg));
            }

            Uri    uri = new Uri(url);
            string destFile;

            if (Directory.Exists(destPath))
            {
                destFile = Path.Combine(destPath, Path.GetFileName(uri.LocalPath));
            }
            else // downloadTo is file
            {
                if (File.Exists(destPath))
                {
                    if (cmd.Type == CodeType.WebGetIfNotExist)
                    {
                        logs.Add(new LogInfo(LogState.Ignore, $"File [{destPath}] already exists"));
                        return(logs);
                    }

                    logs.Add(new LogInfo(LogState.Overwrite, $"File [{destPath}] will be overwritten"));
                }
                else
                {
                    Directory.CreateDirectory(FileHelper.GetDirNameEx(destPath));
                }
                destFile = destPath;
            }

            string destFileExt = Path.GetExtension(destFile);

            s.MainViewModel.SetBuildCommandProgress("WebGet Progress");
            try
            {
                // Set User-Agent to use
                // (1) Use Command's custom User-Agent
                // (2) Use EngineState's custom User-Agent
                // (3) Use PEBakery's default User-Agent
                string userAgent = null;
                if (info.UserAgent != null)
                {
                    userAgent = info.UserAgent;
                }
                else
                {
                    userAgent = s.CustomUserAgent;
                }

                if (info.HashType == HashHelper.HashType.None)
                { // Standard WebGet
                    string tempPath = FileHelper.GetTempFile(destFileExt);

                    HttpFileDownloader        downloader = new HttpFileDownloader(s.MainViewModel, timeOut, userAgent, refererUrl);
                    HttpFileDownloader.Report report;
                    try
                    {
                        CancellationTokenSource ct = new CancellationTokenSource();
                        s.CancelWebGet = ct;

                        Task <HttpFileDownloader.Report> task = downloader.Download(url, tempPath, ct.Token);
                        task.Wait(ct.Token);

                        report = task.Result;
                    }
                    catch (Exception e)
                    {
                        report = new HttpFileDownloader.Report(false, 0, Logger.LogExceptionMessage(e));
                    }
                    finally
                    {
                        s.CancelWebGet = null;
                    }

                    int statusCode = report.StatusCode;
                    if (report.Result)
                    {
                        FileHelper.FileReplaceEx(tempPath, destFile);
                        logs.Add(new LogInfo(LogState.Success, $"[{destFile}] downloaded from [{url}]"));
                    }
                    else
                    {
                        LogState state = info.NoErrFlag ? LogState.Warning : LogState.Error;
                        logs.Add(new LogInfo(state, $"Error occured while downloading [{url}]"));
                        logs.Add(new LogInfo(LogState.Info, report.ErrorMsg));
                        if (statusCode == 0)
                        {
                            logs.Add(new LogInfo(LogState.Info, "Request failed, no response received."));
                        }
                        else
                        {
                            logs.Add(new LogInfo(LogState.Info, $"Response returned HTTP status code [{statusCode}]"));
                        }
                    }

                    // PEBakery extension -> Report exit code via #r
                    if (!s.CompatDisableExtendedSectionParams)
                    {
                        s.ReturnValue = statusCode.ToString();
                        if (statusCode < 100)
                        {
                            logs.Add(new LogInfo(LogState.Success, $"Returned [{statusCode}] into [#r]"));
                        }
                        else
                        {
                            logs.Add(new LogInfo(LogState.Success, $"Returned HTTP status code [{statusCode}] to [#r]"));
                        }
                    }
                }
                else
                { // Validate downloaded file with hash
                    Debug.Assert(info.HashDigest != null);

                    string tempPath = FileHelper.GetTempFile(destFileExt);

                    HttpFileDownloader        downloader = new HttpFileDownloader(s.MainViewModel, timeOut, userAgent, refererUrl);
                    HttpFileDownloader.Report report;
                    try
                    {
                        CancellationTokenSource ct = new CancellationTokenSource();
                        s.CancelWebGet = ct;

                        Task <HttpFileDownloader.Report> task = downloader.Download(url, tempPath, ct.Token);
                        task.Wait(ct.Token);

                        report = task.Result;
                    }
                    catch (Exception e)
                    {
                        report = new HttpFileDownloader.Report(false, 0, Logger.LogExceptionMessage(e));
                    }
                    finally
                    {
                        s.CancelWebGet = null;
                    }

                    int statusCode = report.StatusCode;
                    if (report.Result)
                    { // Success -> Check hash
                        string hashDigest = StringEscaper.Preprocess(s, info.HashDigest);
                        if (hashDigest.Length != 2 * HashHelper.GetHashByteLen(info.HashType))
                        {
                            return(LogInfo.LogErrorMessage(logs, $"Hash digest [{hashDigest}] is not [{info.HashType}]"));
                        }

                        string downDigest;
                        using (FileStream fs = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
                        {
                            byte[] digest = HashHelper.GetHash(info.HashType, fs);
                            downDigest = StringHelper.ToHexStr(digest);
                        }

                        if (hashDigest.Equals(downDigest, StringComparison.OrdinalIgnoreCase)) // Success
                        {
                            FileHelper.FileReplaceEx(tempPath, destFile);
                            logs.Add(new LogInfo(LogState.Success, $"[{destFile}] downloaded from [{url}] and verified "));
                        }
                        else
                        {
                            statusCode = 1; // 1 means hash mismatch
                            logs.Add(new LogInfo(LogState.Error, $"Downloaded file from [{url}] was corrupted"));
                        }
                    }
                    else
                    { // Failure -> Log error message
                        LogState state = info.NoErrFlag ? LogState.Warning : LogState.Error;
                        logs.Add(new LogInfo(state, $"Error occured while downloading [{url}]"));
                        logs.Add(new LogInfo(LogState.Info, report.ErrorMsg));
                        if (statusCode == 0)
                        {
                            logs.Add(new LogInfo(LogState.Info, "Request failed, no response received."));
                        }
                        else
                        {
                            logs.Add(new LogInfo(LogState.Info, $"Response returned HTTP Status Code [{statusCode}]"));
                        }
                    }

                    // PEBakery extension -> Report exit code via #r
                    if (!s.CompatDisableExtendedSectionParams)
                    {
                        s.ReturnValue = statusCode.ToString();
                        if (statusCode < 100)
                        {
                            logs.Add(new LogInfo(LogState.Success, $"Returned [{statusCode}] into [#r]"));
                        }
                        else
                        {
                            logs.Add(new LogInfo(LogState.Success, $"Returned HTTP status code [{statusCode}] to [#r]"));
                        }
                    }
                }
            }
            finally
            {
                s.MainViewModel.ResetBuildCommandProgress();
            }

            return(logs);
        }
示例#10
0
 public DownloaderCache() : base(new RuneTek5CacheFileDecoder())
 {
     this._tcpFileDownloader  = new TcpFileDownloader();
     this._httpFileDownloader = new HttpFileDownloader();
 }