public async Task <IFileResponse> UploadProfileImageAsync(UploadProfileImageView model) { try { var userInfo = await _appDbContext.UserInfos.Include(k => k.User).FirstOrDefaultAsync(k => k.Id == model.UserId); var data = new File() { IdGuid = Guid.NewGuid(), SizeMb = GetFileSize(model.File.Length), Name = model.File.FileName, ProviderName = "SqlProvider", ExtraParams = model.ExtraParams, Created = DateTime.UtcNow, MimeType = model.File.ContentType, Modified = DateTime.UtcNow, CreatedBy = userInfo.Email, ModifiedBy = userInfo.Email }; var savedEntity = (await _appDbContext.Files.AddAsync(data)).Entity; await _appDbContext.SaveChangesAsync(); var user = await _appDbContext.Users.FirstOrDefaultAsync(k => k.Id == model.UserId); user.OriginalImageFileId = savedEntity.Id; user.SmallImageFileId = savedEntity.Id; user.LargeImageFileId = savedEntity.Id; await _appDbContext.SaveChangesAsync(); var fileDb = new FileDB() { Id = savedEntity.IdGuid, Bytes = StreamToBytes(model.File.OpenReadStream()) }; await _fileDbContext.FileDB.AddAsync(fileDb); await _fileDbContext.SaveChangesAsync(); return(new FileResponse(true, "", HttpStatusCode.Accepted) { ProfileCompletedPercentage = await _profilePercentageCalculation.UpdateProfileCompletedPercentageAsync(model.UserId), UploadedFileUrl = ConstantUrlPath.ProfileImagePath + savedEntity.Id }); } catch (Exception e) { return(new FileResponse(e)); } }
public async Task <IActionResult> UploadProfileImageAsync([FromForm] UploadProfileImageView viewModel) { var model = await _fileService.UploadProfileImageAsync(viewModel).ConfigureAwait(false); return(Ok(model)); }