MME-30 Fix null pointer exception when getUser returns null; better querying of Images.

This commit is contained in:
Jesse Brault 2026-02-23 17:01:05 -06:00
parent 96a7807ab5
commit 517ff1ad41
3 changed files with 5 additions and 20 deletions

View File

@ -60,8 +60,7 @@ public class ImageController {
@PathVariable String username, @PathVariable String username,
@PathVariable String filename @PathVariable String filename
) throws IOException { ) throws IOException {
final User owner = this.userService.getUser(username); final Image image = this.imageService.getByUsernameAndFilename(username, filename, principal);
final Image image = this.imageService.getByOwnerAndFilename(owner, filename, principal);
final InputStream imageInputStream = this.imageService.getImageContent(image, principal); final InputStream imageInputStream = this.imageService.getImageContent(image, principal);
return ResponseEntity.status(200) return ResponseEntity.status(200)
.contentType(MediaType.parseMediaType(image.getMimeType())) .contentType(MediaType.parseMediaType(image.getMimeType()))
@ -74,8 +73,7 @@ public class ImageController {
@PathVariable String username, @PathVariable String username,
@PathVariable String filename @PathVariable String filename
) { ) {
final User owner = this.userService.getUser(username); final Image image = this.imageService.getByUsernameAndFilename(username, filename, principal);
final Image image = this.imageService.getByOwnerAndFilename(owner, filename, principal);
return ResponseEntity.ok( return ResponseEntity.ok(
this.imageToViewConverter.convert(image, principal, false) this.imageToViewConverter.convert(image, principal, false)
); );
@ -116,8 +114,7 @@ public class ImageController {
@PathVariable String filename, @PathVariable String filename,
@RequestBody ImageUpdateBody body @RequestBody ImageUpdateBody body
) { ) {
final User owner = this.userService.getUser(username); final Image image = this.imageService.getByUsernameAndFilename(username, filename, principal);
final Image image = this.imageService.getByOwnerAndFilename(owner, filename, principal);
final Image updated = this.imageService.update( final Image updated = this.imageService.update(
image, image,
principal, principal,
@ -142,8 +139,7 @@ public class ImageController {
@PathVariable String username, @PathVariable String username,
@PathVariable String filename @PathVariable String filename
) throws IOException { ) throws IOException {
final User owner = this.userService.getUser(username); final Image image = this.imageService.getByUsernameAndFilename(username, filename, principal);
final Image image = this.imageService.getByOwnerAndFilename(owner, filename, principal);
this.imageService.deleteImage(image, principal); this.imageService.deleteImage(image, principal);
return ResponseEntity.noContent().build(); return ResponseEntity.noContent().build();
} }

View File

@ -17,7 +17,7 @@ public interface ImageService {
throws IOException, ImageException; throws IOException, ImageException;
Image getById(Integer id, @Nullable User viewer); Image getById(Integer id, @Nullable User viewer);
Image getByOwnerAndFilename(User owner, String filename, User viewer);
Image getByUsernameAndFilename(String username, String filename, User viewer); Image getByUsernameAndFilename(String username, String filename, User viewer);
InputStream getImageContent(Image image, @Nullable User viewer) throws IOException; InputStream getImageContent(Image image, @Nullable User viewer) throws IOException;

View File

@ -231,17 +231,6 @@ public class S3ImageService implements ImageService {
return this.imageRepository.findById(id).orElseThrow(() -> new NoSuchEntityWithIdException(Image.class, id)); return this.imageRepository.findById(id).orElseThrow(() -> new NoSuchEntityWithIdException(Image.class, id));
} }
@Override
@PostAuthorize("@imageSecurity.isViewableBy(returnObject, #viewer)")
public Image getByOwnerAndFilename(User owner, String filename, User viewer) {
return this.imageRepository.findByOwnerAndUserFilename(owner, filename)
.orElseThrow(() -> new NoSuchEntityWithUsernameAndFilenameException(
Image.class,
owner.getUsername(),
filename
));
}
@Override @Override
@PostAuthorize("@imageSecurity.isViewableBy(returnObject, #viewer)") @PostAuthorize("@imageSecurity.isViewableBy(returnObject, #viewer)")
public Image getByUsernameAndFilename(String username, String filename, User viewer) { public Image getByUsernameAndFilename(String username, String filename, User viewer) {