public async Task <IActionResult> Edit([FromBody] User_Update_BindingModel collection) { HeaderValidator(collection); try { var fileName = string.Empty; var model = _mapper.Map <Void_Schema>(collection); var user = await _userService.GetAsync(model); switch (model.StatusCode) { case 400: return(BadRequest(_stringLocalizer[SharedResource.AuthenticationFailed])); case 405: return(BadRequest(_stringLocalizer[SharedResource.DeviceIsNotActive])); case 410: return(BadRequest(_stringLocalizer[SharedResource.UserIsNotActive])); } if (collection.Avatar != null) { using (var memoryStream = new MemoryStream()) { await collection.Avatar.CopyToAsync(memoryStream); if (memoryStream.ToArray().Length == 0) { return(BadRequest(_stringLocalizer["FileHasNoContent"])); } if (memoryStream.ToArray().Length > int.Parse(AppSettings.AvatarSize) * 1024) { return(BadRequest(_stringLocalizer["FileSizeIsTooMuch"])); } var image = new Bitmap(memoryStream); if (!ImageFormats.Contains(image.RawFormat)) { return(BadRequest(_stringLocalizer["WrongFileType"])); } var avatarResolution = AppSettings.AvatarResolution.Split('x'); if (image.Width * image.Height > int.Parse(avatarResolution[0]) * int.Parse(avatarResolution[1])) { return(BadRequest(_stringLocalizer["FileResolutionIsTooMuch"])); } if (!Directory.Exists(AppSettings.FilePath)) { Directory.CreateDirectory(AppSettings.FilePath); } if (string.IsNullOrWhiteSpace(user.Avatar)) { fileName = $"{user.Username}_001"; } else { var avatarArray = user.Username.Split('_'); var avatarNo = int.Parse(avatarArray[1]); fileName = (++avatarNo).ToString(); for (var index = 0; index < (3 - avatarNo.ToString().Length); index++) { fileName = "0" + fileName; } fileName = $"{user.Username}_{fileName}"; } image.Save($@"{AppSettings.FilePath}\{fileName}.jpeg", ImageFormat.Jpeg); } } var editModel = new User_Update_Schema { Token = collection.Token, DeviceId = collection.DeviceId, Avatar = $"{fileName}.jpeg", Nickname = collection.Nickname, BirthDate = collection.BirthDate.ToDateTime(null) }; await _userService.UpdateAsync(editModel); switch (model.StatusCode) { case 200: return(Ok()); } } catch (Exception ex) { await _exceptionService.InsertAsync(ex, URL, IP); } return(InternalServerError()); }
public async Task <IActionResult> Avatar([FromForm] AvatarBindingModel collection) { if (string.IsNullOrWhiteSpace(collection?.Token)) { return(BadRequest(GeneralMessage.TokenNotFound)); } if (string.IsNullOrWhiteSpace(collection?.DeviceId)) { return(BadRequest(GeneralMessage.DeviceIdNotFound)); } try { var model = _mapper.Map <UserGetSchema>(collection); var user = await _userService.GetAsync(model); switch (model.StatusCode) { case -1: return(BadRequest(GeneralMessage.UserNotFound)); case -2: return(BadRequest(GeneralMessage.UserIsNotActive)); case -3: return(BadRequest(GeneralMessage.PhoneIsNotVerified)); } using (var memoryStream = new MemoryStream()) { await collection.Avatar.CopyToAsync(memoryStream); if (memoryStream.ToArray().Length == 0) { return(BadRequest("فایل فاقد محتوی می باشد")); } if (memoryStream.ToArray().Length > int.Parse(AppSettings.AvatarSize) * 1024) { return(BadRequest("حجم تصویر شما بیشتر از میزان مجاز است")); } var image = new Bitmap(memoryStream); if (!ImageFormats.Contains(image.RawFormat)) { return(BadRequest("نوع تصویر شما از انواع مجاز نمی باشد")); } var avatarResolution = AppSettings.AvatarResolution.Split('x'); if (image.Width * image.Height > int.Parse(avatarResolution[0]) * int.Parse(avatarResolution[1])) { return(BadRequest("اندازه تصویر شما بزرگتر از میزان مجاز است")); } if (!Directory.Exists(AppSettings.FilePath)) { Directory.CreateDirectory(AppSettings.FilePath); } image.Save($@"{AppSettings.FilePath}\{user.UserName}.jpeg", ImageFormat.Jpeg); } var editModel = new UserEditSchema { Token = collection.Token, DeviceId = collection.DeviceId, Avatar = $"{user.UserName}.jpeg" }; await _userService.EditAsync(editModel); switch (model.StatusCode) { case 1: return(Ok()); } } catch (Exception ex) { await _exceptionService.InsertAsync(ex, URL, IP); } return(InternalServerError()); }