Оценка 4 готово
This commit is contained in:
@@ -2,8 +2,10 @@ package org.example;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class LidoApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LidoApplication.class, args);
|
||||
|
||||
@@ -5,9 +5,11 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -20,17 +22,15 @@ public class ImageUploadValidator {
|
||||
private static final Set<String> ALLOWED = Set.of("image/png", "image/jpeg");
|
||||
|
||||
public record ValidatedImage(
|
||||
byte[] bytes,
|
||||
Path tempFile,
|
||||
String contentType,
|
||||
String safeFilename,
|
||||
int width,
|
||||
int height,
|
||||
long sizeBytes,
|
||||
String sha256Hex
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Raw body (не multipart) + filename из query.
|
||||
*/
|
||||
public ValidatedImage validate(String filename, InputStream in) throws IOException {
|
||||
if (in == null) {
|
||||
throw new BadRequestException("Empty body");
|
||||
@@ -38,24 +38,70 @@ public class ImageUploadValidator {
|
||||
|
||||
String safeName = sanitizeFilename(filename);
|
||||
|
||||
byte[] bytes = in.readNBytes((int) MAX_BYTES + 1);
|
||||
if (bytes.length == 0) {
|
||||
throw new BadRequestException("Empty file");
|
||||
}
|
||||
if (bytes.length > MAX_BYTES) {
|
||||
throw new BadRequestException("File too large (max 10 MiB)");
|
||||
byte[] header = new byte[8];
|
||||
int totalRead = 0;
|
||||
int read;
|
||||
while (totalRead < header.length && (read = in.read(header, totalRead, header.length - totalRead)) != -1) {
|
||||
totalRead += read;
|
||||
}
|
||||
|
||||
String detected = detectMime(bytes);
|
||||
if (totalRead == 0) {
|
||||
throw new BadRequestException("Empty file");
|
||||
}
|
||||
|
||||
String detected = detectMime(header, totalRead);
|
||||
if (!ALLOWED.contains(detected)) {
|
||||
throw new BadRequestException("Only PNG and JPEG are allowed");
|
||||
}
|
||||
|
||||
Path tempFile = Files.createTempFile("upload-", ".tmp");
|
||||
MessageDigest digest;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
} catch (Exception e) {
|
||||
Files.deleteIfExists(tempFile);
|
||||
throw new IllegalStateException("SHA-256 not available", e);
|
||||
}
|
||||
|
||||
long sizeBytes = 0;
|
||||
boolean success = false;
|
||||
try (OutputStream out = Files.newOutputStream(tempFile)) {
|
||||
out.write(header, 0, totalRead);
|
||||
digest.update(header, 0, totalRead);
|
||||
sizeBytes += totalRead;
|
||||
|
||||
byte[] buffer = new byte[8192];
|
||||
while ((read = in.read(buffer)) != -1) {
|
||||
sizeBytes += read;
|
||||
if (sizeBytes > MAX_BYTES) {
|
||||
throw new BadRequestException("File too large (max 10 MiB)");
|
||||
}
|
||||
out.write(buffer, 0, read);
|
||||
digest.update(buffer, 0, read);
|
||||
}
|
||||
success = true;
|
||||
} finally {
|
||||
if (!success) {
|
||||
Files.deleteIfExists(tempFile);
|
||||
}
|
||||
}
|
||||
|
||||
if (sizeBytes == 0) {
|
||||
Files.deleteIfExists(tempFile);
|
||||
throw new BadRequestException("Empty file");
|
||||
}
|
||||
|
||||
BufferedImage image;
|
||||
try (InputStream imageIn = new ByteArrayInputStream(bytes)) {
|
||||
image = ImageIO.read(imageIn);
|
||||
try (InputStream imageIn = Files.newInputStream(tempFile)) {
|
||||
try {
|
||||
image = ImageIO.read(imageIn);
|
||||
} catch (IOException e) {
|
||||
Files.deleteIfExists(tempFile);
|
||||
throw new BadRequestException("Invalid image");
|
||||
}
|
||||
}
|
||||
if (image == null) {
|
||||
Files.deleteIfExists(tempFile);
|
||||
throw new BadRequestException("Invalid image");
|
||||
}
|
||||
|
||||
@@ -63,27 +109,26 @@ public class ImageUploadValidator {
|
||||
int height = image.getHeight();
|
||||
long pixels = (long) width * (long) height;
|
||||
if (width <= 0 || height <= 0 || pixels > MAX_PIXELS) {
|
||||
Files.deleteIfExists(tempFile);
|
||||
throw new BadRequestException("Image exceeds pixel limit");
|
||||
}
|
||||
|
||||
return new ValidatedImage(
|
||||
bytes,
|
||||
detected,
|
||||
safeName,
|
||||
width,
|
||||
height,
|
||||
sha256Hex(bytes)
|
||||
);
|
||||
StringBuilder sb = new StringBuilder(64);
|
||||
for (byte b : digest.digest()) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
|
||||
return new ValidatedImage(tempFile, detected, safeName, width, height, sizeBytes, sb.toString());
|
||||
}
|
||||
|
||||
private static String detectMime(byte[] b) {
|
||||
if (b.length >= 3
|
||||
private static String detectMime(byte[] b, int len) {
|
||||
if (len >= 3
|
||||
&& (b[0] & 0xFF) == 0xFF
|
||||
&& (b[1] & 0xFF) == 0xD8
|
||||
&& (b[2] & 0xFF) == 0xFF) {
|
||||
return "image/jpeg";
|
||||
}
|
||||
if (b.length >= 8
|
||||
if (len >= 8
|
||||
&& (b[0] & 0xFF) == 0x89
|
||||
&& b[1] == 0x50
|
||||
&& b[2] == 0x4E
|
||||
@@ -118,17 +163,4 @@ public class ImageUploadValidator {
|
||||
}
|
||||
return base;
|
||||
}
|
||||
|
||||
private static String sha256Hex(byte[] data) {
|
||||
try {
|
||||
byte[] dig = MessageDigest.getInstance("SHA-256").digest(data);
|
||||
StringBuilder sb = new StringBuilder(dig.length * 2);
|
||||
for (byte value : dig) {
|
||||
sb.append(String.format("%02x", value));
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("SHA-256 not available", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,12 +10,13 @@ import org.example.model.Attachment;
|
||||
import org.example.model.User;
|
||||
import org.example.repository.AttachmentRepository;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -30,6 +31,8 @@ public class AttachmentService {
|
||||
private final ChatService chatService;
|
||||
private final UserService userService;
|
||||
|
||||
private static final long ORPHAN_GRACE_MS = 60L * 60L * 1000L; // 1 hour grace period
|
||||
|
||||
@Transactional
|
||||
public AttachmentResponse upload(
|
||||
UUID chatId,
|
||||
@@ -45,28 +48,46 @@ public class AttachmentService {
|
||||
UUID id = UUID.randomUUID();
|
||||
String objectKey = "chats/" + chatId + "/" + id;
|
||||
|
||||
s3StorageService.put(
|
||||
objectKey,
|
||||
new ByteArrayInputStream(img.bytes()),
|
||||
img.bytes().length,
|
||||
img.contentType()
|
||||
);
|
||||
boolean s3Ok = false;
|
||||
try (InputStream fileIn = Files.newInputStream(img.tempFile())) {
|
||||
s3StorageService.put(
|
||||
objectKey,
|
||||
fileIn,
|
||||
img.sizeBytes(),
|
||||
img.contentType()
|
||||
);
|
||||
s3Ok = true;
|
||||
} finally {
|
||||
Files.deleteIfExists(img.tempFile());
|
||||
}
|
||||
|
||||
Attachment saved = attachmentRepository.save(Attachment.builder()
|
||||
.id(id)
|
||||
.chatId(chatId)
|
||||
.ownerId(user.getId())
|
||||
.objectKey(objectKey)
|
||||
.filename(img.safeFilename())
|
||||
.contentType(img.contentType())
|
||||
.sizeBytes(img.bytes().length)
|
||||
.sha256(img.sha256Hex())
|
||||
.status("ready")
|
||||
.errorCode(null)
|
||||
.variants(List.of())
|
||||
.createdAt(Instant.now())
|
||||
.updatedAt(Instant.now())
|
||||
.build());
|
||||
Attachment saved;
|
||||
try {
|
||||
saved = attachmentRepository.save(Attachment.builder()
|
||||
.id(id)
|
||||
.chatId(chatId)
|
||||
.ownerId(user.getId())
|
||||
.objectKey(objectKey)
|
||||
.filename(img.safeFilename())
|
||||
.contentType(img.contentType())
|
||||
.sizeBytes(img.sizeBytes())
|
||||
.sha256(img.sha256Hex())
|
||||
.status("ready")
|
||||
.errorCode(null)
|
||||
.variants(List.of())
|
||||
.createdAt(Instant.now())
|
||||
.updatedAt(Instant.now())
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
if (s3Ok) {
|
||||
try {
|
||||
s3StorageService.delete(objectKey);
|
||||
} catch (Exception deleteEx) {
|
||||
// orphan will be reclaimed by the reconciler
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
return toResponse(saved);
|
||||
}
|
||||
@@ -84,7 +105,6 @@ public class AttachmentService {
|
||||
return toResponse(getForMember(attachmentId));
|
||||
}
|
||||
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public void writeContent(UUID attachmentId,
|
||||
String variant,
|
||||
@@ -102,9 +122,46 @@ public class AttachmentService {
|
||||
response.flushBuffer();
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "${app.attachment.reconciler.interval-ms:300000}")
|
||||
public void reconcileOrphanedObjects() {
|
||||
Instant now = Instant.now();
|
||||
Instant cutoff = now.minusMillis(ORPHAN_GRACE_MS);
|
||||
|
||||
List<Attachment> all = attachmentRepository.findAll();
|
||||
java.util.Set<UUID> dbKeys = new java.util.HashSet<>();
|
||||
for (Attachment a : all) {
|
||||
dbKeys.add(a.getId());
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
var objects = s3StorageService.listObjects();
|
||||
for (var obj : objects) {
|
||||
String key = obj.key();
|
||||
if (!key.startsWith("chats/")) {
|
||||
continue;
|
||||
}
|
||||
String[] parts = key.split("/", 3);
|
||||
if (parts.length < 3) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
UUID attachmentId = UUID.fromString(parts[2]);
|
||||
if (!dbKeys.contains(attachmentId)) {
|
||||
if (obj.lastModified().isBefore(cutoff)) {
|
||||
s3StorageService.delete(key);
|
||||
}
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
if (obj.lastModified().isBefore(cutoff)) {
|
||||
s3StorageService.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
org.slf4j.LoggerFactory.getLogger(AttachmentService.class)
|
||||
.warn("Reconciler S3 list failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
static AttachmentResponse toResponse(Attachment a) {
|
||||
return AttachmentResponse.builder()
|
||||
@@ -123,5 +180,4 @@ public class AttachmentService {
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,12 +7,17 @@ import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.core.sync.ResponseTransformer;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectAclRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.S3Object;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -21,6 +26,8 @@ public class S3StorageService {
|
||||
private final S3Client s3Client;
|
||||
private final S3Properties properties;
|
||||
|
||||
public record S3ObjectInfo(String key, Instant lastModified) {}
|
||||
|
||||
public void put(
|
||||
String objectKey,
|
||||
InputStream in,
|
||||
@@ -58,4 +65,19 @@ public class S3StorageService {
|
||||
.key(objectKey)
|
||||
.build());
|
||||
}
|
||||
|
||||
public List<S3ObjectInfo> listObjects(){
|
||||
List<S3ObjectInfo> result = new ArrayList<>();
|
||||
ListObjectsV2Request request = ListObjectsV2Request.builder()
|
||||
.bucket(properties.getBucket())
|
||||
.build();
|
||||
ListObjectsV2Response response = s3Client.listObjectsV2(request);
|
||||
for (S3Object obj : response.contents()) {
|
||||
result.add(new S3ObjectInfo(
|
||||
obj.key(),
|
||||
obj.lastModified() != null ? obj.lastModified() : Instant.now()
|
||||
));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user