public async Task <bool> UploadSingleImage(ImageUploadAvatarDTO model) { try { var imageDataByteArray = Convert.FromBase64String(model.Base64); var imageDataStream = new MemoryStream(imageDataByteArray); imageDataStream.Position = 0; var file = File(imageDataByteArray, model.ExtensionType); var fileName = model.FileName; if (file.FileContents.Length > 0) { await CommonUtil.UploadS3(model.FileName, model.PathSave, imageDataStream, model.ExtensionType); return(true); } else { return(false); } } catch (Exception ex) { _logger.LogError($"UploadSingleImage: " + ex.ToString()); return(false); } }
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); }
public async Task <IActionResult> ChangeLogo([FromBody] ImageUploadAvatarDTO model) { if (model.MainImage != null) { String[] substrings = model.MainImage.Base64.Split(','); model.Base64 = substrings[1]; } model.ExtensionType = model.MainImage.ExtensionType; model.FileName = model.MainImage.FileName; var result = await _repoWrapper.Profile.UpdateAvatar(model); return(Json(result.Message)); }
private async Task <bool> UploadImage(ImageUploadAvatarDTO model) { try { var pathAbsolute = @"C:\Domains\DaNBVQ\wwwroot\"; var imageDataByteArray = Convert.FromBase64String(model.Base64); var imageDataStream = new MemoryStream(imageDataByteArray); imageDataStream.Position = 0; var file = File(imageDataByteArray, model.ExtensionType); var fileName = model.FileName; var pathToSave = Path.Combine(pathAbsolute, model.PathSave); var outPath = Path.Combine(pathToSave, fileName); if (file.FileContents.Length > 0) { using (MemoryStream ms = new MemoryStream(imageDataByteArray)) { using (Bitmap bm2 = new Bitmap(ms)) { bm2.Save(Path.Combine(pathToSave, fileName)); } } return(true); } else { return(false); } } catch (Exception ex) { return(false); } }
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); } }