all for lab4

This commit is contained in:
2026-11-08 18:17:48 +03:00
parent c1bd817abf
commit fc22dcc4a4
72 changed files with 12880 additions and 102 deletions
+6 -1
View File
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -98,6 +97,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.17</version>
</dependency>
</dependencies>
<build>
@@ -39,6 +39,10 @@ public class GlobalExceptionHandler {
ResponseEntity.BodyBuilder builder =
ResponseEntity.status(e.getStatus());
if (e.getStatus() == HttpStatus.TOO_MANY_REQUESTS) {
builder.header("Retry-After", "1");
}
if (e.getStatus() == HttpStatus.UNAUTHORIZED) {
String path = request.getRequestURI();
@@ -111,7 +115,7 @@ public class GlobalExceptionHandler {
return ResponseEntity
.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(build(
"service_unavailable",
"unavailable",
"Сервис временно недоступен"
));
}
@@ -123,7 +127,7 @@ public class GlobalExceptionHandler {
int status = e.getStatusCode().value();
String code = status == 503
? "service_unavailable"
? "unavailable"
: "request_failed";
String message = status == 503
@@ -0,0 +1,9 @@
package com.vnp.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@EnableScheduling
public class SchedulingConfig {
}
@@ -0,0 +1,25 @@
package com.vnp.demo.config;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UploadTimeoutConfig {
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory>
uploadTimeoutCustomizer() {
return factory -> factory.addConnectorCustomizers(connector -> {
if (connector.getProtocolHandler()
instanceof AbstractHttp11Protocol<?> protocol) {
protocol.setDisableUploadTimeout(false);
protocol.setConnectionUploadTimeout(15_000);
}
});
}
}
@@ -0,0 +1,100 @@
package com.vnp.demo.controllers;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.vnp.demo.BusinessException;
import com.vnp.demo.dto.AttachmentResponse;
import com.vnp.demo.service.AttachmentService;
import jakarta.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/api/v1")
public class AttachmentController {
private final AttachmentService attachmentService;
public AttachmentController(
AttachmentService attachmentService
) {
this.attachmentService = attachmentService;
}
@PostMapping("/chats/{chat_id}/attachments")
@ResponseStatus(HttpStatus.CREATED)
public AttachmentResponse upload(
@AuthenticationPrincipal UUID userId,
@PathVariable("chat_id") UUID chatId,
@RequestParam(value = "filename", required = false)
String filename,
HttpServletRequest request
) {
try {
return attachmentService.upload(
userId,
chatId,
filename,
request.getContentType(),
request.getContentLengthLong(),
request.getInputStream()
);
} catch (IOException e) {
throw BusinessException.invalid(
"Не удалось прочитать тело запроса"
);
}
}
@GetMapping("/attachments/{id}")
public AttachmentResponse metadata(
@AuthenticationPrincipal UUID userId,
@PathVariable("id") UUID id
) {
return attachmentService.getMetadata(userId, id);
}
@GetMapping("/attachments/{id}/content")
public ResponseEntity<byte[]> content(
@AuthenticationPrincipal UUID userId,
@PathVariable("id") UUID id,
@RequestParam(defaultValue = "original")
String variant
) {
AttachmentService.Download download =
attachmentService.download(userId, id, variant);
String disposition = ContentDisposition.attachment()
.filename(
download.filename(),
StandardCharsets.UTF_8
)
.build()
.toString();
return ResponseEntity.ok()
.contentType(
MediaType.parseMediaType(download.contentType())
)
.contentLength(download.bytes().length)
.header(HttpHeaders.CONTENT_DISPOSITION, disposition)
.header(HttpHeaders.CACHE_CONTROL, "private, no-store")
.header("X-Content-Type-Options", "nosniff")
.body(download.bytes());
}
}
@@ -8,6 +8,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.vnp.demo.service.S3StorageService;
import com.vnp.demo.BusinessException;
import com.vnp.demo.dto.HealthResponse;
@@ -18,8 +19,17 @@ public class HealthController {
private final JdbcTemplate jdbc;
private final StringRedisTemplate redis;
private final ApplicationAvailability availability;
public HealthController(JdbcTemplate jdbc, StringRedisTemplate redis, ApplicationAvailability availability) {
this.jdbc = jdbc; this.redis = redis; this.availability = availability;
private final S3StorageService storage;
public HealthController(
JdbcTemplate jdbc,
StringRedisTemplate redis,
ApplicationAvailability availability,
S3StorageService storage
) {
this.jdbc = jdbc;
this.redis = redis;
this.availability = availability;
this.storage = storage;
}
@GetMapping("/live")
public HealthResponse live() { return HealthResponse.ok(); }
@@ -30,9 +40,10 @@ public class HealthController {
if (!Integer.valueOf(1).equals(jdbc.queryForObject("SELECT 1", Integer.class))) throw new IllegalStateException();
String pong = redis.execute((org.springframework.data.redis.core.RedisCallback<String>) c -> c.ping());
if (!"PONG".equals(pong)) throw new IllegalStateException();
storage.checkReady();
return HealthResponse.ok();
} catch (Exception ex) {
throw new BusinessException("unavailable", "Сервис не готов", HttpStatus.SERVICE_UNAVAILABLE);
throw new BusinessException("service_unavailable", "Сервис не готов", HttpStatus.SERVICE_UNAVAILABLE);
}
}
}
@@ -0,0 +1,56 @@
package com.vnp.demo.dto;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.vnp.demo.models.Attachment;
public record AttachmentResponse(
UUID id,
@JsonProperty("chat_id")
UUID chatId,
@JsonProperty("owner_id")
UUID ownerId,
String filename,
@JsonProperty("content_type")
String contentType,
@JsonProperty("size_bytes")
long sizeBytes,
String sha256,
String status,
List<Object> variants,
@JsonProperty("error_code")
String errorCode,
@JsonProperty("created_at")
Instant createdAt,
@JsonProperty("updated_at")
Instant updatedAt
) {
public static AttachmentResponse from(Attachment attachment) {
return new AttachmentResponse(
attachment.id(),
attachment.chatId(),
attachment.ownerId(),
attachment.filename(),
attachment.contentType(),
attachment.sizeBytes(),
attachment.sha256(),
attachment.status(),
List.of(),
null,
attachment.createdAt(),
attachment.updatedAt()
);
}
}
@@ -1,8 +1,53 @@
package com.vnp.demo.dto;
import jakarta.validation.constraints.NotBlank;
import java.util.List;
import java.util.UUID;
public record MessageCreateRequest(
@NotBlank String text
) {
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
public class MessageCreateRequest {
private String text;
private List<UUID> attachmentIds = List.of();
public MessageCreateRequest() {
}
public MessageCreateRequest(String text) {
this.text = text;
}
public MessageCreateRequest(
String text,
List<UUID> attachmentIds
) {
this.text = text;
this.attachmentIds = attachmentIds;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String text() {
return text;
}
public List<UUID> attachmentIds() {
return attachmentIds;
}
@JsonSetter(
value = "attachment_ids",
nulls = Nulls.FAIL,
contentNulls = Nulls.FAIL
)
public void setAttachmentIds(List<UUID> attachmentIds) {
this.attachmentIds = attachmentIds;
}
}
@@ -1,21 +1,26 @@
package com.vnp.demo.dto;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
import com.fasterxml.jackson.annotation.JsonProperty;
public record MessageResponse(
UUID id,
@JsonProperty("chat_id")
UUID chat_id,
UUID id,
@JsonProperty("sender_id")
UUID sender_id,
@JsonProperty("chat_id")
UUID chatId,
String text,
@JsonProperty("sender_id")
UUID senderId,
@JsonProperty("created_at")
Instant createdAt
){}
String text,
@JsonProperty("created_at")
Instant createdAt,
@JsonProperty("attachment_ids")
List<UUID> attachmentIds
) {
}
@@ -0,0 +1,19 @@
package com.vnp.demo.models;
import java.time.Instant;
import java.util.UUID;
public record Attachment(
UUID id,
UUID chatId,
UUID ownerId,
String filename,
String contentType,
long sizeBytes,
String sha256,
String objectKey,
String status,
Instant createdAt,
Instant updatedAt
) {
}
@@ -1,14 +1,19 @@
package com.vnp.demo.models;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OrderColumn;
import jakarta.persistence.Table;
@Entity
@@ -32,13 +37,32 @@ public class Message {
@Column(name = "created_at", nullable = false)
private Instant createdAt = Instant.now();
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(
name = "message_attachments",
joinColumns = @JoinColumn(name = "message_id")
)
@Column(name = "attachment_id", nullable = false)
@OrderColumn(name = "position")
private List<UUID> attachmentIds = new ArrayList<>();
protected Message() {
}
public Message(Chat chat, User sender, String text) {
this(chat, sender, text, List.of());
}
public Message(
Chat chat,
User sender,
String text,
List<UUID> attachmentIds
) {
this.chat = chat;
this.sender = sender;
this.text = text;
this.attachmentIds = new ArrayList<>(attachmentIds);
}
public UUID getId() {
@@ -60,4 +84,8 @@ public class Message {
public Instant getCreatedAt() {
return createdAt;
}
public List<UUID> getAttachmentIds() {
return attachmentIds;
}
}
@@ -0,0 +1,122 @@
package com.vnp.demo.repositories;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Optional;
import java.util.UUID;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.vnp.demo.models.Attachment;
@Repository
public class AttachmentRepository {
private final JdbcTemplate jdbc;
public AttachmentRepository(JdbcTemplate jdbc) {
this.jdbc = jdbc;
}
public void save(Attachment attachment) {
jdbc.update(
"""
INSERT INTO attachments (
id,
chat_id,
owner_id,
filename,
content_type,
size_bytes,
sha256,
object_key,
status,
created_at,
updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
attachment.id(),
attachment.chatId(),
attachment.ownerId(),
attachment.filename(),
attachment.contentType(),
attachment.sizeBytes(),
attachment.sha256(),
attachment.objectKey(),
attachment.status(),
Timestamp.from(attachment.createdAt()),
Timestamp.from(attachment.updatedAt())
);
}
public Optional<Attachment> findById(UUID id) {
return jdbc.query(
"SELECT * FROM attachments WHERE id = ?",
this::mapRow,
id
).stream().findFirst();
}
private Attachment mapRow(ResultSet rs, int rowNum)
throws SQLException {
return new Attachment(
rs.getObject("id", UUID.class),
rs.getObject("chat_id", UUID.class),
rs.getObject("owner_id", UUID.class),
rs.getString("filename"),
rs.getString("content_type"),
rs.getLong("size_bytes"),
rs.getString("sha256"),
rs.getString("object_key"),
rs.getString("status"),
rs.getTimestamp("created_at").toInstant(),
rs.getTimestamp("updated_at").toInstant()
);
}
public boolean existsByObjectKey(String objectKey) {
Boolean exists = jdbc.queryForObject(
"""
SELECT EXISTS (
SELECT 1
FROM attachments
WHERE object_key = ?
)
""",
Boolean.class,
objectKey
);
return Boolean.TRUE.equals(exists);
}
public void lockObject(String objectKey) {
jdbc.query(
"""
SELECT pg_advisory_xact_lock(
hashtextextended(?, 0)
)
""",
(rs, rowNum) -> Boolean.TRUE,
"attachment-object:" + objectKey
);
}
public boolean tryLockObject(String objectKey) {
Boolean acquired = jdbc.queryForObject(
"""
SELECT pg_try_advisory_xact_lock(
hashtextextended(?, 0)
)
""",
Boolean.class,
"attachment-object:" + objectKey
);
return Boolean.TRUE.equals(acquired);
}
}
@@ -0,0 +1,119 @@
package com.vnp.demo.service;
import java.time.Instant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.vnp.demo.service.AttachmentPersistenceService.CleanupResult;
import io.minio.Result;
import io.minio.messages.Item;
@Component
@ConditionalOnProperty(
name = "app.cleanup.enabled",
havingValue = "true",
matchIfMissing = true
)
public class AttachmentCleanupJob {
private static final Logger log =
LoggerFactory.getLogger(AttachmentCleanupJob.class);
private final S3StorageService storage;
private final AttachmentPersistenceService persistence;
private final long graceSeconds;
public AttachmentCleanupJob(
S3StorageService storage,
AttachmentPersistenceService persistence,
@Value("${app.cleanup.grace-seconds:3600}")
long graceSeconds
) {
if (graceSeconds < 60) {
throw new IllegalArgumentException(
"Grace period должен быть не меньше 60 секунд"
);
}
this.storage = storage;
this.persistence = persistence;
this.graceSeconds = graceSeconds;
}
@Scheduled(
initialDelayString =
"${app.cleanup.initial-delay-ms:60000}",
fixedDelayString =
"${app.cleanup.interval-ms:300000}"
)
public void cleanup() {
Instant cutoff = Instant.now()
.minusSeconds(graceSeconds);
long scanned = 0;
long candidates = 0;
long deleted = 0;
try {
for (Result<Item> result : storage.listOriginals()) {
Item item = result.get();
if (item.isDir()) {
continue;
}
scanned++;
Instant lastModified = item.lastModified()
.toInstant();
if (!lastModified.isBefore(cutoff)) {
continue;
}
candidates++;
CleanupResult outcome =
persistence.removeIfOrphan(
item.objectName(),
cutoff
);
if (outcome == CleanupResult.DELETED) {
deleted++;
log.info(
"Attachment cleanup removed orphan: {}",
item.objectName()
);
}
}
log.info(
"Attachment cleanup completed: "
+ "scanned={}, candidates={}, deleted={}",
scanned,
candidates,
deleted
);
} catch (Exception e) {
// Прерываем текущий проход при ошибке БД или S3.
// Следующий запуск повторит проверку.
// Не выводим credentials или содержимое ответа S3.
log.warn(
"Attachment cleanup interrupted: "
+ "scanned={}, candidates={}, deleted={}, errorType={}",
scanned,
candidates,
deleted,
e.getClass().getSimpleName()
);
}
}
}
@@ -0,0 +1,94 @@
package com.vnp.demo.service;
import java.nio.file.Path;
import java.time.Instant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import com.vnp.demo.models.Attachment;
import com.vnp.demo.repositories.AttachmentRepository;
@Service
public class AttachmentPersistenceService {
private final AttachmentRepository attachmentRepository;
private final S3StorageService storage;
public AttachmentPersistenceService(
AttachmentRepository attachmentRepository,
S3StorageService storage
) {
this.attachmentRepository = attachmentRepository;
this.storage = storage;
}
@Transactional(
isolation = Isolation.READ_COMMITTED,
timeout = 60
)
public void save(
Attachment attachment,
Path temporaryFile
) {
attachmentRepository.lockObject(
attachment.objectKey()
);
storage.put(
attachment.objectKey(),
temporaryFile,
attachment.sizeBytes(),
attachment.contentType()
);
attachmentRepository.save(attachment);
}
@Transactional(
isolation = Isolation.READ_COMMITTED,
timeout = 30
)
public CleanupResult removeIfOrphan(
String objectKey,
Instant cutoff
) {
if (!objectKey.startsWith("originals/")) {
return CleanupResult.SKIPPED;
}
if (!attachmentRepository.tryLockObject(objectKey)) {
return CleanupResult.BUSY;
}
if (attachmentRepository.existsByObjectKey(objectKey)) {
return CleanupResult.REFERENCED;
}
Instant lastModified = storage.lastModifiedOrNull(
objectKey
);
if (lastModified == null) {
return CleanupResult.MISSING;
}
if (!lastModified.isBefore(cutoff)) {
return CleanupResult.RECENT;
}
storage.remove(objectKey);
return CleanupResult.DELETED;
}
public enum CleanupResult {
DELETED,
REFERENCED,
RECENT,
BUSY,
MISSING,
SKIPPED
}
}
@@ -0,0 +1,163 @@
package com.vnp.demo.service;
import java.io.InputStream;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.UUID;
import org.springframework.stereotype.Service;
import com.vnp.demo.BusinessException;
import com.vnp.demo.dto.AttachmentResponse;
import com.vnp.demo.models.Attachment;
import com.vnp.demo.repositories.AttachmentRepository;
import com.vnp.demo.repositories.ChatMemberRepository;
import com.vnp.demo.validation.ImageValidator;
import com.vnp.demo.validation.ImageValidator.ValidatedImage;
@Service
public class AttachmentService {
private final AttachmentRepository attachmentRepository;
private final ChatMemberRepository chatMemberRepository;
private final ImageValidator imageValidator;
private final S3StorageService storage;
private final UploadLimiter uploadLimiter;
private final AttachmentPersistenceService persistence;
public AttachmentService(
AttachmentRepository attachmentRepository,
ChatMemberRepository chatMemberRepository,
ImageValidator imageValidator,
S3StorageService storage,
UploadLimiter uploadLimiter,
AttachmentPersistenceService persistence
) {
this.attachmentRepository = attachmentRepository;
this.chatMemberRepository = chatMemberRepository;
this.imageValidator = imageValidator;
this.storage = storage;
this.uploadLimiter = uploadLimiter;
this.persistence = persistence;
}
public AttachmentResponse upload(
UUID userId,
UUID chatId,
String filename,
String contentType,
long contentLength,
InputStream input
) {
checkChatAccess(userId, chatId);
imageValidator.validateFilename(filename);
try (UploadLimiter.Permit permit = uploadLimiter.acquire();
ValidatedImage image = imageValidator.validate(
input,
contentLength,
contentType
)) {
UUID attachmentId = UUID.randomUUID();
String objectKey = "originals/"
+ chatId
+ "/"
+ attachmentId;
Instant now = Instant.now()
.truncatedTo(ChronoUnit.MICROS);
Attachment attachment = new Attachment(
attachmentId,
chatId,
userId,
filename,
image.contentType(),
image.sizeBytes(),
image.sha256(),
objectKey,
"ready",
now,
now
);
persistence.save(attachment, image.path());
return AttachmentResponse.from(attachment);
}
}
public AttachmentResponse getMetadata(
UUID userId,
UUID attachmentId
) {
return AttachmentResponse.from(
getAccessibleAttachment(userId, attachmentId)
);
}
public Download download(
UUID userId,
UUID attachmentId,
String variant
) {
Attachment attachment = getAccessibleAttachment(
userId,
attachmentId
);
if (!"ready".equals(attachment.status())) {
throw BusinessException.conflict(
"Вложение ещё не готово"
);
}
if (!"original".equals(variant)) {
throw BusinessException.notFound();
}
byte[] bytes = storage.get(
attachment.objectKey(),
attachment.sizeBytes(),
attachment.sha256()
);
return new Download(
attachment.filename(),
attachment.contentType(),
bytes
);
}
private Attachment getAccessibleAttachment(
UUID userId,
UUID attachmentId
) {
Attachment attachment = attachmentRepository
.findById(attachmentId)
.orElseThrow(BusinessException::notFound);
checkChatAccess(userId, attachment.chatId());
return attachment;
}
private void checkChatAccess(UUID userId, UUID chatId) {
boolean member = chatMemberRepository
.existsByIdChatIdAndIdUserId(chatId, userId);
if (!member) {
throw BusinessException.notFound();
}
}
public record Download(
String filename,
String contentType,
byte[] bytes
) {
}
}
@@ -1,7 +1,9 @@
package com.vnp.demo.service;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
@@ -12,9 +14,11 @@ import com.vnp.demo.CursorCodec;
import com.vnp.demo.dto.MessageCreateRequest;
import com.vnp.demo.dto.MessagePageResponse;
import com.vnp.demo.dto.MessageResponse;
import com.vnp.demo.models.Attachment;
import com.vnp.demo.models.Chat;
import com.vnp.demo.models.Message;
import com.vnp.demo.models.User;
import com.vnp.demo.repositories.AttachmentRepository;
import com.vnp.demo.repositories.ChatMemberRepository;
import com.vnp.demo.repositories.ChatRepository;
import com.vnp.demo.repositories.MessageRepository;
@@ -30,6 +34,7 @@ public class MessageService {
private final ChatRepository chatRepository;
private final ChatMemberRepository chatMemberRepository;
private final UserRepository userRepository;
private final AttachmentRepository attachmentRepository;
private final CursorCodec cursorCodec;
@PersistenceContext
@@ -40,12 +45,14 @@ public class MessageService {
ChatRepository chatRepository,
ChatMemberRepository chatMemberRepository,
UserRepository userRepository,
AttachmentRepository attachmentRepository,
CursorCodec cursorCodec
) {
this.messageRepository = messageRepository;
this.chatRepository = chatRepository;
this.chatMemberRepository = chatMemberRepository;
this.userRepository = userRepository;
this.attachmentRepository = attachmentRepository;
this.cursorCodec = cursorCodec;
}
@@ -53,43 +60,72 @@ public class MessageService {
public MessageResponse create(
UUID userId,
UUID chatId,
MessageCreateRequest request, String key
MessageCreateRequest request,
String key
) {
if (key == null || !key.matches("[A-Za-z0-9._:-]{1,128}")) {
throw BusinessException.invalid("Некорректный Idempotency-Key");
}
checkAccess(userId, chatId);
if (request == null
|| request.text() == null
|| request.text().isBlank()
|| request.text().codePointCount(
0,
request.text().length()
) > 4000) {
if (key == null
|| !key.matches("[A-Za-z0-9._:-]{1,128}")) {
throw BusinessException.invalid(
"Текст сообщения должен содержать от 1 до 4000 символов"
"Некорректный Idempotency-Key"
);
}
checkAccess(userId, chatId);
List<UUID> attachmentIds = validateRequest(request);
String attachmentSignature = attachmentIds.stream()
.map(UUID::toString)
.collect(Collectors.joining(","));
String scope = userId + ":" + chatId + ":" + key;
entityManager.createNativeQuery("SELECT 1 FROM pg_advisory_xact_lock(hashtextextended(:scope, 0))")
.setParameter("scope", scope).getResultList();
entityManager.createNativeQuery(
"""
SELECT 1
FROM pg_advisory_xact_lock(
hashtextextended(:scope, 0)
)
"""
).setParameter("scope", scope).getResultList();
@SuppressWarnings("unchecked")
List<Object[]> existing = entityManager.createNativeQuery(
"SELECT message_id, request_text FROM message_idempotency " +
"WHERE user_id = :user AND chat_id = :chat AND key = :key")
.setParameter("user", userId).setParameter("chat", chatId)
.setParameter("key", key).getResultList();
"""
SELECT message_id, request_text, request_attachment_ids
FROM message_idempotency
WHERE user_id = :user
AND chat_id = :chat
AND key = :key
"""
)
.setParameter("user", userId)
.setParameter("chat", chatId)
.setParameter("key", key)
.getResultList();
if (!existing.isEmpty()) {
Object[] row = existing.get(0);
if (!request.text().equals(row[1])) {
throw BusinessException.conflict("Ключ уже использован с другим сообщением");
boolean sameText = request.text().equals(row[1]);
boolean sameAttachments =
attachmentSignature.equals(row[2]);
if (!sameText || !sameAttachments) {
throw BusinessException.conflict(
"Ключ уже использован с другим сообщением"
);
}
return toResponse(messageRepository.findById((UUID) row[0]).orElseThrow());
Message original = messageRepository
.findById((UUID) row[0])
.orElseThrow(BusinessException::notFound);
return toResponse(original);
}
validateAttachments(chatId, attachmentIds);
Chat chat = chatRepository.findById(chatId)
.orElseThrow(BusinessException::notFound);
@@ -99,23 +135,110 @@ public class MessageService {
Message message = new Message(
chat,
sender,
request.text()
request.text(),
attachmentIds
);
Message saved = messageRepository.saveAndFlush(message);
// Перечитываем значения, фактически сохранённые PostgreSQL.
entityManager.refresh(saved);
entityManager.createNativeQuery(
"INSERT INTO message_idempotency(user_id, chat_id, key, request_text, message_id) " +
"VALUES (:user, :chat, :key, :text, :message)")
.setParameter("user", userId).setParameter("chat", chatId)
.setParameter("key", key).setParameter("text", request.text())
.setParameter("message", saved.getId()).executeUpdate();
"""
INSERT INTO message_idempotency (
user_id,
chat_id,
key,
request_text,
request_attachment_ids,
message_id
)
VALUES (
:user,
:chat,
:key,
:text,
:attachments,
:message
)
"""
)
.setParameter("user", userId)
.setParameter("chat", chatId)
.setParameter("key", key)
.setParameter("text", request.text())
.setParameter("attachments", attachmentSignature)
.setParameter("message", saved.getId())
.executeUpdate();
return toResponse(saved);
}
private List<UUID> validateRequest(
MessageCreateRequest request
) {
if (request == null || request.text() == null) {
throw BusinessException.invalid(
"Поле text обязательно"
);
}
if (request.text().codePointCount(
0,
request.text().length()
) > 4000) {
throw BusinessException.invalid(
"Текст не должен превышать 4000 символов"
);
}
List<UUID> attachmentIds = request.attachmentIds();
if (attachmentIds == null
|| attachmentIds.size() > 10
|| attachmentIds.stream().anyMatch(id -> id == null)) {
throw BusinessException.invalid(
"attachment_ids должен содержать от 0 до 10 UUID"
);
}
if (new HashSet<>(attachmentIds).size()
!= attachmentIds.size()) {
throw BusinessException.invalid(
"Повторяющиеся вложения недопустимы"
);
}
if (request.text().isBlank() && attachmentIds.isEmpty()) {
throw BusinessException.invalid(
"Сообщение должно содержать текст или вложение"
);
}
return List.copyOf(attachmentIds);
}
private void validateAttachments(
UUID chatId,
List<UUID> attachmentIds
) {
for (UUID attachmentId : attachmentIds) {
Attachment attachment = attachmentRepository
.findById(attachmentId)
.orElseThrow(BusinessException::notFound);
if (!chatId.equals(attachment.chatId())) {
throw BusinessException.notFound();
}
if (!"ready".equals(attachment.status())) {
throw BusinessException.conflict(
"Вложение не готово"
);
}
}
}
@Transactional(readOnly = true)
public MessagePageResponse list(
UUID userId,
@@ -140,14 +263,11 @@ public class MessageService {
List<Message> messages;
if (position == null) {
messages = messageRepository.findFirstPage(
chatId,
PageRequest.of(0, limit + 1)
);
} else {
messages = messageRepository.findNextPage(
chatId,
position.createdAt(),
@@ -169,7 +289,6 @@ public class MessageService {
String nextCursor = null;
if (hasNext && !page.isEmpty()) {
Message last = page.get(page.size() - 1);
nextCursor = cursorCodec.encodeMessage(
@@ -179,23 +298,14 @@ public class MessageService {
);
}
return new MessagePageResponse(
items,
nextCursor
);
return new MessagePageResponse(items, nextCursor);
}
private void checkAccess(
UUID userId,
UUID chatId
) {
boolean isMember = chatMemberRepository
.existsByIdChatIdAndIdUserId(
chatId,
userId
);
private void checkAccess(UUID userId, UUID chatId) {
boolean member = chatMemberRepository
.existsByIdChatIdAndIdUserId(chatId, userId);
if (!isMember) {
if (!member) {
throw BusinessException.notFound();
}
}
@@ -206,7 +316,8 @@ public class MessageService {
message.getChat().getId(),
message.getSender().getId(),
message.getText(),
message.getCreatedAt()
message.getCreatedAt(),
List.copyOf(message.getAttachmentIds())
);
}
}
@@ -0,0 +1,194 @@
package com.vnp.demo.service;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import com.vnp.demo.BusinessException;
import com.vnp.demo.validation.ImageValidator;
import io.minio.BucketExistsArgs;
import io.minio.GetObjectArgs;
import io.minio.ListObjectsArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.Result;
import io.minio.StatObjectArgs;
import io.minio.StatObjectResponse;
import io.minio.errors.ErrorResponseException;
import io.minio.messages.Item;
import okhttp3.OkHttpClient;
@Service
public class S3StorageService {
private final MinioClient client;
private final String bucket;
public S3StorageService(
@Value("${app.s3.endpoint}") String endpoint,
@Value("${app.s3.access-key}") String accessKey,
@Value("${app.s3.secret-key}") String secretKey,
@Value("${app.s3.bucket}") String bucket
) {
this.bucket = bucket;
OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(2, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.callTimeout(30, TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.build();
this.client = MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.region("us-east-1")
.httpClient(httpClient)
.build();
}
public void put(
String objectKey,
Path file,
long sizeBytes,
String contentType
) {
if (sizeBytes < 1
|| sizeBytes > ImageValidator.MAX_BYTES) {
throw new BusinessException(
"payload_too_large",
"Некорректный размер загружаемого файла",
HttpStatus.PAYLOAD_TOO_LARGE
);
}
try (InputStream input = Files.newInputStream(file)) {
client.putObject(
PutObjectArgs.builder()
.bucket(bucket)
.object(objectKey)
.contentType(contentType)
.stream(input, sizeBytes, ImageValidator.MAX_BYTES)
.build()
);
} catch (Exception e) {
throw unavailable();
}
}
public byte[] get(
String objectKey,
long expectedSize,
String expectedSha256
) {
if (expectedSize < 1
|| expectedSize > ImageValidator.MAX_BYTES) {
throw unavailable();
}
try (InputStream input = client.getObject(
GetObjectArgs.builder()
.bucket(bucket)
.object(objectKey)
.build()
)) {
byte[] bytes = input.readNBytes(
Math.toIntExact(expectedSize) + 1
);
if (bytes.length != expectedSize
|| !ImageValidator.sha256(bytes)
.equals(expectedSha256)) {
throw unavailable();
}
return bytes;
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
throw unavailable();
}
}
public Iterable<Result<Item>> listOriginals() {
return client.listObjects(
ListObjectsArgs.builder()
.bucket(bucket)
.prefix("originals/")
.recursive(true)
.build()
);
}
public Instant lastModifiedOrNull(String objectKey) {
try {
StatObjectResponse response = client.statObject(
StatObjectArgs.builder()
.bucket(bucket)
.object(objectKey)
.build()
);
return response.lastModified().toInstant();
} catch (ErrorResponseException e) {
String code = e.errorResponse().code();
if ("NoSuchKey".equals(code)
|| "NoSuchObject".equals(code)) {
return null;
}
throw unavailable();
} catch (Exception e) {
throw unavailable();
}
}
public void remove(String objectKey) {
try {
client.removeObject(
RemoveObjectArgs.builder()
.bucket(bucket)
.object(objectKey)
.build()
);
} catch (Exception e) {
throw unavailable();
}
}
public void checkReady() {
try {
boolean exists = client.bucketExists(
BucketExistsArgs.builder()
.bucket(bucket)
.build()
);
if (!exists) {
throw unavailable();
}
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
throw unavailable();
}
}
private static BusinessException unavailable() {
return new BusinessException(
"unavailable",
"Хранилище файлов временно недоступно",
HttpStatus.SERVICE_UNAVAILABLE
);
}
}
@@ -0,0 +1,58 @@
package com.vnp.demo.service;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import com.vnp.demo.BusinessException;
@Component
public class UploadLimiter {
private final Semaphore semaphore;
public UploadLimiter(
@Value("${app.uploads.max-concurrent:1}")
int maxConcurrent
) {
if (maxConcurrent < 1) {
throw new IllegalArgumentException(
"max-concurrent должен быть больше нуля"
);
}
this.semaphore = new Semaphore(maxConcurrent);
}
public Permit acquire() {
if (!semaphore.tryAcquire()) {
throw new BusinessException(
"rate_limited",
"Все обработчики загрузки заняты. Повторите запрос позже",
HttpStatus.TOO_MANY_REQUESTS
);
}
return new Permit(semaphore);
}
public static final class Permit implements AutoCloseable {
private final Semaphore semaphore;
private final AtomicBoolean released = new AtomicBoolean(false);
private Permit(Semaphore semaphore) {
this.semaphore = semaphore;
}
@Override
public void close() {
if (released.compareAndSet(false, true)) {
semaphore.release();
}
}
}
}
@@ -0,0 +1,481 @@
package com.vnp.demo.validation;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.HexFormat;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.vnp.demo.BusinessException;
@Component
public class ImageValidator {
public static final int MAX_BYTES = 10 * 1024 * 1024;
public static final long MAX_PIXELS = 40_000_000L;
private static final int BUFFER_SIZE = 64 * 1024;
private static final long MAX_RECEIVE_NANOS =
TimeUnit.SECONDS.toNanos(60);
private static final long TEMP_FILE_GRACE_SECONDS = 3600;
private static final Logger log =
LoggerFactory.getLogger(ImageValidator.class);
private static final Set<Path> ACTIVE_FILES =
ConcurrentHashMap.newKeySet();
private final Path tempDirectory;
public ImageValidator(
@Value("${app.uploads.temp-dir:/tmp/messenger-uploads}")
String tempDirectory
) {
this.tempDirectory = Path.of(tempDirectory)
.toAbsolutePath()
.normalize();
try {
Files.createDirectories(this.tempDirectory);
} catch (IOException e) {
throw new IllegalStateException(
"Не удалось создать каталог временных загрузок",
e
);
}
}
public record ValidatedImage(
Path path,
long sizeBytes,
String contentType,
String sha256
) implements AutoCloseable {
@Override
public void close() {
deleteTemporaryFile(path);
}
}
public void validateFilename(String filename) {
if (filename == null) {
throw BusinessException.invalid(
"Не указано имя файла"
);
}
int length = filename.codePointCount(
0,
filename.length()
);
if (length < 1 || length > 255) {
throw BusinessException.invalid(
"Имя файла должно содержать от 1 до 255 символов"
);
}
boolean unsafe = filename.codePoints().anyMatch(
codePoint -> codePoint == '/'
|| codePoint == '\\'
|| Character.isISOControl(codePoint)
);
if (unsafe) {
throw BusinessException.invalid(
"Имя файла содержит недопустимые символы"
);
}
}
public ValidatedImage validate(
InputStream input,
long contentLength,
String declaredContentType
) {
if (contentLength > MAX_BYTES) {
throw tooLarge(
"Размер файла превышает 10 MiB"
);
}
String declaredMime = normalizeMime(
declaredContentType
);
Path temporaryFile = createTemporaryFile();
boolean success = false;
try {
MessageDigest digest = newSha256();
long size = copyLimited(
input,
temporaryFile,
digest
);
if (size == 0) {
throw unsupported();
}
if (contentLength >= 0 && size != contentLength) {
throw BusinessException.invalid(
"Тело запроса получено не полностью"
);
}
String actualMime = validateImageContent(
temporaryFile
);
if (!actualMime.equals(declaredMime)) {
throw unsupported();
}
String checksum = HexFormat.of()
.formatHex(digest.digest());
ValidatedImage result = new ValidatedImage(
temporaryFile,
size,
actualMime,
checksum
);
success = true;
return result;
} finally {
if (!success) {
deleteTemporaryFile(temporaryFile);
}
}
}
private Path createTemporaryFile() {
try {
Path file = Files.createTempFile(
tempDirectory,
"upload-",
".tmp"
);
ACTIVE_FILES.add(file);
return file;
} catch (IOException e) {
throw temporaryStorageUnavailable();
}
}
private long copyLimited(
InputStream input,
Path destination,
MessageDigest digest
) {
byte[] buffer = new byte[BUFFER_SIZE];
long total = 0;
long started = System.nanoTime();
try (OutputStream output =
Files.newOutputStream(destination)) {
while (true) {
checkReceiveDeadline(started);
int requested = (int) Math.min(
buffer.length,
(long) MAX_BYTES - total + 1
);
int count;
try {
count = input.read(buffer, 0, requested);
} catch (IOException e) {
throw BusinessException.invalid(
"Передача файла прервана или превышено время ожидания"
);
}
checkReceiveDeadline(started);
if (count == -1) {
break;
}
if (count == 0) {
continue;
}
total += count;
if (total > MAX_BYTES) {
throw tooLarge(
"Размер файла превышает 10 MiB"
);
}
output.write(buffer, 0, count);
digest.update(buffer, 0, count);
}
return total;
} catch (IOException e) {
throw temporaryStorageUnavailable();
}
}
private static void checkReceiveDeadline(long started) {
if (System.nanoTime() - started >= MAX_RECEIVE_NANOS) {
throw BusinessException.invalid(
"Превышено допустимое время приёма файла"
);
}
}
private String normalizeMime(String value) {
if (value == null) {
throw unsupported();
}
try {
MediaType type = MediaType.parseMediaType(value);
if (type.getType().equalsIgnoreCase("image")
&& type.getSubtype().equalsIgnoreCase("png")) {
return "image/png";
}
if (type.getType().equalsIgnoreCase("image")
&& type.getSubtype().equalsIgnoreCase("jpeg")) {
return "image/jpeg";
}
} catch (IllegalArgumentException e) {
throw unsupported();
}
throw unsupported();
}
private String validateImageContent(Path file) {
try (FileImageInputStream imageInput =
new FileImageInputStream(file.toFile())) {
Iterator<ImageReader> readers =
ImageIO.getImageReaders(imageInput);
if (!readers.hasNext()) {
throw unsupported();
}
ImageReader reader = readers.next();
try {
reader.setInput(imageInput, true, true);
String format = reader.getFormatName();
String mime;
if ("png".equalsIgnoreCase(format)) {
mime = "image/png";
} else if ("jpeg".equalsIgnoreCase(format)
|| "jpg".equalsIgnoreCase(format)) {
mime = "image/jpeg";
} else {
throw unsupported();
}
int width = reader.getWidth(0);
int height = reader.getHeight(0);
if (width <= 0 || height <= 0) {
throw unsupported();
}
long pixels = (long) width * height;
if (pixels > MAX_PIXELS) {
throw tooLarge(
"Изображение содержит более 40 миллионов пикселей"
);
}
AtomicBoolean warning = new AtomicBoolean(false);
reader.addIIOReadWarningListener(
(source, message) -> warning.set(true)
);
BufferedImage image = reader.read(0);
if (image == null) {
throw unsupported();
}
try {
if (warning.get()) {
throw unsupported();
}
} finally {
image.flush();
}
return mime;
} finally {
reader.dispose();
}
} catch (BusinessException e) {
throw e;
} catch (IOException | RuntimeException e) {
throw unsupported();
}
}
@Scheduled(
initialDelay = 60_000,
fixedDelay = 300_000
)
public void cleanupTemporaryFiles() {
Instant cutoff = Instant.now()
.minusSeconds(TEMP_FILE_GRACE_SECONDS);
long deleted = 0;
try (DirectoryStream<Path> files =
Files.newDirectoryStream(
tempDirectory,
"upload-*.tmp"
)) {
for (Path file : files) {
if (ACTIVE_FILES.contains(file)) {
continue;
}
if (!Files.isRegularFile(
file,
LinkOption.NOFOLLOW_LINKS
)) {
continue;
}
try {
Instant modified = Files.getLastModifiedTime(
file,
LinkOption.NOFOLLOW_LINKS
).toInstant();
if (!modified.isBefore(cutoff)) {
continue;
}
if (Files.deleteIfExists(file)) {
deleted++;
}
} catch (IOException e) {
log.warn(
"Не удалось очистить временный файл {}",
file.getFileName(),
e
);
}
}
log.info(
"Temporary upload cleanup completed: deleted={}",
deleted
);
} catch (IOException e) {
log.warn(
"Не удалось просмотреть каталог временных загрузок",
e
);
}
}
private static MessageDigest newSha256() {
try {
return MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(
"SHA-256 недоступен",
e
);
}
}
public static String sha256(byte[] bytes) {
return HexFormat.of().formatHex(
newSha256().digest(bytes)
);
}
private static void deleteTemporaryFile(Path file) {
try {
Files.deleteIfExists(file);
} catch (IOException e) {
log.warn(
"Не удалось удалить временный файл {}",
file.getFileName(),
e
);
} finally {
ACTIVE_FILES.remove(file);
}
}
private static BusinessException unsupported() {
return new BusinessException(
"unsupported_media_type",
"Ожидается корректное изображение PNG или JPEG",
HttpStatus.UNSUPPORTED_MEDIA_TYPE
);
}
private static BusinessException tooLarge(String message) {
return new BusinessException(
"payload_too_large",
message,
HttpStatus.PAYLOAD_TOO_LARGE
);
}
private static BusinessException temporaryStorageUnavailable() {
return new BusinessException(
"unavailable",
"Временное хранилище загрузок недоступно",
HttpStatus.SERVICE_UNAVAILABLE
);
}
}
+16
View File
@@ -47,3 +47,19 @@ spring:
app:
session:
ttl-seconds: 86400
s3:
endpoint: ${S3_ENDPOINT:http://localhost:9000}
access-key: ${S3_ACCESS_KEY:}
secret-key: ${S3_SECRET_KEY:}
bucket: ${S3_BUCKET:messenger-attachments}
uploads:
max-concurrent: ${UPLOAD_MAX_CONCURRENT:1}
temp-dir: ${UPLOAD_TEMP_DIR:/tmp/messenger-uploads}
cleanup:
enabled: ${ATTACHMENT_CLEANUP_ENABLED:true}
grace-seconds: ${ATTACHMENT_CLEANUP_GRACE_SECONDS:3600}
interval-ms: ${ATTACHMENT_CLEANUP_INTERVAL_MS:300000}
initial-delay-ms: ${ATTACHMENT_CLEANUP_INITIAL_DELAY_MS:60000}
@@ -0,0 +1,67 @@
CREATE TABLE attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id),
owner_id UUID NOT NULL REFERENCES users(id),
filename TEXT NOT NULL,
content_type VARCHAR(32) NOT NULL,
size_bytes BIGINT NOT NULL,
sha256 CHAR(64) NOT NULL,
object_key TEXT NOT NULL UNIQUE,
status VARCHAR(16) NOT NULL DEFAULT 'ready',
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
CONSTRAINT chk_attachment_filename
CHECK (char_length(filename) BETWEEN 1 AND 255),
CONSTRAINT chk_attachment_content_type
CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT chk_attachment_size
CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT chk_attachment_sha256
CHECK (sha256 ~ '^[a-f0-9]{64}$'),
CONSTRAINT chk_attachment_status
CHECK (status = 'ready'),
CONSTRAINT fk_attachment_membership
FOREIGN KEY (chat_id, owner_id)
REFERENCES chat_members(chat_id, user_id)
);
CREATE INDEX idx_attachments_chat
ON attachments(chat_id, created_at, id);
CREATE TABLE message_attachments (
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
attachment_id UUID NOT NULL REFERENCES attachments(id),
position INTEGER NOT NULL,
PRIMARY KEY (message_id, position),
CONSTRAINT uq_message_attachment
UNIQUE (message_id, attachment_id),
CONSTRAINT chk_attachment_position
CHECK (position BETWEEN 0 AND 9)
);
CREATE INDEX idx_message_attachments_attachment
ON message_attachments(attachment_id);
ALTER TABLE messages
DROP CONSTRAINT chk_message_text;
ALTER TABLE messages
ADD CONSTRAINT chk_message_text
CHECK (char_length(text) BETWEEN 0 AND 4000);
-- Старые запросы не содержали вложений, поэтому для них значение пустое.
ALTER TABLE message_idempotency
ADD COLUMN request_attachment_ids TEXT NOT NULL DEFAULT '';