public static WorkingBeatmap GetBeatmap(int beatmapId, bool verbose = false, bool forceDownload = true, IReporter reporter = null)
        {
            string fileLocation = Path.Combine(AppSettings.BEATMAPS_PATH, beatmapId.ToString()) + ".osu";

            if ((forceDownload || !File.Exists(fileLocation)) && AppSettings.ALLOW_DOWNLOAD)
            {
                Stream stream;
                if (verbose)
                {
                    reporter?.Verbose($"Downloading {beatmapId}.");
                }
                stream = GetBeatmapByBid(beatmapId);
                if (stream == null)
                {
                    var req = new WebRequest(string.Format(AppSettings.DOWNLOAD_PATH, beatmapId))
                    {
                        AllowInsecureRequests = true,
                    };

                    req.Failed += _ =>
                    {
                        if (verbose)
                        {
                            reporter?.Error($"Failed to download {beatmapId}.");
                        }
                    };

                    req.Finished += () =>
                    {
                        if (verbose)
                        {
                            reporter?.Verbose($"{beatmapId} successfully downloaded.");
                        }
                    };

                    req.Perform();
                    RedisHelper.HSet("beatmap", beatmapId.ToString(), req.GetResponseData());
                    stream = req.ResponseStream;
                }
                if (AppSettings.SAVE_DOWNLOADED)
                {
                    using (var fileStream = File.Create(fileLocation))
                    {
                        stream.CopyTo(fileStream);
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                }

                return(new LoaderWorkingBeatmap(stream));
            }

            return(!File.Exists(fileLocation) ? null : new LoaderWorkingBeatmap(fileLocation));
        }
示例#2
0
 public static void AuthenticatedBlockingPerform(this WebRequest r)
 {
     r.AddHeader("Authorization", $"token {GitHubAccessToken}");
     r.Perform();
 }