Example #1
0
        public async Task <UpdateAvatarDTO> UpdateAvatar(ImageUploadAvatarDTO updateAvatar)
        {
            UpdateAvatarDTO output  = new UpdateAvatarDTO();
            var             token   = _httpContextAccessor.HttpContext.User.FindFirst(p => p.Type == "access_token").Value;
            var             user    = _httpContextAccessor.HttpContext.User.FindFirst(p => p.Type == "UserInfomation").Value;
            var             jwt     = JsonConvert.DeserializeObject <SumProfileResponseDTO>(_httpContextAccessor.HttpContext.Request.Cookies["JWT"]);
            var             jwtUser = JsonConvert.DeserializeObject <SumProfileResponseDTO>(user);

            if (jwt != null)
            {
                _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwt.JWT);
                string apiUrl = $"/api/v1/ImageUpload/UpdateAvatar";
                updateAvatar.UserId = jwt.UserId;
                var json          = JsonConvert.SerializeObject(updateAvatar, Formatting.Indented);
                var stringContent = new StringContent(json, Encoding.UTF8, "application/json");

                var response = await _client.PostAsync(apiUrl, stringContent);

                if (response.IsSuccessStatusCode)
                {
                    string responseStream = await response.Content.ReadAsStringAsync();

                    output = JsonConvert.DeserializeObject <UpdateAvatarDTO>(responseStream);
                }
            }
            return(output);
        }
Example #2
0
        public async Task <UpdateAvatarDTO> UpdateAvatar(ImageUploadAvatarDTO model)
        {
            model.ExtensionType = "image/jpeg";
            var response = new UpdateAvatarDTO();
            var profile  = new AspNetUserProfiles();

            profile = _repositoryWrapper.AspNetUserProfiles.FirstOrDefault(p => p.UserId == model.UserId);
            if (profile == null)
            {
                _logger.LogError($"[ManageController] {ConstMessage.GetMsgConst("ACC008")}");
                response.ErrorCode = "ACC008";
                response.Message   = ConstMessage.GetMsgConst("ACC008");
                return(response);
            }
            try
            {
                if (model.Base64.Length > 0)
                {
                    using (var client = new HttpClient())
                    {
                        //client.BaseAddress = new Uri("https://cdn.hanoma.vn/api/UploadFile/UploadSingleImage");
                        //client.DefaultRequestHeaders.Accept.Clear();
                        //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        model.PathSave = "user/avatar/original";
                        var stringContent = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
                        //var buffer = System.Text.Encoding.UTF8.GetBytes(content);
                        //var byteContent = new ByteArrayContent(buffer);
                        // HTTP POST
                        //HttpResponseMessage responseSent = await client.PostAsync("https://cdn.hanoma.vn/api/UploadFile/UploadSingleImage", stringContent);
                        //File Extension Type

                        model.FileName = profile.UserId + "-" + DateTime.Now.ToString("dd-MM-yyyy") + "-" + DateTime.Now.ToString("HH-mm-ss") + "." + model.ExtensionType.Replace("image/", "");
                        var responseUpload = await UploadImage(model);

                        if (responseUpload)
                        {
                            response.UserId    = model.UserId;
                            response.AvatarUrl = model.FileName;

                            _mapper.Map(response, profile);
                            _repositoryWrapper.AspNetUserProfiles.UpdateProfilers(profile);
                            _repositoryWrapper.Save();
                            response.ErrorCode = "00";
                            response.Message   = "Upload thành công";
                            return(response);
                        }
                        else
                        {
                            response.ErrorCode = "002";
                            response.Message   = ConstMessage.GetMsgConst("002");
                            return(response);
                        }
                    }
                }
                else
                {
                    response.ErrorCode = "ACC014";
                    response.Message   = ConstMessage.GetMsgConst("ACC014");
                    return(response);
                }
            }
            catch (Exception ex)
            {
                response.ErrorCode = "002";
                response.Message   = ConstMessage.GetMsgConst("002") + " " + ex.Message.ToString();
                return(response);
            }
        }