WriteAsync() public abstract method

public abstract WriteAsync ( string data ) : Task
data string
return Task
Exemplo n.º 1
0
        /// <summary>
        /// Find and returns item icon
        /// </summary>
        /// <param name="context"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public async Task Resolve(Server.RequestContext context, string path)
        {
            var tag     = path.Split("/").Last();
            var key     = "img" + tag;
            var preview = await CacheService.Instance.GetFromRedis <PreviewService.Preview>(key);

            var  cacheTime = TimeSpan.FromDays(1);
            Task save      = null;

            if (preview == null)
            {
                if (!ItemDetails.Instance.TagLookup.ContainsKey(tag))
                {
                    throw new CoflnetException("unkown_item", "The requested item was not found, please file a bugreport");
                }
                preview = PreviewService.Instance.GetItemPreview(tag, 64);
                if (preview.Image == "cmVxdWVzdGVkIFVSTCBpcyBub3QgYWxsb3dlZAo=" || preview.Image == null)
                {
                    // transparent 64x64 image
                    preview.Image    = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAQAAAAAYLlVAAAAOUlEQVR42u3OIQEAAAACIP1/2hkWWEBzVgEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAYF3YDicAEE8VTiYAAAAAElFTkSuQmCC";
                    preview.MimeType = "image/png";
                    cacheTime        = TimeSpan.FromMinutes(1);
                    TrackingService.Instance.TrackPage("https://error" + path, "not found/" + tag, null, null);
                }
                save = CacheService.Instance.SaveInRedis <PreviewService.Preview>(key, preview, cacheTime);
            }
            context.SetContentType(preview.MimeType);
            context.WriteAsync(Convert.FromBase64String(preview.Image));
            if (save != null)
            {
                await save;
            }
        }
Exemplo n.º 2
0
        private static async Task WriteHeader(string path, Server.RequestContext res, string description, string title, string imageUrl, string keyword, string header)
        {
            title += " Hypixel SkyBlock Auction house history tracker";
            // shrink to fit
            while (title.Length > 65)
            {
                title = title.Substring(0, title.LastIndexOf(' '));
            }
            if (path == "/index.html")
            {
                path = "";
            }


            await res.WriteAsync(header
                                 .Replace(defaultText, description)
                                 .Replace(defaultTitle, title)
                                 .Replace("</title>", $"</title><meta property=\"keywords\" content=\"{keyword},hypixel,skyblock,auction,history,bazaar,tracker\" />"
                                          + $"<meta property=\"og:image\" content=\"{imageUrl}\" />"
                                          + $"<meta property=\"og:url\" content=\"https://sky.coflnet.com{path}\" />"
                                          + $"<meta property=\"og:title\" content=\"{title}\" />"
                                          + $"<meta property=\"og:description\" content=\"{description}\" />"
                                          + $"<link rel=\"canonical\" href=\"https://sky.coflnet.com{path}\" />"
                                          )
                                 + "</head>");

            res.ForceSend();
        }
Exemplo n.º 3
0
        public static async Task WriteEnd(
            this Server.RequestContext response, string stringContent
        )
        {
            await response.WriteAsync(stringContent + "</body></html>");
            //response.Close();

        }
Exemplo n.º 4
0
        private static async Task WriteStart(Server.RequestContext res, string content)
        {
            await res.WriteAsync(content);

            // res.SendChunked = true;
            res.AddHeader("cache-control", "public,max-age=" + 1800);

            res.ForceSend();
        }
Exemplo n.º 5
0
        public static async Task <string> ModifyContent(string path, byte[] contents, Server.RequestContext res)
        {
            string parameter = "";
            var    urlParts  = path.Split('/', '?', '#');

            if (urlParts.Length > 2)
            {
                parameter = urlParts[2];
            }
            string description     = "Browse over 200 million auctions, and the bazaar of Hypixel SkyBlock.";
            string longDescription = null;
            string title           = defaultTitle;
            string imageUrl        = "https://sky.coflnet.com/logo192.png";
            string keyword         = "";

            var    start       = Encoding.UTF8.GetString(contents).Split("<title>");
            var    headerStart = start[0] + "<title>";
            var    parts       = start[1].Split("</head>");
            string header      = parts.First();
            string html        = parts.Last().Substring(0, parts.Last().Length - 14);


            if (path.StartsWith("/p/"))
            {
                return(res.RedirectSkyblock(parameter, "player"));
            }
            if (path.StartsWith("/a/"))
            {
                return(res.RedirectSkyblock(parameter, "auction"));
            }
            if (path == "/item/" || path == "/item")
            {
                return(res.RedirectSkyblock());
            }

            // try to fill in title
            if (path.Contains("auction/"))
            {
                await WriteStart(res, headerStart);

                // is an auction
                using (var context = new HypixelContext())
                {
                    var result = context.Auctions.Where(a => a.Uuid == parameter)
                                 .Select(a => new { a.Tag, a.AuctioneerId, a.ItemName, a.End, bidCount = a.Bids.Count, a.Tier, a.Category }).FirstOrDefault();
                    if (result == null)
                    {
                        await WriteHeader("/error", res, "This site was not found", "Error", imageUrl, null, header);

                        await res.WriteEnd(html);

                        return("");
                    }


                    var playerName = PlayerSearch.Instance.GetNameWithCache(result.AuctioneerId);
                    title       = $"Auction for {result.ItemName} by {playerName}";
                    description = $"{title} ended on {result.End.ToString("yyyy-MM-dd HH\\:mm\\:ss")} with {result.bidCount} bids, Category: {result.Category}, {result.Tier}.";


                    if (!string.IsNullOrEmpty(result.Tag))
                    {
                        imageUrl = "https://sky.lea.moe/item/" + result.Tag;
                    }
                    else
                    {
                        imageUrl = SearchService.PlayerHeadUrl(result.AuctioneerId);
                    }

                    await WriteHeader(path, res, description, title, imageUrl, keyword, header);

                    longDescription = description
                                      + $"<ul><li> <a href=\"/player/{result.AuctioneerId}/{playerName}\"> other auctions by {playerName} </a></li>"
                                      + $" <li><a href=\"/item/{result.Tag}/{result.ItemName}\"> more auctions for {result.ItemName} </a></li></ul>";
                    keyword = $"{result.ItemName},{playerName}";
                }
            }
            else if (path.Contains("player/"))
            {
                if (parameter.Length < 30)
                {
                    var uuid = PlayerSearch.Instance.GetIdForName(parameter);
                    return(res.RedirectSkyblock(uuid, "player", uuid));
                }

                await WriteStart(res, headerStart);

                keyword     = PlayerSearch.Instance.GetNameWithCache(parameter);
                title       = $"{keyword} Auctions and bids";
                description = $"Auctions and bids for {keyword} in hypixel skyblock.";

                imageUrl = SearchService.PlayerHeadUrl(parameter);

                await WriteHeader(path, res, description, title, imageUrl, keyword, header);


                var auctions = GetAuctions(parameter, keyword);
                var bids     = GetBids(parameter, keyword);
                await res.WriteAsync(html);

                await res.WriteAsync(DETAILS_START + $"<h1>{title}</h1>{description} " + await auctions);

                await res.WriteEnd(await bids + PopularPages());

                return("");
            }
            else if (path.Contains("item/") || path.Contains("i/"))
            {
                if (path.Contains("i/"))
                {
                    return(res.RedirectSkyblock(parameter, "item", keyword));
                }
                if (!ItemDetails.Instance.TagLookup.ContainsKey(parameter))
                {
                    var upperCased = parameter.ToUpper();
                    if (ItemDetails.Instance.TagLookup.ContainsKey(upperCased))
                    {
                        return(res.RedirectSkyblock(upperCased, "item"));
                    }
                    // likely not a tag
                    parameter = HttpUtility.UrlDecode(parameter);
                    var thread = ItemDetails.Instance.Search(parameter, 1);
                    thread.Wait();
                    var item = thread.Result.FirstOrDefault();
                    keyword   = item?.Name;
                    parameter = item?.Tag;
                    return(res.RedirectSkyblock(parameter, "item", keyword));
                }
                await WriteStart(res, headerStart);

                keyword = ItemDetails.TagToName(parameter);


                var i = await ItemDetails.Instance.GetDetailsWithCache(parameter);

                path = CreateCanoicalPath(urlParts, i);
                var name = i?.Names?.FirstOrDefault();
                if (name != null)
                {
                    keyword = name;
                }

                title = $"{keyword} price ";
                float price = await GetAvgPrice(parameter);

                description = $"Price for item {keyword} in hypixel SkyBlock is {price.ToString("0,0.0")} on average. Visit for a nice chart and filter options";
                imageUrl    = "https://sky.lea.moe/item/" + parameter;
                if (parameter.StartsWith("PET_") && !parameter.StartsWith("PET_ITEM") || parameter.StartsWith("POTION"))
                {
                    imageUrl = i.IconUrl;
                }
                await WriteHeader(path, res, description, title, imageUrl, keyword, header);

                longDescription = description
                                  + AddAlternativeNames(i);

                longDescription += await GetRecentAuctions(i.Tag == "Unknown" || i.Tag == null?parameter : i.Tag);
            }
            else
            {
                if (path.Contains("/flipper"))
                {
                    title       = "Skyblock AH history auction flipper";
                    description = "Free auction house item flipper for Hypixel Skyblock";
                    keyword     = "flipper";
                }
                // unkown site, write the header
                await WriteStart(res, headerStart);
                await WriteHeader(path, res, description, title, imageUrl, keyword, header);
            }
            if (longDescription == null)
            {
                longDescription = description;
            }


            var newHtml = html + DETAILS_START
                          + BottomText(title, longDescription);

            await res.WriteEnd(newHtml);

            return(newHtml);
        }