public async Task <IActionResult> Add() { var token = HttpContext.Request.Headers["Authorization"].Last().Split(" ").Last(); string[] roles = { "User" }; var handler = new JwtSecurityTokenHandler(); if (RoleService.CheckRoles(token, roles, _userManager)) { var httpRequest = HttpContext.Request; var file = httpRequest.Body; //checks the size of file var imageHandler = new ImageSecurityHandler(); if (!imageHandler.CheckFileSize(httpRequest.ContentLength.Value)) { _logger.LogInformation($"size is {httpRequest.ContentLength}"); return(BadRequest("Photo must be between 5KB and 5MB")); } //checks the format of file if (!imageHandler.CheckFileFormat(httpRequest.ContentType)) { _logger.LogInformation($"file format is {httpRequest.ContentType}"); return(BadRequest("Wrong file format")); } var sub = handler.ReadJwtToken(token).Payload.Sub; var credentials = GoogleCredential.FromFile("../Infrastructure/Images/GCStorage/Rosta-a2299c0ab851.json"); var storage = StorageClient.CreateAsync(credentials); var lastId = 0; if (storage.Result .ListObjects("deep-castle-261418-user-photo-bucket") .Select(x => x.Name) .Count(x => x.Contains(sub)) > 0) { lastId = int.Parse(storage.Result .ListObjects("deep-castle-261418-user-photo-bucket") .Select(x => x.Name).Last(x => x.Contains(sub)) .Split("-").Last()); } var detailsRepository = new UserDetailsRepository(); var details = detailsRepository.GetByUserId(sub); var candidacyRepository = new CandidacyRepository(); var candidacy = candidacyRepository.GetAll().Last(x => x.OwnerId == details.Id); //Checks if User have candidacy if (candidacyRepository.GetAll().Count(x => x.OwnerId == details.Id) == 0) { return(BadRequest("User didnt submited candidacy.")); } //Uploading Photo to Google Cloud and updating indecies. var photoName = $"{sub}-profilePhoto-{lastId + 1}"; storage.Result.UploadObject("deep-castle-261418-user-photo-bucket", photoName, MediaTypeNames.Image.Jpeg, file, null); candidacy.PhotoPath = photoName; candidacyRepository.Edit(candidacy); return(Ok()); } return(Unauthorized()); }
public async Task <IActionResult> Add(int id) { var token = HttpContext.Request.Headers["Authorization"].Last().Split(" ").Last(); string[] roles = { "User", "Admin", "SchoolAdmin" }; var handler = new JwtSecurityTokenHandler(); if (RoleService.CheckRoles(token, roles, _userManager)) { var httpRequest = HttpContext.Request; var file = httpRequest.Body; //checks the size of file var imageHandler = new ImageSecurityHandler(); if (!imageHandler.CheckFileSize(httpRequest.ContentLength.Value)) { _logger.LogInformation($"size is {httpRequest.ContentLength}"); return(BadRequest("Photo must be between 5KB and 5MB")); } //checks the format of file if (!imageHandler.CheckFileFormat(httpRequest.ContentType)) { _logger.LogInformation($"file format is {httpRequest.ContentType}"); return(BadRequest("Wrong file format")); } var sub = handler.ReadJwtToken(token).Payload.Sub; var credentials = GoogleCredential.FromFile( PathHelper.GetCredentialsPath()); var storage = StorageClient.CreateAsync(credentials); var lastId = 0; if (storage.Result .ListObjects("deep-castle-261418-survey-photo-bucket") .Select(x => x.Name) .Count(x => x.Contains(sub)) > 0) { lastId = int.Parse(storage.Result .ListObjects("deep-castle-261418-survey-photo-bucket") .Select(x => x.Name).Last(x => x.Contains(sub)) .Split("-").Last()); } var surveyRepo = new SurveyRepository(); if (!surveyRepo.GetAll().Select(x => x.Id).Contains(id)) { return(BadRequest($"Survey doesnt with {id} exsit")); } var detailsRepo = new UserDetailsRepository(); var detailsId = detailsRepo.GetByUserId(sub).Id; if (surveyRepo.GetAll().First(x => x.Id == id).AuthorId != detailsId) { return(BadRequest("You dont have rights to edit that survey")); } var survey = surveyRepo.GetById(id); var photoPath = $"{sub}-{survey.Id}-surveyPhoto-{lastId + 1}"; storage.Result.UploadObject("deep-castle-261418-survey-photo-bucket", photoPath, MediaTypeNames.Image.Jpeg, file, null); survey.PhotoPath = photoPath; surveyRepo.Edit(survey); return(Ok()); } return(Unauthorized()); }