Skip to content

Instantly share code, notes, and snippets.

@DeebiKaaRaviSankar
Created August 19, 2022 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeebiKaaRaviSankar/a4c197df88faeae1086f86016b3384e6 to your computer and use it in GitHub Desktop.
Save DeebiKaaRaviSankar/a4c197df88faeae1086f86016b3384e6 to your computer and use it in GitHub Desktop.
[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