Fix attachment contract and reconcile storage safely across API replicas

This commit is contained in:
2026-11-06 17:41:30 +03:00
parent 057143dd91
commit cc0ef713ed
82 changed files with 1828 additions and 1054 deletions
@@ -1,5 +1,6 @@
package ru.mtusi.messenger.controller;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -19,8 +20,8 @@ public class AttachmentController implements AttachmentsApi {
private final AttachmentService service;
@Override
public ResponseEntity<AttachmentDTO> uploadAttachment(UUID chatId, String filename, byte[] body, @AuthenticationPrincipal SessionPrincipal principal) {
return ResponseEntity.status(201).body(service.upload(chatId, principal.userId(), filename, body));
public ResponseEntity<AttachmentDTO> uploadAttachment(UUID chatId, String filename, HttpServletRequest body, String contentType, @AuthenticationPrincipal SessionPrincipal principal) {
return ResponseEntity.status(201).body(service.upload(chatId, principal.userId(), filename, body, contentType));
}
@Override
@@ -30,19 +31,6 @@ public class AttachmentController implements AttachmentsApi {
@Override
public ResponseEntity<byte[]> downloadAttachment(UUID attachmentId, String variant, SessionPrincipal principal) {
var downloaded = service.download(attachmentId, principal.userId(), variant);
byte[] bytes;
try (var in = downloaded.stream()) {
bytes = in.readAllBytes();
} catch (Exception e) {
throw new RuntimeException("Failed to read attachment stream", e);
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(downloaded.contentType()))
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + downloaded.filename() + "\"")
.body(bytes);
return service.downloadResponse(attachmentId, principal.userId(), variant);
}
}
}
@@ -1,5 +1,6 @@
package ru.mtusi.messenger.controller;
import jakarta.servlet.http.HttpServletRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
@@ -71,7 +72,8 @@ public interface AttachmentsApi {
@PathVariable("chat_id") UUID chatId,
@Parameter(name = "filename", required = true, in = ParameterIn.QUERY)
@Size(min = 1, max = 255) @RequestParam("filename") String filename,
@RequestBody byte[] body,
HttpServletRequest body,
@RequestHeader("Content-Type") String contentType,
@AuthenticationPrincipal SessionPrincipal principal
);
@@ -154,8 +156,7 @@ public interface AttachmentsApi {
)
@RequestMapping(
method = RequestMethod.GET,
value = "/api/v1/attachments/{attachment_id}/content",
produces = { "image/png", "image/jpeg", "image/webp" }
value = "/api/v1/attachments/{attachment_id}/content"
)
ResponseEntity<byte[]> downloadAttachment(
@Parameter(name = "attachment_id", required = true, in = ParameterIn.PATH)
@@ -26,6 +26,7 @@ import ru.mtusi.messenger.exception.*;
@RestControllerAdvice
@RequiredArgsConstructor
public class GlobalExceptionHandler {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(GlobalExceptionHandler.class);
private final RateLimitProperties limitProperties;
private final ApiErrorFactory errors;
@@ -113,6 +114,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorDTO> fallback(Exception e, HttpServletRequest req) {
log.error("Unexpected API failure type={}", e.getClass().getName());
return respond(HttpStatus.INTERNAL_SERVER_ERROR, ErrorErrorDTO.CodeEnum.INTERNAL_ERROR,
"Internal error", req);
}