Skip to content
Snippets Groups Projects

Fjern autentiseringskrav fra å hente profilbilder

Merged Jakob Karevold Grønhaug requested to merge profilbilde-auth into main
2 files
+ 3
10
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -99,26 +99,19 @@ public class FileController {
@@ -99,26 +99,19 @@ public class FileController {
@GetMapping("/{profileId}")
@GetMapping("/{profileId}")
public ResponseEntity<Resource> get(@PathVariable Integer profileId) {
public ResponseEntity<Resource> get(@PathVariable Integer profileId) {
String authenticatedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
Account loggedInAccount = accountRepo.findByEmail(authenticatedUsername).orElseThrow();
// Ensure that the provided profile ID is valid
// Ensure that the provided profile ID is valid
Profile profile = profileRepo.findById(profileId).orElseThrow(() -> {
profileRepo.findById(profileId).orElseThrow(() -> {
return new ResponseStatusException(HttpStatus.BAD_REQUEST);
return new ResponseStatusException(HttpStatus.BAD_REQUEST);
});
});
// Ensure that the profile is part of the authenticated account
if (!loggedInAccount.getId().equals(profile.getAccountId())) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
}
Path path = Paths.get(basePath, String.format("%d.jpeg", profileId));
Path path = Paths.get(basePath, String.format("%d.jpeg", profileId));
if (!path.toFile().exists()) {
if (!path.toFile().exists()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
}
Resource file = null;
Resource file;
try {
try {
file = new UrlResource(path.toUri());
file = new UrlResource(path.toUri());
Loading