Exemplo n.º 1
0
        public async Task <IActionResult> Register([FromBody] UserRegisterViewModel model)
        {
            //return Ok();

            if (!ModelState.IsValid)
            {
                var errors = CustomValidator.GetErrorsByModel(ModelState);
                return(BadRequest(errors));
            }

            var base64 = model.Photo;

            if (base64.Contains(","))
            {
                base64 = base64.Split(',')[1];
            }
            var bmp = ImageUploader.FromBase64StringToImage(base64);

            var serverPath = _env.ContentRootPath;          //Directory.GetCurrentDirectory(); //_env.WebRootPath;

            var path = Path.Combine(serverPath, "Uploads"); //

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

            var width = 50;

            var fileName = string.Concat(width, "_", Path.GetRandomFileName(), ".jpg");

            var filePathSave = Path.Combine(path, fileName);

            bmp = ImageUploader.CreateImage(bmp, width, width);

            bmp.Save(filePathSave, ImageFormat.Jpeg);

            var user = new DbUser
            {
                Email       = model.Email,
                UserName    = model.Email,
                Image       = fileName, //"https://cdn.pixabay.com/photo/2017/07/28/23/34/fantasy-picture-2550222_960_720.jpg",
                Age         = 30,
                Phone       = model.Phone,
                Description = "PHP programmer"
            };

            var result = await _userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(BadRequest(new { invalid = "Хюстон у нас проблеми!" }));
            }

            return(Ok());
        }