Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.stripe.exception.SignatureVerificationException;
import com.stripe.exception.StripeException;
import dev.samstevens.totp.exceptions.QrGenerationException;
import hng_java_boilerplate.profile.dto.response.ProfilePictureResponse;
import hng_java_boilerplate.squeeze.dto.ResponseMessageDto;
import hng_java_boilerplate.user.dto.response.ErrorResponse;
import io.jsonwebtoken.ExpiredJwtException;
Expand Down Expand Up @@ -126,4 +127,10 @@ private ErrorResponseDto setResponse(String message, String error, int statusCod
errorResponseDTO.setStatus_code(statusCode);
return errorResponseDTO;
}

@ExceptionHandler()
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponseDto handleProfilePictureUploadException(ProfilePictureUploadException ex) {
return setResponse("Image Not Uploaded",HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),HttpStatus.INTERNAL_SERVER_ERROR.value());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package hng_java_boilerplate.exception;

public class ProfilePictureUploadException extends RuntimeException {
public ProfilePictureUploadException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.amazonaws.services.s3.model.PutObjectRequest;
import hng_java_boilerplate.exception.BadRequestException;
import hng_java_boilerplate.exception.NotFoundException;
import hng_java_boilerplate.exception.ProfilePictureUploadException;
import hng_java_boilerplate.profile.dto.request.DeactivateUserRequest;
import hng_java_boilerplate.profile.dto.request.UpdateUserProfileDto;
import hng_java_boilerplate.profile.dto.response.*;
Expand Down Expand Up @@ -131,18 +132,15 @@ public ProfileResponse getUserProfile(String userId) {
@Override
public ResponseEntity<ProfilePictureResponse> uploadProfileImage(MultipartFile file) {
if (file.isEmpty() || !isValidImage(file)) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ProfilePictureResponse(false, "Invalid file type or missing image. Only JPG or JPEG formats are allowed.", null));
throw new BadRequestException("Invalid file type or missing image. Only JPG or JPEG formats are allowed");
}
try {
String filename = UUID.randomUUID() + "_" + file.getOriginalFilename();
uploadFileToS3(file, filename);

String fileUrl = amazonS3.getUrl(bucketName, filename).toString();
return ResponseEntity.ok(new ProfilePictureResponse(true, "Profile image uploaded successfully", fileUrl));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ProfilePictureResponse(false, "An error occurred while uploading your profile image. Please try again later.", null));
throw new ProfilePictureUploadException("An error occurred while uploading your profile image: " + e.getMessage());
}
}

Expand Down
Loading