public async Task <IActionResult> UploadImage([FromForm] GameFileInputModel file)
        {
            var foundGame = await _gameService.Get(file.GameId); // Needs to be a game that can use image

            if (foundGame == null)
            {
                return(BadRequest()); // Dont add a image that would not be used
            }

            foundGame.Images.Add(_gameService.CreateImageFile(file));
            await _gameService.Update(foundGame.GameId, foundGame);

            return(CreatedAtRoute("GetGameImage", new { gameId = file.GameId }, foundGame));
        }
Exemple #2
0
        public string CreateImageFile(GameFileInputModel file)
        {
            string wwwrootPath    = _hosting.WebRootPath;
            string gameFolderPath = Path.Combine(wwwrootPath, "images", "games", file.GameId);

            if (!Directory.Exists(gameFolderPath))
            {
                Directory.CreateDirectory(gameFolderPath);
            }
            string absolutePath = Path.Combine(gameFolderPath, file.File.FileName);

            using (var fileStream = new FileStream(absolutePath, FileMode.Create)){
                file.File.CopyTo(fileStream);
            }

            return(Path.Combine("images", "games", file.GameId, file.File.FileName));
        }