public async Task <IActionResult> DeleteSingleFile(int fileId) { File file = await fileService.FindAsync(fileId); User user = await HttpContext.GetContextUser(userService) .ConfigureAwait(false); if (file == null) { ProblemDetails problem = new ProblemDetails { Title = "File was not found.", Detail = "File was not found.", Instance = "9D3830A2-E7D1-4610-A147-1D43BFB8DDBC" }; return(NotFound(problem)); } bool isAllowed = userService.UserHasScope(user.IdentityId, nameof(Defaults.Scopes.FileWrite)); if (!(file.Uploader.Id.Equals(user.Id) || isAllowed)) { ProblemDetails problem = new ProblemDetails { Title = "Not authorized.", Detail = "You do not have the required permissions to delete this file.", Instance = "88967A6F-B168-44E2-A8E7-E9EBD555940E" }; return(Unauthorized(problem)); } try { await fileService.RemoveAsync(fileId) .ConfigureAwait(false); fileService.Save(); fileUploader.DeleteFileFromDirectory(file); return(Ok()); } catch (FileNotFoundException) { ProblemDetails problem = new ProblemDetails { Title = "File could not be deleted because the path does not exist.", Detail = "File could not be found.", Instance = "436349B4-50D9-49FD-8618-82367BEB7941" }; return(NotFound(problem)); } }
public async Task <IActionResult> UpdateProject(int projectId, [FromBody] ProjectResource projectResource) { Project project = await projectService.FindAsync(projectId) .ConfigureAwait(false); if (project == null) { ProblemDetails problem = new ProblemDetails { Title = "Failed to update project.", Detail = "The specified project could not be found in the database.", Instance = "b27d3600-33b0-42a0-99aa-4b2f28ea07bb" }; return(NotFound(problem)); } User user = await HttpContext.GetContextUser(userService) .ConfigureAwait(false); bool isAllowed = userService.UserHasScope(user.IdentityId, nameof(Defaults.Scopes.ProjectWrite)); if (!(project.UserId == user.Id || isAllowed)) { ProblemDetails problem = new ProblemDetails { Title = "Failed to edit the project.", Detail = "The user is not allowed to edit the project.", Instance = "906cd8ad-b75c-4efb-9838-849f99e8026b" }; return(Unauthorized(problem)); } if (projectResource.CallToAction != null) { IEnumerable <CallToActionOption> callToActionOptions = await callToActionOptionService.GetCallToActionOptionFromValueAsync( projectResource.CallToAction.OptionValue); if (!callToActionOptions.Any()) { ProblemDetails problem = new ProblemDetails { Title = "Call to action value was not found.", Detail = "The specified call to action value was not found while creating the project.", Instance = "40EE82EB-930F-40C8-AE94-0041F7573FE9" }; return(BadRequest(problem)); } } // Upload the new file if there is one File file = null; if (projectResource.FileId != 0) { if (project.ProjectIconId != 0 && project.ProjectIconId != null) { if (project.ProjectIconId != projectResource.FileId) { File fileToDelete = await fileService.FindAsync(project.ProjectIconId.Value); // Remove the file from the filesystem fileUploader.DeleteFileFromDirectory(fileToDelete); // Remove file from DB await fileService.RemoveAsync(project.ProjectIconId.Value); fileService.Save(); } } // Get the uploaded file file = await fileService.FindAsync(projectResource.FileId); if (file != null) { project.ProjectIcon = file; } else { ProblemDetails problem = new ProblemDetails { Title = "File was not found.", Detail = "The specified file was not found while updating project.", Instance = "69166D3D-6D34-4050-BD25-71F1BEBE43D3" }; return(BadRequest(problem)); } } mapper.Map(projectResource, project); projectService.Update(project); projectService.Save(); return(Ok(mapper.Map <Project, ProjectResourceResult>(project))); }