public async Task <IActionResult> AddPhotos(int schoolId, [FromForm] IFormFileCollection photos) { var school = await database.SchoolRepository.Get <School>(schoolId); int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != school.OwnerId && !await rolesService.IsPermitted(RolesPermitted, currentUserId)) { return(this.Forbidden("Nie jesteś właścicielem tej szkółki")); } if (school.SchoolPhotos.Count + photos.Count > 10) { return(BadRequest("Maksymalna ilość zdjęć dla szkółki wynosi 10")); } string uploadPath = filesUploader.FilesPath + $@"\{schoolId}\"; foreach (var photo in photos) { var uploadedPhoto = await filesUploader.Upload(photo, uploadPath); string photoUrl = null; if (uploadedPhoto != null) { photoUrl = filesUploader.CombinePaths($@"{filesUploader.FilesUrl}/{schoolId}/", uploadedPhoto.FileName); } var schoolPhoto = new SchoolPhoto(school.Id); schoolPhoto.SetFilePathAndUrl(uploadedPhoto.FilePath, photoUrl); school.SchoolPhotos.Add(schoolPhoto); } if (await database.Complete()) { return(Ok("Zdjęcia zostały dodane do galerii")); } return(BadRequest("Nie dodano żadnych zdjęć do galerii")); }
private async Task <string> GeneratePhotoUrl(int id, UserToSetPhotoDto userToSetPhotoDto) { string uploadPath = GetAndClearUserDirectoryPath(id); var uploadedPhoto = await filesUploader.Upload(userToSetPhotoDto.Photo, uploadPath); string photoUrl = null; if (uploadedPhoto != null) { photoUrl = filesUploader.CombinePaths($@"{filesUploader.FilesUrl}/{id}/", uploadedPhoto.FileName); } return(photoUrl); }