This commit is contained in:
2026-10-23 17:49:14 +03:00
parent 1fddc0e76c
commit f39147689a
23 changed files with 1351 additions and 10 deletions
+1
View File
@@ -39,6 +39,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'org.springframework.boot:spring-boot-starter-aspectj'
implementation 'org.springframework.boot:spring-boot-starter-jackson'
implementation("io.minio:minio:9.0.3")
runtimeOnly 'io.netty:netty-transport-native-epoll::linux-x86_64'
+23 -8
View File
@@ -159,11 +159,14 @@ services:
image: quay.io/minio/minio:RELEASE.2025-09-07T16-13-09Z
command: server /data --console-address ":9001"
environment:
MINIO_ROOT_USER: ${MINIO_ROOT_USER}
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
MINIO_ROOT_USER_FILE: /run/secrets/minio_root_user
MINIO_ROOT_PASSWORD_FILE: /run/secrets/minio_root_password
MINIO_BROWSER: "off"
volumes:
- minio-data:/data
secrets:
- minio_root_user
- minio_root_password
networks:
- messenger-net
healthcheck:
@@ -180,6 +183,10 @@ services:
condition: service_healthy
entrypoint: >
/bin/sh -c "
export MINIO_ROOT_USER=$$(cat /run/secrets/minio_root_user);
export MINIO_ROOT_PASSWORD=$$(cat /run/secrets/minio_root_password);
export S3_ACCESS_KEY_ID=$$(cat /run/secrets/s3_access_key_id);
export S3_SECRET_ACCESS_KEY=$$(cat /run/secrets/s3_secret_access_key);
until mc alias set local http://minio:9000 $$MINIO_ROOT_USER $$MINIO_ROOT_PASSWORD; do
echo 'Waiting for MinIO...' && sleep 2;
done;
@@ -190,13 +197,13 @@ services:
mc admin policy attach local app-uploads-policy --user $$S3_ACCESS_KEY_ID;
echo 'MinIO initialization completed';
"
environment:
MINIO_ROOT_USER: ${MINIO_ROOT_USER}
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID}
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY}
volumes:
- ./policy.json:/tmp/policy.json:ro
secrets:
- minio_root_user
- minio_root_password
- s3_access_key_id
- s3_secret_access_key
networks:
- messenger-net
restart: "no"
@@ -216,4 +223,12 @@ secrets:
postgres_user:
file: ./secrets/postgres_user.txt
postgres_password:
file: ./secrets/postgres_password.txt
file: ./secrets/postgres_password.txt
minio_root_user:
file: ./secrets/minio_root_user.txt
minio_root_password:
file: ./secrets/minio_root_password.txt
s3_access_key_id:
file: ./secrets/s3_access_key_id.txt
s3_secret_access_key:
file: ./secrets/s3_secret_access_key.txt
+21
View File
@@ -0,0 +1,21 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListBucket",
"s3:GetBucketLocation",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploadParts"
],
"Resource": [
"arn:aws:s3:::messenger-attachments",
"arn:aws:s3:::messenger-attachments/*"
]
}
]
}
@@ -0,0 +1,22 @@
package ru.mtusi.messenger.config;
import io.minio.MinioClient;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.mtusi.messenger.config.properties.S3Properties;
@Configuration
@RequiredArgsConstructor
public class MinioConfig {
private final S3Properties props;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(props.endpoint())
.region(props.region())
.credentials(props.accessKey(), props.secretKey())
.build();
}
}
@@ -0,0 +1,16 @@
package ru.mtusi.messenger.config.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;
@ConfigurationProperties(prefix = "s3")
public record S3Properties(
String endpoint,
String bucket,
@DefaultValue("us-east-1") String region,
String accessKey,
String secretKey,
@DefaultValue("true") boolean pathStyleAccess,
@DefaultValue("300") int presignExpirySeconds
) {
}
@@ -0,0 +1,13 @@
package ru.mtusi.messenger.config.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;
import java.util.List;
@ConfigurationProperties(prefix = "upload")
public record UploadProperties(
@DefaultValue("10485760") long maxBytes,
@DefaultValue("40000000") long maxPixels,
@DefaultValue({"image/png", "image/jpeg"}) List<String> allowedContentTypes
) {}
@@ -0,0 +1,48 @@
package ru.mtusi.messenger.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import ru.mtusi.messenger.dto.AttachmentDTO;
import ru.mtusi.messenger.security.SessionPrincipal;
import ru.mtusi.messenger.service.AttachmentService;
import java.util.UUID;
@RestController
@RequiredArgsConstructor
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));
}
@Override
public ResponseEntity<AttachmentDTO> getAttachment(UUID attachmentId, @AuthenticationPrincipal SessionPrincipal principal) {
return ResponseEntity.ok(service.getMetadata(attachmentId, principal.userId()));
}
@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);
}
}
@@ -0,0 +1,167 @@
package ru.mtusi.messenger.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Size;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.mtusi.messenger.dto.AttachmentDTO;
import ru.mtusi.messenger.dto.ErrorDTO;
import ru.mtusi.messenger.security.SessionPrincipal;
import java.util.UUID;
@Validated
@Tag(name = "Attachments", description = "Загрузка и скачивание вложений (PNG/JPEG) в чатах.")
@RequestMapping("${openapi.mTUSIMessenger2.base-path:}")
public interface AttachmentsApi {
@Operation(
operationId = "uploadAttachment",
summary = "Принять один PNG/JPEG, до 10 MiB",
tags = { "Attachments" },
responses = {
@ApiResponse(responseCode = "201", description = "Успех", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = AttachmentDTO.class))
}),
@ApiResponse(responseCode = "400", description = "Неверный запрос", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "401", description = "Нет действительной аутентификации", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "404", description = "Объект отсутствует или недоступен", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "413", description = "Превышен лимит", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "415", description = "Неподдерживаемый тип", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "429", description = "Ограничение частоты запросов", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "503", description = "Зависимость недоступна", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "500", description = "Неожиданная ошибка без внутренних деталей", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
})
},
security = { @SecurityRequirement(name = "bearerAuth") }
)
@RequestMapping(
method = RequestMethod.POST,
value = "/api/v1/chats/{chat_id}/attachments",
consumes = { MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE }
)
ResponseEntity<AttachmentDTO> uploadAttachment(
@Parameter(name = "chat_id", required = true, in = ParameterIn.PATH)
@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,
@AuthenticationPrincipal SessionPrincipal principal
);
@Operation(
operationId = "getAttachment",
summary = "Метаданные вложения, доступ участникам чата",
tags = { "Attachments" },
responses = {
@ApiResponse(responseCode = "200", description = "Успех", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = AttachmentDTO.class))
}),
@ApiResponse(responseCode = "400", description = "Неверный запрос", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "401", description = "Нет действительной аутентификации", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "404", description = "Объект отсутствует или недоступен", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "409", description = "Конфликт", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "429", description = "Ограничение частоты запросов", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "503", description = "Зависимость недоступна", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "500", description = "Неожиданная ошибка без внутренних деталей", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
})
},
security = { @SecurityRequirement(name = "bearerAuth") }
)
@RequestMapping(
method = RequestMethod.GET,
value = "/api/v1/attachments/{attachment_id}",
produces = { MediaType.APPLICATION_JSON_VALUE }
)
ResponseEntity<AttachmentDTO> getAttachment(
@Parameter(name = "attachment_id", required = true, in = ParameterIn.PATH)
@PathVariable("attachment_id") UUID attachmentId,
@AuthenticationPrincipal SessionPrincipal principal
);
@Operation(
operationId = "downloadAttachment",
summary = "Скачать байты через авторизованный API",
tags = { "Attachments" },
responses = {
@ApiResponse(responseCode = "200", description = "Успех", content = {
@Content(mediaType = "image/png", schema = @Schema(type = "string", format = "binary")),
@Content(mediaType = "image/jpeg", schema = @Schema(type = "string", format = "binary")),
@Content(mediaType = "image/webp", schema = @Schema(type = "string", format = "binary"))
}),
@ApiResponse(responseCode = "400", description = "Неверный запрос", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "401", description = "Нет действительной аутентификации", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "404", description = "Объект отсутствует или недоступен", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "409", description = "Конфликт", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "429", description = "Ограничение частоты запросов", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "503", description = "Зависимость недоступна", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
}),
@ApiResponse(responseCode = "500", description = "Неожиданная ошибка без внутренних деталей", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorDTO.class))
})
},
security = { @SecurityRequirement(name = "bearerAuth") }
)
@RequestMapping(
method = RequestMethod.GET,
value = "/api/v1/attachments/{attachment_id}/content",
produces = { "image/png", "image/jpeg", "image/webp" }
)
ResponseEntity<byte[]> downloadAttachment(
@Parameter(name = "attachment_id", required = true, in = ParameterIn.PATH)
@PathVariable("attachment_id") UUID attachmentId,
@Parameter(name = "variant", in = ParameterIn.QUERY)
@RequestParam(value = "variant", required = false, defaultValue = "original") String variant,
@AuthenticationPrincipal SessionPrincipal principal
);
}
@@ -147,6 +147,18 @@ public class GlobalExceptionHandler {
"Missing header: " + e.getHeaderName(), req);
}
@ExceptionHandler(UnsupportedMediaTypeException.class)
public ResponseEntity<ErrorDTO> unsupportedMedia(UnsupportedMediaTypeException e, HttpServletRequest req) {
return respond(HttpStatus.UNSUPPORTED_MEDIA_TYPE,
ErrorErrorDTO.CodeEnum.UNSUPPORTED_MEDIA_TYPE, e.getMessage(), req);
}
@ExceptionHandler(StorageUnavailableException.class)
public ResponseEntity<ErrorDTO> storageUnavailable(StorageUnavailableException e, HttpServletRequest req) {
return respond(HttpStatus.SERVICE_UNAVAILABLE,
ErrorErrorDTO.CodeEnum.UNAVAILABLE, e.getMessage(), req);
}
private ResponseEntity<ErrorDTO> respond(HttpStatus status,
ErrorErrorDTO.CodeEnum code,
String message,
@@ -0,0 +1,393 @@
package ru.mtusi.messenger.dto;
import java.util.Objects;
import java.time.Instant;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.UUID;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.*;
import jakarta.annotation.Generated;
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.10.0")
public class AttachmentDTO {
private UUID id;
private UUID chatId;
private UUID ownerId;
private String filename;
/**
* Gets or Sets contentType
*/
public enum ContentTypeEnum {
IMAGE_PNG("image/png"),
IMAGE_JPEG("image/jpeg");
private final String value;
ContentTypeEnum(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static ContentTypeEnum fromValue(String value) {
for (ContentTypeEnum b : ContentTypeEnum.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}
private ContentTypeEnum contentType;
private Long sizeBytes;
private String sha256;
/**
* Gets or Sets status
*/
public enum StatusEnum {
READY("ready");
private final String value;
StatusEnum(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static StatusEnum fromValue(String value) {
for (StatusEnum b : StatusEnum.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}
private StatusEnum status;
@Valid
private List<@Valid VariantDTO> variants = new ArrayList<>();
private String errorCode;
private Instant createdAt;
private Instant updatedAt;
public AttachmentDTO() {
super();
}
/**
* Constructor with only required parameters
*/
public AttachmentDTO(UUID id, UUID chatId, UUID ownerId, String filename,
ContentTypeEnum contentType, Long sizeBytes, String sha256,
StatusEnum status, List<@Valid VariantDTO> variants,
String errorCode, Instant createdAt, Instant updatedAt) {
this.id = id;
this.chatId = chatId;
this.ownerId = ownerId;
this.filename = filename;
this.contentType = contentType;
this.sizeBytes = sizeBytes;
this.sha256 = sha256;
this.status = status;
this.variants = variants;
this.errorCode = errorCode;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
public AttachmentDTO id(UUID id) {
this.id = id;
return this;
}
@Valid
@Schema(name = "id", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("id")
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public AttachmentDTO chatId(UUID chatId) {
this.chatId = chatId;
return this;
}
@Valid
@Schema(name = "chat_id", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("chat_id")
public UUID getChatId() {
return chatId;
}
public void setChatId(UUID chatId) {
this.chatId = chatId;
}
public AttachmentDTO ownerId(UUID ownerId) {
this.ownerId = ownerId;
return this;
}
@Valid
@Schema(name = "owner_id", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("owner_id")
public UUID getOwnerId() {
return ownerId;
}
public void setOwnerId(UUID ownerId) {
this.ownerId = ownerId;
}
public AttachmentDTO filename(String filename) {
this.filename = filename;
return this;
}
@NotNull
@Size(min = 1, max = 255)
@Schema(name = "filename", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("filename")
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public AttachmentDTO contentType(ContentTypeEnum contentType) {
this.contentType = contentType;
return this;
}
@NotNull
@Schema(name = "content_type", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("content_type")
public ContentTypeEnum getContentType() {
return contentType;
}
public void setContentType(ContentTypeEnum contentType) {
this.contentType = contentType;
}
public AttachmentDTO sizeBytes(Long sizeBytes) {
this.sizeBytes = sizeBytes;
return this;
}
@NotNull
@Min(1L) @Max(10485760L)
@Schema(name = "size_bytes", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("size_bytes")
public Long getSizeBytes() {
return sizeBytes;
}
public void setSizeBytes(Long sizeBytes) {
this.sizeBytes = sizeBytes;
}
public AttachmentDTO sha256(String sha256) {
this.sha256 = sha256;
return this;
}
@NotNull
@Pattern(regexp = "^[a-f0-9]{64}$")
@Schema(name = "sha256", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("sha256")
public String getSha256() {
return sha256;
}
public void setSha256(String sha256) {
this.sha256 = sha256;
}
public AttachmentDTO status(StatusEnum status) {
this.status = status;
return this;
}
@NotNull
@Schema(name = "status", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("status")
public StatusEnum getStatus() {
return status;
}
public void setStatus(StatusEnum status) {
this.status = status;
}
public AttachmentDTO variants(List<@Valid VariantDTO> variants) {
this.variants = variants;
return this;
}
public AttachmentDTO addVariantsItem(VariantDTO variantsItem) {
if (this.variants == null) {
this.variants = new ArrayList<>();
}
this.variants.add(variantsItem);
return this;
}
@NotNull @Valid
@Schema(name = "variants", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("variants")
public List<@Valid VariantDTO> getVariants() {
return variants;
}
public void setVariants(List<@Valid VariantDTO> variants) {
this.variants = variants;
}
public AttachmentDTO errorCode(String errorCode) {
this.errorCode = errorCode;
return this;
}
@Schema(name = "error_code", requiredMode = Schema.RequiredMode.REQUIRED, nullable = true)
@JsonProperty("error_code")
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public AttachmentDTO createdAt(Instant createdAt) {
this.createdAt = createdAt;
return this;
}
@NotNull
@Schema(name = "created_at", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("created_at")
public Instant getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}
public AttachmentDTO updatedAt(Instant updatedAt) {
this.updatedAt = updatedAt;
return this;
}
@NotNull
@Schema(name = "updated_at", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("updated_at")
public Instant getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Instant updatedAt) {
this.updatedAt = updatedAt;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AttachmentDTO attachment = (AttachmentDTO) o;
return Objects.equals(this.id, attachment.id) &&
Objects.equals(this.chatId, attachment.chatId) &&
Objects.equals(this.ownerId, attachment.ownerId) &&
Objects.equals(this.filename, attachment.filename) &&
Objects.equals(this.contentType, attachment.contentType) &&
Objects.equals(this.sizeBytes, attachment.sizeBytes) &&
Objects.equals(this.sha256, attachment.sha256) &&
Objects.equals(this.status, attachment.status) &&
Objects.equals(this.variants, attachment.variants) &&
Objects.equals(this.errorCode, attachment.errorCode) &&
Objects.equals(this.createdAt, attachment.createdAt) &&
Objects.equals(this.updatedAt, attachment.updatedAt);
}
@Override
public int hashCode() {
return Objects.hash(id, chatId, ownerId, filename, contentType, sizeBytes,
sha256, status, variants, errorCode, createdAt, updatedAt);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class AttachmentDTO {\n");
sb.append(" id: ").append(toIndentedString(id)).append("\n");
sb.append(" chatId: ").append(toIndentedString(chatId)).append("\n");
sb.append(" ownerId: ").append(toIndentedString(ownerId)).append("\n");
sb.append(" filename: ").append(toIndentedString(filename)).append("\n");
sb.append(" contentType: ").append(toIndentedString(contentType)).append("\n");
sb.append(" sizeBytes: ").append(toIndentedString(sizeBytes)).append("\n");
sb.append(" sha256: ").append(toIndentedString(sha256)).append("\n");
sb.append(" status: ").append(toIndentedString(status)).append("\n");
sb.append(" variants: ").append(toIndentedString(variants)).append("\n");
sb.append(" errorCode: ").append(toIndentedString(errorCode)).append("\n");
sb.append(" createdAt: ").append(toIndentedString(createdAt)).append("\n");
sb.append(" updatedAt: ").append(toIndentedString(updatedAt)).append("\n");
sb.append("}");
return sb.toString();
}
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
@@ -0,0 +1,257 @@
package ru.mtusi.messenger.dto;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import jakarta.validation.constraints.*;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.*;
import jakarta.annotation.Generated;
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.10.0")
public class VariantDTO {
/**
* Gets or Sets name
*/
public enum NameEnum {
THUMBNAIL("thumbnail"),
CROP("crop"),
OPTIMIZED("optimized");
private final String value;
NameEnum(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static NameEnum fromValue(String value) {
for (NameEnum b : NameEnum.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}
private NameEnum name;
/**
* Gets or Sets contentType
*/
public enum ContentTypeEnum {
IMAGE_JPEG("image/jpeg"),
IMAGE_PNG("image/png"),
IMAGE_WEBP("image/webp");
private final String value;
ContentTypeEnum(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static ContentTypeEnum fromValue(String value) {
for (ContentTypeEnum b : ContentTypeEnum.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}
private ContentTypeEnum contentType;
private Long sizeBytes;
private Integer width;
private Integer height;
private String sha256;
public VariantDTO() {
super();
}
/**
* Constructor with only required parameters
*/
public VariantDTO(NameEnum name, ContentTypeEnum contentType, Long sizeBytes,
Integer width, Integer height, String sha256) {
this.name = name;
this.contentType = contentType;
this.sizeBytes = sizeBytes;
this.width = width;
this.height = height;
this.sha256 = sha256;
}
public VariantDTO name(NameEnum name) {
this.name = name;
return this;
}
@NotNull
@Schema(name = "name", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("name")
public NameEnum getName() {
return name;
}
public void setName(NameEnum name) {
this.name = name;
}
public VariantDTO contentType(ContentTypeEnum contentType) {
this.contentType = contentType;
return this;
}
@NotNull
@Schema(name = "content_type", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("content_type")
public ContentTypeEnum getContentType() {
return contentType;
}
public void setContentType(ContentTypeEnum contentType) {
this.contentType = contentType;
}
public VariantDTO sizeBytes(Long sizeBytes) {
this.sizeBytes = sizeBytes;
return this;
}
@NotNull
@Min(1L)
@Schema(name = "size_bytes", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("size_bytes")
public Long getSizeBytes() {
return sizeBytes;
}
public void setSizeBytes(Long sizeBytes) {
this.sizeBytes = sizeBytes;
}
public VariantDTO width(Integer width) {
this.width = width;
return this;
}
@NotNull
@Min(1)
@Schema(name = "width", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("width")
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public VariantDTO height(Integer height) {
this.height = height;
return this;
}
@NotNull
@Min(1)
@Schema(name = "height", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("height")
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public VariantDTO sha256(String sha256) {
this.sha256 = sha256;
return this;
}
@NotNull
@Pattern(regexp = "^[a-f0-9]{64}$")
@Schema(name = "sha256", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("sha256")
public String getSha256() {
return sha256;
}
public void setSha256(String sha256) {
this.sha256 = sha256;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
VariantDTO variant = (VariantDTO) o;
return Objects.equals(this.name, variant.name) &&
Objects.equals(this.contentType, variant.contentType) &&
Objects.equals(this.sizeBytes, variant.sizeBytes) &&
Objects.equals(this.width, variant.width) &&
Objects.equals(this.height, variant.height) &&
Objects.equals(this.sha256, variant.sha256);
}
@Override
public int hashCode() {
return Objects.hash(name, contentType, sizeBytes, width, height, sha256);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class VariantDTO {\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" contentType: ").append(toIndentedString(contentType)).append("\n");
sb.append(" sizeBytes: ").append(toIndentedString(sizeBytes)).append("\n");
sb.append(" width: ").append(toIndentedString(width)).append("\n");
sb.append(" height: ").append(toIndentedString(height)).append("\n");
sb.append(" sha256: ").append(toIndentedString(sha256)).append("\n");
sb.append("}");
return sb.toString();
}
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
@@ -0,0 +1,5 @@
package ru.mtusi.messenger.exception;
public class StorageUnavailableException extends RuntimeException {
public StorageUnavailableException(String message, Throwable cause) { super(message, cause); }
}
@@ -0,0 +1,5 @@
package ru.mtusi.messenger.exception;
public class UnsupportedMediaTypeException extends RuntimeException {
public UnsupportedMediaTypeException(String message) { super(message); }
}
@@ -0,0 +1,42 @@
package ru.mtusi.messenger.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import ru.mtusi.messenger.dto.AttachmentDTO;
import ru.mtusi.messenger.model.Attachment;
import ru.mtusi.messenger.model.AttachmentStatus;
import ru.mtusi.messenger.model.Chat;
import ru.mtusi.messenger.model.User;
@Mapper(componentModel = "spring")
public interface AttachmentMapper {
@Mapping(target = "chatId", source = "chat.id")
@Mapping(target = "ownerId", source = "owner.id")
@Mapping(target = "sha256", source = "checksum")
@Mapping(target = "status", source = "status", qualifiedByName = "mapStatus")
@Mapping(target = "contentType", source = "contentType")
@Mapping(target = "variants", expression = "java(Collections.emptyList())")
@Mapping(target = "errorCode", constant = "null")
AttachmentDTO toDto(Attachment entity);
@Named("mapStatus")
default AttachmentDTO.StatusEnum mapStatus(AttachmentStatus status) {
if (status == AttachmentStatus.READY) {
return AttachmentDTO.StatusEnum.READY;
}
return null;
}
@Mapping(target = "id", ignore = true)
@Mapping(target = "createdAt", ignore = true)
@Mapping(target = "updatedAt", ignore = true)
@Mapping(target = "status", constant = "READY")
Attachment toEntity(String objectKey,
String filename,
String contentType,
Long sizeBytes,
String checksum,
Chat chat,
User owner);
}
@@ -0,0 +1,63 @@
package ru.mtusi.messenger.model;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.UUID;
@Entity
@Table(name = "attachments")
@Getter @Setter
public class Attachment {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
@Column(name = "object_key", nullable = false, unique = true, length = 512)
private String objectKey;
@Column(nullable = false, length = 255)
private String filename;
@Column(name = "content_type", nullable = false, length = 100)
private String contentType;
@Column(name = "size_bytes", nullable = false)
private Long sizeBytes;
@Column(nullable = false, length = 64)
private String checksum;
@Enumerated(EnumType.STRING)
@Column(nullable = false, length = 20)
private AttachmentStatus status;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "chat_id", nullable = false)
private Chat chat;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "owner_id", nullable = false)
private User owner;
@Column(name = "created_at", nullable = false)
private Instant createdAt;
@Column(name = "updated_at", nullable = false)
private Instant updatedAt;
@PrePersist
private void onCreate() {
Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS);
this.createdAt = now;
this.updatedAt = now;
}
@PreUpdate
private void onUpdate() {
this.updatedAt = Instant.now().truncatedTo(ChronoUnit.MILLIS);
}
}
@@ -0,0 +1,7 @@
package ru.mtusi.messenger.model;
public enum AttachmentStatus {
UPLOADING,
READY,
BROKEN
}
@@ -0,0 +1,10 @@
package ru.mtusi.messenger.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.mtusi.messenger.model.Attachment;
import java.util.Optional;
import java.util.UUID;
public interface AttachmentRepository extends JpaRepository<Attachment, UUID> {
Optional<Attachment> findByObjectKey(String objectKey);
}
@@ -36,4 +36,6 @@ public interface ChatRepository extends JpaRepository<Chat, UUID> {
@Param("cursorCreated") Instant cursorCreated,
@Param("cursorId") UUID cursorId,
Pageable pageable);
boolean existsByIdAndMembersId(UUID chatId, UUID userId);
}
@@ -0,0 +1,113 @@
package ru.mtusi.messenger.service;
import io.minio.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.mtusi.messenger.config.properties.S3Properties;
import ru.mtusi.messenger.dto.AttachmentDTO;
import ru.mtusi.messenger.mapper.AttachmentMapper;
import ru.mtusi.messenger.model.Attachment;
import ru.mtusi.messenger.exception.*;
import ru.mtusi.messenger.model.Chat;
import ru.mtusi.messenger.model.User;
import ru.mtusi.messenger.repository.AttachmentRepository;
import ru.mtusi.messenger.validation.AttachmentValidator;
import java.security.MessageDigest;
import java.util.UUID;
@Service
@RequiredArgsConstructor
@Slf4j
public class AttachmentService {
private final UserService userService;
private final ChatService chatService;
private final MinioClient minio;
private final AttachmentRepository attachmentRepository;
private final S3Properties s3Props;
private final AttachmentValidator validator;
private final AttachmentMapper attachmentMapper;
@Transactional
public AttachmentDTO upload(UUID chatId, UUID caller, String filename, byte[] body) {
Chat chat = chatService.getChat(caller, chatId);
String contentType = validator.validate(body, filename);
String safeFilename = validator.sanitizeFilename(filename);
String objectKey = UUID.randomUUID().toString();
String checksum = uploadToS3AndCalculateChecksum(body, objectKey, contentType);
User owner = userService.getReferenceUserById(caller);
Attachment attachment = attachmentMapper.toEntity(objectKey, safeFilename, contentType, (long) body.length, checksum , chat , owner);
attachmentRepository.save(attachment);
return attachmentMapper.toDto(attachment);
}
private String uploadToS3AndCalculateChecksum(byte[] body, String objectKey, String contentType) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
try (var in = new java.security.DigestInputStream(new java.io.ByteArrayInputStream(body), digest)) {
minio.putObject(io.minio.PutObjectArgs.builder()
.bucket(s3Props.bucket())
.object(objectKey)
.stream(in, (long) body.length, -1L)
.contentType(contentType)
.build());
}
return java.util.HexFormat.of().formatHex(digest.digest());
} catch (Exception e) {
log.error("S3 upload failed for objectKey: {}", objectKey, e);
throw new StorageUnavailableException("S3 upload failed", e);
}
}
public AttachmentDTO getMetadata(UUID attachmentId, UUID userId) {
Attachment attachment = attachmentRepository.findById(attachmentId)
.orElseThrow(() -> new NotFoundException("Attachment not found"));
assertMember(attachment.getChat().getId(), userId);
return attachmentMapper.toDto(attachment);
}
public DownloadedAttachment download(UUID attachmentId, UUID userId, String variant) {
Attachment attachment = attachmentRepository.findById(attachmentId)
.orElseThrow(() -> new NotFoundException("Attachment not found"));
assertMember(attachment.getChat().getId(), userId);
if (!"original".equals(variant)) {
throw new NotFoundException("Variant not found");
}
try {
var stream = minio.getObject(GetObjectArgs.builder()
.bucket(s3Props.bucket())
.object(attachment.getObjectKey())
.build());
return new DownloadedAttachment(stream, attachment.getContentType(), attachment.getFilename());
} catch (Exception e) {
throw translateS3Exception(e, attachmentId, attachment.getObjectKey());
}
}
private RuntimeException translateS3Exception(Exception e, UUID attachmentId, String objectKey) {
if (e instanceof io.minio.errors.ErrorResponseException minioEx) {
if ("NoSuchKey".equals(minioEx.errorResponse().code())) {
log.warn("S3 object missing for attachment {}", attachmentId);
return new NotFoundException("Attachment content not found");
}
}
log.error("S3 download failed for {}", objectKey, e);
return new StorageUnavailableException("S3 download failed", e);
}
public record DownloadedAttachment(java.io.InputStream stream, String contentType, String filename) {}
private void assertMember(UUID chatId, UUID userId) {
boolean isMember = chatService.existsByIdAndMembersId(chatId, userId);
if (!isMember) {
throw new NotFoundException("Attachment not found");
}
}
}
@@ -127,4 +127,9 @@ public class ChatService {
private CursorCodec.PageBoundary boundaryOf(Chat chat) {
return new CursorCodec.PageBoundary(chat.getCreatedAt(), chat.getId());
}
@Transactional
public boolean existsByIdAndMembersId(UUID chatId, UUID userId){
return chatRepository.existsByIdAndMembersId(chatId, userId);
}
}
@@ -0,0 +1,72 @@
package ru.mtusi.messenger.validation;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import ru.mtusi.messenger.config.properties.UploadProperties;
import ru.mtusi.messenger.exception.InvalidRequestException;
import ru.mtusi.messenger.exception.PayloadTooLargeException;
import ru.mtusi.messenger.exception.UnsupportedMediaTypeException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;
@Component
@RequiredArgsConstructor
public class AttachmentValidator {
private final UploadProperties props;
public String validate(byte[] bytes, String filename) {
if (bytes.length > props.maxBytes()) {
throw new PayloadTooLargeException("File exceeds max size");
}
sanitizeFilename(filename);
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ImageInputStream iis = ImageIO.createImageInputStream(bais)) {
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (!readers.hasNext()) {
throw new UnsupportedMediaTypeException("Only PNG/JPEG allowed");
}
ImageReader reader = readers.next();
reader.setInput(iis, true, true);
String format = reader.getFormatName().toLowerCase();
String contentType = switch (format) {
case "png" -> "image/png";
case "jpeg", "jpg" -> "image/jpeg";
default -> throw new UnsupportedMediaTypeException("Only PNG/JPEG allowed");
};
long pixels = (long) reader.getWidth(0) * reader.getHeight(0);
if (pixels > props.maxPixels()) {
throw new PayloadTooLargeException("Image dimensions exceed limit");
}
return contentType;
} catch (IOException e) {
throw new UnsupportedMediaTypeException("Corrupted or unsupported image");
}
}
public String sanitizeFilename(String filename) {
if (filename == null || filename.isBlank()) {
throw new InvalidRequestException("Filename is blank");
}
String cleaned = filename
.replaceAll("[\\x00-\\x1F/\\\\]", "_")
.replaceAll("^\\.+", "");
if (cleaned.isBlank() || cleaned.codePointCount(0, cleaned.length()) > 255) {
throw new InvalidRequestException("Invalid filename");
}
return cleaned;
}
}
+31 -1
View File
@@ -38,6 +38,13 @@ spring:
cache:
type: redis
servlet:
multipart:
max-file-size: ${MAX_UPLOAD_BYTES:10485760}
max-request-size: ${MAX_UPLOAD_BYTES:10485760}
file-size-threshold: 0
resolve-lazily: false
server:
address: ${APP_HOST:0.0.0.0}
port: ${APP_PORT:8080}
@@ -81,9 +88,32 @@ app:
keep-alive-interval: ${REDIS_KEEP_ALIVE_INTERVAL:10s}
tcp-user-timeout: ${REDIS_TCP_USER_TIMEOUT:10s}
s3:
endpoint: ${S3_ENDPOINT:http://minio:9000}
bucket: ${S3_BUCKET:messenger-attachments}
region: ${S3_REGION:us-east-1}
access-key: ${S3_ACCESS_KEY_ID:}
secret-key: ${S3_SECRET_ACCESS_KEY:}
path-style-access: true
presign-expiry-seconds: ${S3_PRESIGN_EXPIRY_SECONDS:300}
upload:
max-bytes: ${MAX_UPLOAD_BYTES:10485760}
max-pixels: ${MAX_IMAGE_PIXELS:40000000}
allowed-content-types:
- image/png
- image/jpeg
reconciler:
enabled: ${RECONCILER_ENABLED:true}
interval-ms: ${RECONCILER_INTERVAL_MS:3600000}
grace-period-minutes: ${RECONCILER_GRACE_PERIOD_MINUTES:15}
dry-run: ${RECONCILER_DRY_RUN:false}
logging:
pattern:
console: "%d{ISO8601} %-5level [%X{requestId}] %logger{20} - %msg%n"
level:
org.springframework.cache: TRACE
org.springframework.cache.interceptor: TRACE
org.springframework.cache.interceptor: TRACE
software.amazon.awssdk: DEBUG
+23 -1
View File
@@ -68,6 +68,22 @@ CREATE TABLE idempotency_keys (
CONSTRAINT ck_idem_status_body_paired CHECK ((status_code IS NULL) = (response_body IS NULL))
);
CREATE TABLE attachments (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
object_key VARCHAR(512) NOT NULL,
filename VARCHAR(255) NOT NULL,
content_type VARCHAR(100) NOT NULL,
size_bytes BIGINT NOT NULL,
checksum VARCHAR(64) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'READY'
CHECK (status IN ('UPLOADING', 'READY', 'BROKEN')),
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT uq_attachments_object_key UNIQUE (object_key)
);
-- Keyset pagination: ORDER BY created_at ASC, id ASC (JPQL uses ASC)
CREATE INDEX idx_messages_chat_created_asc_id
ON messages (chat_id, created_at ASC, id ASC);
@@ -91,4 +107,10 @@ CREATE INDEX idx_sessions_expires_active
WHERE revoked_at IS NULL;
CREATE INDEX idx_idempotency_created
ON idempotency_keys (created_at);
ON idempotency_keys (created_at);
CREATE INDEX idx_attachments_object_key ON attachments(object_key);
CREATE INDEX idx_attachments_chat_id ON attachments(chat_id);
CREATE INDEX idx_attachments_owner_id ON attachments(owner_id);
CREATE INDEX idx_attachments_status ON attachments(status);
CREATE INDEX idx_attachments_created_at ON attachments(created_at);