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:
@@ -9,6 +9,7 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import com.krylov.refound.config.MinioProperties;
|
||||
import com.krylov.refound.repository.ImageRepository;
|
||||
import com.krylov.refound.repository.MessageAttachmentRepository;
|
||||
import com.krylov.refound.repository.UserRepository;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
@@ -30,6 +31,8 @@ class OrphanFileCleanupServiceTest {
|
||||
S3Object.builder().key("used.jpg").lastModified(old).build(),
|
||||
S3Object.builder().key("avatars/legacy.jpg").lastModified(old).build(),
|
||||
S3Object.builder().key("orphan.jpg").lastModified(old).build(),
|
||||
S3Object.builder().key("chat/5/photo.jpg").lastModified(old).build(),
|
||||
S3Object.builder().key("chat/5/deleted-chat.jpg").lastModified(old).build(),
|
||||
S3Object.builder().key("fresh.jpg").lastModified(Instant.now()).build()).build();
|
||||
when(s3.listObjectsV2Paginator(any(ListObjectsV2Request.class)))
|
||||
.thenReturn(new ListObjectsV2Iterable(s3, ListObjectsV2Request.builder().build()));
|
||||
@@ -41,15 +44,20 @@ class OrphanFileCleanupServiceTest {
|
||||
UserRepository users = mock(UserRepository.class);
|
||||
// Старая запись аватара хранит полный URL
|
||||
when(users.findAllAvatarUrls()).thenReturn(List.of("http://minio:9010/refound-images/avatars/legacy.jpg?x=1"));
|
||||
MessageAttachmentRepository attachments = mock(MessageAttachmentRepository.class);
|
||||
when(attachments.findAllObjectKeys()).thenReturn(List.of("chat/5/photo.jpg"));
|
||||
FileStorageService storage = mock(FileStorageService.class);
|
||||
|
||||
new OrphanFileCleanupService(s3, properties, images, users, storage, true, Duration.ofHours(24), 100)
|
||||
new OrphanFileCleanupService(s3, properties, images, users, attachments, storage, true, Duration.ofHours(24), 100)
|
||||
.cleanupOrphanFiles();
|
||||
|
||||
verify(storage).deleteFile("orphan.jpg");
|
||||
verify(storage, never()).deleteFile("used.jpg");
|
||||
verify(storage, never()).deleteFile("avatars/legacy.jpg");
|
||||
verify(storage, never()).deleteFile("fresh.jpg");
|
||||
// Фото чата живёт, пока на него ссылается вложение; файл удалённого чата — сирота
|
||||
verify(storage, never()).deleteFile("chat/5/photo.jpg");
|
||||
verify(storage).deleteFile("chat/5/deleted-chat.jpg");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -58,7 +66,7 @@ class OrphanFileCleanupServiceTest {
|
||||
FileStorageService storage = mock(FileStorageService.class);
|
||||
|
||||
new OrphanFileCleanupService(s3, mock(MinioProperties.class), mock(ImageRepository.class),
|
||||
mock(UserRepository.class), storage, false, Duration.ofHours(24), 100).cleanupOrphanFiles();
|
||||
mock(UserRepository.class), mock(MessageAttachmentRepository.class), storage, false, Duration.ofHours(24), 100).cleanupOrphanFiles();
|
||||
|
||||
verify(storage, never()).deleteFile(anyString());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user