Delete orphan files from the image bucket nightly

OrphanFileCleanupService (03:30, ShedLock) lists the bucket and deletes objects
older than 24 hours that no images.url or users.avatar_url points to (full
URLs in legacy rows are normalised to keys), at most 5000 per run. Such files
appear when an upload via POST /api/v1/files is never attached or a request
fails between upload and save. Deleted files stay recoverable for 30 days as
noncurrent versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-20 01:42:39 +03:00
parent 3dcd27d08a
commit 4ff61d8eda
6 changed files with 206 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
package com.krylov.refound.service;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.krylov.refound.config.MinioProperties;
import com.krylov.refound.repository.ImageRepository;
import com.krylov.refound.repository.UserRepository;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.S3Object;
import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable;
class OrphanFileCleanupServiceTest {
@Test
void deletesOnlyOldUnreferencedFiles() {
S3Client s3 = mock(S3Client.class);
Instant old = Instant.now().minus(Duration.ofDays(3));
ListObjectsV2Response page = ListObjectsV2Response.builder().contents(
S3Object.builder().key("used.jpg").lastModified(old).build(),
S3Object.builder().key("avatars/legacy.jpg").lastModified(old).build(),
S3Object.builder().key("orphan.jpg").lastModified(old).build(),
S3Object.builder().key("fresh.jpg").lastModified(Instant.now()).build()).build();
when(s3.listObjectsV2Paginator(any(ListObjectsV2Request.class)))
.thenReturn(new ListObjectsV2Iterable(s3, ListObjectsV2Request.builder().build()));
when(s3.listObjectsV2(any(ListObjectsV2Request.class))).thenReturn(page);
MinioProperties properties = new MinioProperties("http://minio", "refound-images", "a", "b", "us-east-1");
ImageRepository images = mock(ImageRepository.class);
when(images.findAllUrls()).thenReturn(List.of("used.jpg"));
UserRepository users = mock(UserRepository.class);
// Старая запись аватара хранит полный URL
when(users.findAllAvatarUrls()).thenReturn(List.of("http://minio:9010/refound-images/avatars/legacy.jpg?x=1"));
FileStorageService storage = mock(FileStorageService.class);
new OrphanFileCleanupService(s3, properties, images, users, storage, true, Duration.ofHours(24), 100)
.cleanupOrphanFiles();
verify(storage).deleteFile("orphan.jpg");
verify(storage, never()).deleteFile("used.jpg");
verify(storage, never()).deleteFile("avatars/legacy.jpg");
verify(storage, never()).deleteFile("fresh.jpg");
}
@Test
void disabledJobDoesNothing() {
S3Client s3 = mock(S3Client.class);
FileStorageService storage = mock(FileStorageService.class);
new OrphanFileCleanupService(s3, mock(MinioProperties.class), mock(ImageRepository.class),
mock(UserRepository.class), storage, false, Duration.ofHours(24), 100).cleanupOrphanFiles();
verify(storage, never()).deleteFile(anyString());
}
}