public async Task <byte[]> Generate(string b64, Card card, Series series, string path) { string rarityColor = "white"; switch (card.Rarity) { case Rarity.Common: rarityColor = "white"; break; case Rarity.Uncommon: rarityColor = "#26f1ff"; break; case Rarity.Rare: rarityColor = "#d426ff"; break; case Rarity.Legendary: rarityColor = "goldenrod"; break; } string finalHTML = GetFormattedCardHTML(series.MainColor, series.SubColor, b64, card.Name, card.Description, rarityColor, card.Rarity.ToString(), _font); HTIBody body = new HTIBody { width = 872, height = 1272, html = finalHTML }; var response = await _client.PostAsync(_apiSettings.HTI, new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json")); if (response.StatusCode == HttpStatusCode.OK) { byte[] bytes = await response.Content.ReadAsByteArrayAsync(); return(bytes); } return(new byte[0]); }
/// <summary> /// This is very time consuming! Don't upload any new cards while this is running! /// </summary> public async void UpdateAllCardHTML(IProgress <float> updateProgress, string cardPath) { IProgress <float> progress = updateProgress; progress.Report(0.0f); Series[] allSeries = _cardDispatcher.GetAllSeries(); float currentProgress = 0.0f; float seriesProgressSegments = 1f / allSeries.Length; string path = cardPath; for (int i = 0; i < allSeries.Length; i++) { Series currentSeries = allSeries[i]; Card[] cards = _cardDispatcher.GetCardsFromSeries(currentSeries.Id); for (int q = 0; q < cards.Length; q++) { Card card = cards[q]; string rarityColor = "white"; switch (card.Rarity) { case Rarity.Common: rarityColor = "white"; break; case Rarity.Uncommon: rarityColor = "#26f1ff"; break; case Rarity.Rare: rarityColor = "#d426ff"; break; case Rarity.Legendary: rarityColor = "goldenrod"; break; } Image image = Image.FromFile(path + card.BaseURL); using MemoryStream ms = new MemoryStream(); image.Save(ms, image.RawFormat); byte[] imageBytes = ms.ToArray(); await ms.DisposeAsync(); var extension = Path.GetExtension(card.BaseURL); string b64 = $"data:image/{extension.ToLower().Replace(".", "")};base64," + Convert.ToBase64String(imageBytes); string finalHTML = GetFormattedCardHTML(currentSeries.MainColor, currentSeries.SubColor, b64, card.Name, card.Description, rarityColor, card.Rarity.ToString(), _font); HTIBody body = new HTIBody { width = 872, height = 1272, html = finalHTML }; var response = await _client.PostAsync(_apiSettings.HTI, new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json")); if (response.StatusCode == HttpStatusCode.OK) { byte[] bytes = await response.Content.ReadAsByteArrayAsync(); await File.WriteAllBytesAsync(path + card.CoverURL, bytes); } } currentProgress += seriesProgressSegments; progress.Report(currentProgress); } progress.Report(1.0f); }