private static async Task <PreviewResult> GenerateOrFetchPreview(PreviewResult result, string skinId, int?version, DragonCache dragon, bool isDressingRoom, bool swapSilhouette, bool force) { var azureImageService = new AzureImageService(); using (var ctx = new DataContext()) { Skin skin; var skins = ctx.Skins.Where(x => x.GeneratedId == skinId); if (version == null) { skin = skins.OrderByDescending(x => x.Version).FirstOrDefault(); version = skin.Version; } else { skin = skins.FirstOrDefault(x => x.Version == version); } if (skin == null) { return(result.WithErrorMessage("Skin not found.")); } result.Skin = skin; if (dragon == null) { dragon = FRHelpers.ParseUrlForDragon(string.Format(FRHelpers.DressingRoomDummyUrl, skin.DragonType, skin.GenderType), skinId, version); } result.Dragon = dragon; if (dragon.Age == Age.Hatchling) { return(result.WithErrorMessage("Skins can only be previewed on adult dragons.")); } if (swapSilhouette) { var swappedDragon = FRHelpers.ParseUrlForDragon(GeneratedFRHelpers.GenerateDragonImageUrl(dragon, swapSilhouette)); swappedDragon.FRDragonId = dragon.FRDragonId; dragon = swappedDragon; } if (skin.DragonType != (int)dragon.DragonType) { return(result.WithErrorMessage("This skin is meant for a {0} {1}, the dragon you provided is a {2} {3}.", (DragonType)skin.DragonType, (Gender)skin.GenderType, dragon.DragonType, dragon.Gender)); } if (skin.GenderType != (int)dragon.Gender) { return(result.WithErrorMessage("This skin is meant for a {0}, the dragon you provided is a {1}.", (Gender)skin.GenderType, dragon.Gender)); } } Bitmap dragonImage = null; var azureImagePreviewPath = $@"previews\{skinId}\{(version == 1 ? "" : $@"{version}\")}{dragon.GetFileName()}.png";
public async Task <ActionResult> DressResult(DressModelViewModel model) { var scryerDragon = FRHelpers.ParseUrlForDragon(model.ScryerUrl); var dressingRoomDragon = FRHelpers.ParseUrlForDragon(model.DressingRoomUrl); if (scryerDragon.Age == Data.Age.Hatchling) { AddErrorNotification($"You can only dress an adult dragon!"); return(RedirectToRoute("ScryerDresser")); } if (scryerDragon.DragonType != dressingRoomDragon.DragonType) { AddErrorNotification($"The breeds of the two images do not match. Scryer image is a <b>{scryerDragon.DragonType}</b> while the dressing room is a <b>{dressingRoomDragon.DragonType}</b>!"); return(RedirectToRoute("ScryerDresser")); } if (scryerDragon.Gender != dressingRoomDragon.Gender) { AddErrorNotification($"The genders of the two images do not match. Scryer image is a <b>{scryerDragon.Gender}</b> while the dressing room is a <b>{dressingRoomDragon.Gender}</b>!"); return(RedirectToRoute("ScryerDresser")); } var azureImageService = new AzureImageService(); var azureImagePreviewPath = $@"previews\dresser\{scryerDragon.ToString().Trim('_')}\{dressingRoomDragon.Apparel?.Replace(',', '-').ToString()}.png"; var previewUrl = await azureImageService.Exists(azureImagePreviewPath); if (previewUrl == null) { var invisibleDragon = await FRHelpers.GetInvisibleDressingRoomDragon(dressingRoomDragon); var baseDragon = await FRHelpers.GetDragonBaseImage(scryerDragon); using (var graphics = Graphics.FromImage(baseDragon)) { graphics.DrawImage(invisibleDragon, new Rectangle(0, 0, 350, 350)); } using (var memStream = new MemoryStream()) { baseDragon.Save(memStream, ImageFormat.Png); memStream.Position = 0; previewUrl = await azureImageService.WriteImage(azureImagePreviewPath, memStream); } } return(View(new DressModelResultViewModel { PreviewUrl = previewUrl })); }
public async Task <ActionResult> Manage(ManageModelPost model) { var skin = DataContext.Skins.FirstOrDefault(x => x.GeneratedId == model.SkinId && x.SecretKey == model.SecretKey); if (skin == null) { AddErrorNotification("Skin not found or secret invalid"); return(RedirectToRoute("Home")); } else { if (skin.Creator != null) { if (!Request.IsAuthenticated) { AddErrorNotification("This skin is linked to an acocunt, please log in to manage this skin."); return(RedirectToRoute("Home")); } else if (skin.Creator.Id != HttpContext.GetOwinContext().Authentication.User.Identity.GetUserId <int>()) { AddErrorNotification("This skin is linked to a different account than the one that is logged in, you can only manage your own or unclaimed skins."); return(RedirectToRoute("Home")); } } if (skin.GenderType != (int)model.Gender || skin.DragonType != (int)model.DragonType) { var azureImageService = new AzureImageService(); await azureImageService.DeleteImage($@"previews\{model.SkinId}\{(skin.Version == 1 ? "" : $@"{skin.Version}\")}preview.png"); } skin.Title = model.Title; skin.Description = model.Description; skin.GenderType = (int)model.Gender; skin.DragonType = (int)model.DragonType; skin.Visibility = model.Visibility; await DataContext.SaveChangesAsync(); AddSuccessNotification("Changes have been saved!"); return(RedirectToRoute("Manage", new { model.SkinId, model.SecretKey })); }
public async Task <ActionResult> Upload(UploadModelPost model) { var azureImageService = new AzureImageService(); var randomizedId = CodeHelpers.GenerateId(5, DataContext.Skins.Select(x => x.GeneratedId).ToList()); var secretKey = CodeHelpers.GenerateId(7); Bitmap skinImage = null; try { skinImage = (Bitmap)Image.FromStream(model.Skin.InputStream); if (skinImage.Width != 350 || skinImage.Height != 350) { AddErrorNotification("Image needs to be 350px x 350px. Just like FR."); return(View()); } } catch { AddErrorNotification("Upload is not a valid png image"); return(View()); } try { var fixPixelFormat = SkinTester.FixPixelFormat(skinImage); if (fixPixelFormat != null) { skinImage = fixPixelFormat; } model.Skin.InputStream.Position = 0; var url = await azureImageService.WriteImage($@"skins\{randomizedId}.png", model.Skin.InputStream); Bitmap dragonImage = null; using (var client = new WebClient()) { var dwagonImageBytes = client.DownloadDataTaskAsync(string.Format(FRHelpers.DressingRoomDummyUrl, (int)model.DragonType, (int)model.Gender)); try { using (var memStream = new MemoryStream(await dwagonImageBytes, false)) dragonImage = (Bitmap)Image.FromStream(memStream); } catch { } } var skin = new Skin { GeneratedId = randomizedId, SecretKey = secretKey, Title = model.Title, Description = model.Description, DragonType = (int)model.DragonType, GenderType = (int)model.Gender, Coverage = GetCoveragePercentage(skinImage, dragonImage) }; skinImage.Dispose(); if (Request.IsAuthenticated) { var userId = HttpContext.GetOwinContext().Authentication.User.Identity.GetUserId <int>(); skin.Creator = DataContext.Users.FirstOrDefault(x => x.Id == userId); skin.Visibility = skin.Creator.ProfileSettings.DefaultSkinsArePublic ? skin.Creator.ProfileSettings.DefaultShowSkinsInBrowse ? SkinVisiblity.Visible : SkinVisiblity.HideFromBrowse : SkinVisiblity.HideEverywhere; } DataContext.Skins.Add(skin); await DataContext.SaveChangesAsync(); return(View("UploadResult", new UploadModelPostViewModel { SkinId = randomizedId, SecretKey = secretKey, PreviewImageUrl = (await SkinTester.GenerateOrFetchDummyPreview(randomizedId, skin.Version)).Urls[0] })); } catch { AddErrorNotification("Something went wrong uploading"); return(View()); } }