-
-
Save DeebiKaaRaviSankar/a4c197df88faeae1086f86016b3384e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DisableRequestSizeLimit] | |
[HttpPost(DbConstants.Route_Add)] | |
public async Task<IActionResult> AddProfilePicture(IFormFile profilePicture) | |
{ | |
_user_id = _controllerService.GetUserId(HttpContext); | |
_user = await _userRepo.GetUser(user_id: _user_id); | |
var fileName = _controllerService.GenerateFileName(profilePicture.FileName, _user.UserName, DbConstants.ProfilePicture); | |
var fileUrl = ""; | |
BlobContainerClient container = new BlobContainerClient(_config["BlobStorage:ConnectionString"], "profile-picture"); | |
try | |
{ | |
BlobClient blob = container.GetBlobClient(fileName); | |
using (Stream stream = profilePicture.OpenReadStream()) | |
{ | |
blob.Upload(stream); | |
} | |
fileUrl = blob.Uri.AbsoluteUri; | |
var checkProfile = _context.ProfilePictures.Find(_user_id); | |
if (checkProfile != null) | |
{ | |
Console.WriteLine("updating picture"); | |
checkProfile.fileName = fileName; | |
checkProfile.ImagePath = fileUrl; | |
checkProfile.UploadedOn = DateTime.Now; | |
ProfilePicture response = await _profilePictureRepo.UpdateProfilePicture(checkProfile); | |
_profilePictureDTO = _mapper.Map<ProfilePictureDTO>(response); | |
} | |
else | |
{ | |
Console.WriteLine("Adding picture"); | |
_profilePicture.UserId = _user_id; | |
_profilePicture.ImagePath = fileUrl; | |
_profilePicture.UploadedOn = DateTime.Now; | |
_profilePicture.fileName = fileName; | |
ProfilePicture response = await _profilePictureRepo.AddProfilePicture(_profilePicture); | |
_profilePictureDTO = _mapper.Map<ProfilePictureDTO>(response); | |
} | |
return await Task.FromResult(Ok(_profilePictureDTO)); | |
} | |
catch | |
{ | |
var result = fileUrl; | |
return Ok(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment