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:
@@ -39,6 +39,36 @@ public class ImageOptimizer {
|
||||
public record OptimizedImage(byte[] content, String contentType, String extension) {
|
||||
}
|
||||
|
||||
/** Перекодированная картинка вместе с итоговым размером в пикселях. */
|
||||
public record ReencodedImage(byte[] content, String contentType, String extension, int width, int height) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Всегда перекодирует картинку (с поворотом по EXIF и уменьшением до
|
||||
* MAX_DIMENSION), даже маленькую. В отличие от optimize, гарантирует, что
|
||||
* метаданные (геометка, модель телефона) не попадут в хранилище, — для фото,
|
||||
* которые пользователь отправляет в личный чат.
|
||||
*
|
||||
* @throws IOException если картинку не удалось прочитать
|
||||
*/
|
||||
public ReencodedImage reencode(byte[] source, String contentType) throws IOException {
|
||||
Thumbnails.Builder<?> builder = Thumbnails.of(new ByteArrayInputStream(source));
|
||||
builder = maxSide(source) > MAX_DIMENSION
|
||||
? builder.size(MAX_DIMENSION, MAX_DIMENSION)
|
||||
: builder.scale(1.0);
|
||||
BufferedImage image;
|
||||
try {
|
||||
image = builder.asBufferedImage();
|
||||
} catch (RuntimeException e) {
|
||||
throw new IOException("не удалось декодировать картинку", e);
|
||||
}
|
||||
|
||||
boolean keepAlpha = image.getColorModel().hasAlpha() && !"image/jpeg".equals(contentType);
|
||||
return keepAlpha
|
||||
? new ReencodedImage(writePng(image), "image/png", ".png", image.getWidth(), image.getHeight())
|
||||
: new ReencodedImage(writeJpeg(toRgb(image)), "image/jpeg", ".jpg", image.getWidth(), image.getHeight());
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void registerImageIoPlugins() {
|
||||
// Плагины TwelveMonkeys (WebP, CMYK-JPEG) лежат внутри fat jar —
|
||||
|
||||
Reference in New Issue
Block a user