예제 #1
0
        private void uploadButton_Click(object sender, EventArgs e)
        {
            UploadCard uploadform = new UploadCard();

            uploadform.loggedInUser = loggedInUser;
            uploadform.ShowDialog();
        }
예제 #2
0
        public async Task <IActionResult> CreateCard([FromForm] UploadCard body)
        {
            User user = _userService.UserFromContext(HttpContext);

            if (!user.Role.HasFlag(TradeSaberRole.Admin))
            {
                return(Unauthorized());
            }

            if (body.Cover == null)
            {
                return(BadRequest());
            }

            long size = body.Cover.Length;

            if (size > 15000000 || size == 0)
            {
                return(BadRequest());
            }

            Card card = new Card
            {
                Series          = body.Series,
                Name            = body.Name,
                Description     = body.Description,
                Master          = body.Master,
                MaxPrints       = body.MaxPrints,
                Rarity          = body.Rarity,
                Locked          = body.Locked,
                BaseProbability = body.BaseProbability
            };

            _cardDispatcher.Create(card);

            if (!ObjectId.TryParse(card.Series, out _))
            {
                return(NotFound());
            }

            Series series = _cardDispatcher.GetSeries(card.Series);

            if (series == null)
            {
                return(NotFound());
            }

            var path = Path.Combine(Directory.GetCurrentDirectory(), "Images");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            try
            {
                var    extension = Path.GetExtension(body.Cover.FileName);
                string newPath   = path + "/" + card.Id + "_base" + extension;
                using StreamReader sr = new StreamReader(body.Cover.OpenReadStream());
                using Stream stream   = System.IO.File.Create(newPath);
                await body.Cover.CopyToAsync(stream);

                card.BaseURL = "/images/" + card.Id + "_base" + extension;
                await stream.DisposeAsync();

                stream.Close();
                Image image = Image.FromFile(newPath);
                using MemoryStream ms = new MemoryStream();
                image.Save(ms, image.RawFormat);
                byte[] imageBytes = ms.ToArray();
                await ms.DisposeAsync();

                string b64          = $"data:image/{extension.ToLower().Replace(".", "")};base64," + Convert.ToBase64String(imageBytes);
                string newCoverPath = path + "/" + card.Id + "_cover.png";
                var    bytes        = await _cardGenerator.Generate(b64, card, series, newCoverPath);

                if (bytes.Length == 0)
                {
                    return(Problem());
                }
                await System.IO.File.WriteAllBytesAsync(newCoverPath, bytes);

                card.CoverURL = "/images/" + card.Id + "_cover.png";
                _cardDispatcher.Update(card);
                return(Ok(card));
            }
            catch
            {
                return(BadRequest());
            }
        }