Files
mtusi2026lab6/src/main/java/ru/mtusi/messenger/service/AttachmentVariantService.java
T

58 lines
2.3 KiB
Java

package ru.mtusi.messenger.service;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import ru.mtusi.messenger.dto.VariantDTO;
import ru.mtusi.messenger.exception.NotFoundException;
import ru.mtusi.messenger.model.Attachment;
import ru.mtusi.messenger.model.AttachmentStatus;
import java.util.List;
@Service
@RequiredArgsConstructor
public class AttachmentVariantService {
private final JdbcTemplate jdbc;
public List<VariantDTO> metadata(Attachment attachment) {
if (attachment.getStatus() != AttachmentStatus.READY) return List.of();
return jdbc.query(
"""
SELECT * FROM attachment_variants
WHERE attachment_id = ? AND pipeline_version = ? ORDER BY name
""",
(row, index) ->
new VariantDTO(
VariantDTO.NameEnum.fromValue(row.getString("name")),
VariantDTO.ContentTypeEnum.fromValue(row.getString("content_type")),
row.getLong("size_bytes"),
row.getInt("width"),
row.getInt("height"),
row.getString("sha256")),
attachment.getId(),
attachment.getPipelineVersion());
}
public Content content(Attachment attachment, String variant) {
if ("original".equals(variant))
return new Content(attachment.getObjectKey(), attachment.getContentType());
List<Content> rows =
jdbc.query(
"""
SELECT object_key, content_type FROM attachment_variants
WHERE attachment_id = ? AND pipeline_version = ? AND name = ?
""",
(row, index) -> new Content(row.getString(1), row.getString(2)),
attachment.getId(),
attachment.getPipelineVersion(),
variant);
if (rows.isEmpty()) throw new NotFoundException("Variant not found");
return rows.getFirst();
}
public record Content(String objectKey, String contentType) {}
}