示例#1
0
        public async Task <ImageApiModel> UpdateUserPhotoAsync(ImageApiModel model, string userId, HttpRequest request)
        {
            var user = await _userRepository.GetByIdAsync(userId);

            if (user == null)
            {
                throw new NotFoundException($"{_resourceManager.GetString("UserNotFound")}: {userId}");
            }

            string base64 = model.Photo;

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

            var serverPath = _env.ContentRootPath; //Directory.GetCurrentDirectory(); //_env.WebRootPath;
            var folderName = _configuration.GetValue <string>("ImagesPath");
            var path       = Path.Combine(serverPath, folderName);

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

            string ext          = ".jpg";
            string fileName     = Guid.NewGuid().ToString("D") + ext;
            string filePathSave = Path.Combine(path, fileName);

            string filePathDelete = null;

            if (user.UserProfile != null && user.UserProfile.Photo != null)
            {
                filePathDelete = Path.Combine(path, user.UserProfile.Photo);
            }

            //Convert Base64 Encoded string to Byte Array.
            byte[] imageBytes = Convert.FromBase64String(base64);
            File.WriteAllBytes(filePathSave, imageBytes);

            var result = await _userRepository.UpdateUserPhotoAsync(user, fileName);

            if (!result)
            {
                throw new BadRequestException(_resourceManager.GetString("PhotoNotChanged"));
            }

            if (File.Exists(filePathDelete))
            {
                File.Delete(filePathDelete);
            }

            string pathPhoto = $"{request.Scheme}://{request.Host}/{_configuration.GetValue<string>("UrlImages")}/";

            fileName = fileName != null ? pathPhoto + fileName : null;

            return(new ImageApiModel {
                Photo = fileName
            });
        }
示例#2
0
        public async Task <IActionResult> UploadImageBook([FromBody] ImageApiModel image, [FromQuery] string bookId)
        {
            if (bookId == null)
            {
                return(BadRequest(new MessageApiModel()
                {
                    Message = "Потрібно ID книги"
                }));
            }

            var book = await _bookRepo.GetByIdAsync(bookId);

            if (image.Photo != null)
            {
                string base64 = image.Photo;
                if (base64.Contains(","))
                {
                    base64 = base64.Split(',')[1];
                }

                var serverPath = _env.ContentRootPath;
                var folderName = _configuration.GetValue <string>("BooksPath");
                var path       = Path.Combine(serverPath, folderName);

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

                string ext          = ".jpg";
                string fileName     = Guid.NewGuid().ToString("D") + ext;
                string filePathSave = Path.Combine(path, fileName);

                //Convert Base64 Encoded string to Byte Array.
                byte[] imageBytes = Convert.FromBase64String(base64);
                System.IO.File.WriteAllBytes(filePathSave, imageBytes);

                book.Image = fileName;
                await _bookRepo.UpdateAsync(book);

                return(Ok(new MessageApiModel {
                    Message = fileName
                }));
            }
            else
            {
                return(BadRequest(new MessageApiModel()
                {
                    Message = "Потрібно фото"
                }));
            }
        }
        public async Task <IActionResult> UserCreateAsync([FromBody] ImageApiModel image, string userId)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new MessageApiModel()
                {
                    Message = string.Join("; ", ModelState.Values
                                          .SelectMany(x => x.Errors)
                                          .Select(x => x.ErrorMessage))
                }));
            }

            var result = await _userService.UpdateUserPhotoAsync(image, userId, Request);

            return(Ok(result));
        }
        public async Task <IActionResult> ChangeUserPhoto([FromBody] ImageApiModel model)
        {
            ImageBase64Validator validator = new ImageBase64Validator(_resourceManager);
            var validResult = validator.Validate(model);

            if (!validResult.IsValid)
            {
                return(BadRequest(new MessageApiModel()
                {
                    Message = validResult.ToString()
                }));
            }

            var userId = User.FindFirst("id")?.Value;
            var result = await _userService.UpdateUserPhotoAsync(model, userId, Request);

            return(Ok(result));
        }
 public async Task UploadBookImage(string bookId, ImageApiModel model)
 {
     var query = QueryHelpers.AddQueryString(ApiUrls.FileUploadImage, "bookId", bookId);
     await _httpService.Post <ImageApiModel, MessageApiModel>(query, model);
 }