Add photo attachments to chat messages
- message_attachments table (cascade with messages); files stored under
chat/{chatId}/ in the images bucket
- POST /api/v1/chats/{chatId}/messages (multipart, up to 10 photos + text);
message is broadcast over WebSocket as before
- photos are always re-encoded so EXIF (geotag, device) never reaches storage
- presigned links (6h) only in participant-checked responses, refresh via
GET /api/v1/chats/{chatId}/attachments/{id}/url
- public /api/v1/files no longer serves or deletes chat/ keys
- chat deletion removes files; orphan cleanup keeps referenced attachments
- rate limit chat-photo: 60/hour
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package com.krylov.refound.service;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.startsWith;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.krylov.refound.enums.ErrorCode;
|
||||
import com.krylov.refound.exception.ApiException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
class ChatAttachmentServiceTest {
|
||||
|
||||
private static final byte[] JPEG = {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF, 0x00};
|
||||
|
||||
private FileStorageService storage;
|
||||
private ImageOptimizer optimizer;
|
||||
private ChatAttachmentService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
storage = mock(FileStorageService.class);
|
||||
optimizer = mock(ImageOptimizer.class);
|
||||
service = new ChatAttachmentService(storage, optimizer);
|
||||
when(optimizer.reencode(any(), anyString()))
|
||||
.thenReturn(new ImageOptimizer.ReencodedImage(new byte[] {1, 2}, "image/jpeg", ".jpg", 640, 480));
|
||||
}
|
||||
|
||||
private static MultipartFile photo(byte[] content) {
|
||||
return new MockMultipartFile("files", "IMG_0001.jpg", "image/jpeg", content);
|
||||
}
|
||||
|
||||
@Test
|
||||
void photosAreReencodedAndStoredUnderChatPrefix() {
|
||||
List<ChatAttachmentService.StoredPhoto> stored = service.storePhotos(7L, List.of(photo(JPEG)));
|
||||
|
||||
assertThat(stored).singleElement().satisfies(p -> {
|
||||
assertThat(p.objectKey()).startsWith("chat/7/").endsWith(".jpg");
|
||||
assertThat(p.width()).isEqualTo(640);
|
||||
assertThat(p.height()).isEqualTo(480);
|
||||
assertThat(p.sizeBytes()).isEqualTo(2);
|
||||
});
|
||||
verify(storage).putObject(startsWith("chat/7/"), eq(new byte[] {1, 2}), eq("image/jpeg"), eq("IMG_0001.jpg"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tooManyPhotosAreRejectedBeforeUpload() {
|
||||
List<MultipartFile> files = new ArrayList<>();
|
||||
for (int i = 0; i <= ChatAttachmentService.MAX_PHOTOS_PER_MESSAGE; i++) {
|
||||
files.add(photo(JPEG));
|
||||
}
|
||||
|
||||
assertThatThrownBy(() -> service.storePhotos(7L, files))
|
||||
.isInstanceOfSatisfying(ApiException.class, e -> assertThat(e.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST));
|
||||
verify(storage, never()).putObject(anyString(), any(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void notAnImageIsRejected() {
|
||||
assertThatThrownBy(() -> service.storePhotos(7L, List.of(photo("<svg/>".getBytes()))))
|
||||
.isInstanceOfSatisfying(ApiException.class, e -> assertThat(e.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST));
|
||||
verify(storage, never()).putObject(anyString(), any(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void alreadyUploadedPhotosAreRemovedWhenNextOneFails() {
|
||||
// Первое фото загружается, второе — нет: первое не должно остаться в бакете
|
||||
doNothing().doThrow(new ApiException(ErrorCode.INTERNAL_ERROR, "s3", HttpStatus.INTERNAL_SERVER_ERROR))
|
||||
.when(storage).putObject(anyString(), any(), anyString(), anyString());
|
||||
|
||||
assertThatThrownBy(() -> service.storePhotos(7L, List.of(photo(JPEG), photo(JPEG))))
|
||||
.isInstanceOf(ApiException.class);
|
||||
verify(storage).deleteFile(startsWith("chat/7/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void chatKeysAreRecognised() {
|
||||
assertThat(ChatAttachmentService.isChatKey("chat/7/a.jpg")).isTrue();
|
||||
assertThat(ChatAttachmentService.isChatKey("avatars/a.jpg")).isFalse();
|
||||
assertThat(ChatAttachmentService.isChatKey(null)).isFalse();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user