use MediaType for validate

This commit is contained in:
Anatoly Karlov 2024-08-27 23:25:24 +07:00
parent 226be289e5
commit 29bc4e5800
2 changed files with 12 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.InvalidMimeTypeException;
@ -42,6 +43,14 @@ public class ErrorControllerAdvice {
.message(e.getMessage());
}
@ExceptionHandler({InvalidMediaTypeException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Object handleInvalidMediaTypeException(InvalidMediaTypeException e) {
log.warn("<- Res [400]: MimeType not valid", e);
return new GeneralError()
.message(e.getMessage());
}
@ExceptionHandler({ConstraintViolationException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Object handleConstraintViolationException(ConstraintViolationException e) {

View File

@ -7,10 +7,10 @@ import dev.vality.swag.disputes.model.CreateRequest;
import dev.vality.swag.disputes.model.CreateRequestAttachmentsInner;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MimeType;
@Service
@RequiredArgsConstructor
@ -25,7 +25,8 @@ public class ApiAttachmentsService {
public void createAttachments(CreateRequest req, Long disputeId) {
log.debug("Trying to save Attachments {}", disputeId);
for (CreateRequestAttachmentsInner attachment : req.getAttachments()) {
MimeType.valueOf(attachment.getMimeType());
// validate
MediaType.valueOf(attachment.getMimeType());
// http 500
var fileId = fileStorageService.saveFile(attachment.getData());
var fileMeta = new FileMeta(fileId, disputeId, attachment.getMimeType());