新需求更新 同步更新

This commit is contained in:
supernijia
2026-08-06 01:11:54 +08:00
parent 28423e015b
commit 40f4fae272
112 changed files with 7739 additions and 637 deletions
@@ -25,7 +25,9 @@ public class LocalTempCleanupService {
"convert-result",
"split-result",
"brand-source-download",
"brand-result"
"brand-result",
"similar-asin-result",
"similar-asin-image-cache"
);
private final StorageProperties storageProperties;
@@ -1,7 +1,9 @@
package com.nanri.aiimage.modules.file.service.oss;
import com.nanri.aiimage.config.OssProperties;
import io.minio.BucketExistsArgs;
import io.minio.GetObjectArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
@@ -93,6 +95,77 @@ public class OssStorageService {
}
}
public void ensureBucketExists(String bucket) {
String normalizedBucket = requireStorageName(bucket, "bucket");
try {
boolean exists = buildClient().bucketExists(BucketExistsArgs.builder()
.bucket(normalizedBucket)
.build());
if (exists) {
return;
}
try {
buildClient().makeBucket(MakeBucketArgs.builder()
.bucket(normalizedBucket)
.build());
} catch (ErrorResponseException ex) {
if (!isBucketAlreadyPresent(ex)) {
throw ex;
}
}
} catch (Exception ex) {
throw storageFailure("ensure bucket", normalizedBucket, ex);
}
}
public void uploadBytes(String bucket, String objectKey, byte[] content, String contentType) {
String normalizedBucket = requireStorageName(bucket, "bucket");
String normalizedObjectKey = requireStorageName(objectKey, "objectKey");
byte[] bytes = content == null ? new byte[0] : content;
try (ByteArrayInputStream stream = new ByteArrayInputStream(bytes)) {
buildClient().putObject(PutObjectArgs.builder()
.bucket(normalizedBucket)
.object(normalizedObjectKey)
.stream(stream, bytes.length, -1)
.contentType(firstNonBlank(contentType, "application/octet-stream"))
.build());
} catch (Exception ex) {
throw storageFailure("upload", normalizedBucket + "/" + normalizedObjectKey, ex);
}
}
public byte[] readObjectBytes(String bucket, String objectKey) {
String normalizedBucket = requireStorageName(bucket, "bucket");
String normalizedObjectKey = requireStorageName(objectKey, "objectKey");
try (var stream = buildClient().getObject(GetObjectArgs.builder()
.bucket(normalizedBucket)
.object(normalizedObjectKey)
.build())) {
return stream.readAllBytes();
} catch (Exception ex) {
throw storageFailure("read", normalizedBucket + "/" + normalizedObjectKey, ex);
}
}
public boolean objectExists(String bucket, String objectKey) {
String normalizedBucket = requireStorageName(bucket, "bucket");
String normalizedObjectKey = requireStorageName(objectKey, "objectKey");
try {
buildClient().statObject(StatObjectArgs.builder()
.bucket(normalizedBucket)
.object(normalizedObjectKey)
.build());
return true;
} catch (ErrorResponseException ex) {
if (isNotFound(ex)) {
return false;
}
throw storageFailure("stat", normalizedBucket + "/" + normalizedObjectKey, ex);
} catch (Exception ex) {
throw storageFailure("stat", normalizedBucket + "/" + normalizedObjectKey, ex);
}
}
public String uploadTaskScopePayload(String moduleType, Long taskId, String scopeHash, String content) {
String normalizedScopeHash = scopeHash == null || scopeHash.isBlank() ? UUID.randomUUID().toString() : scopeHash;
String objectKey = String.format("task-scope/%s/%s/%s.json", normalizeModuleType(moduleType), taskId, normalizedScopeHash);
@@ -310,7 +383,7 @@ public class OssStorageService {
}
private List<String> configuredBuckets() {
return Stream.of(ossProperties.getBucket(), imageVideoBucket(), digitalHumanBucket())
return Stream.of(ossProperties.getBucket(), imageVideoBucket(), digitalHumanBucket(), templateBucket())
.filter(Objects::nonNull)
.map(String::trim)
.filter(bucket -> !bucket.isBlank())
@@ -395,6 +468,10 @@ public class OssStorageService {
return firstNonBlank(ossProperties.getDigitalHumanBucket(), ossProperties.getBucket());
}
private String templateBucket() {
return firstNonBlank(ossProperties.getTemplateBucket(), ossProperties.getBucket());
}
private String publicEndpoint() {
return withScheme(firstNonBlank(ossProperties.getPublicEndpoint(), ossProperties.getEndpoint()));
}
@@ -446,6 +523,11 @@ public class OssStorageService {
return "NoSuchKey".equals(code) || "NoSuchObject".equals(code) || "NoSuchBucket".equals(code);
}
private boolean isBucketAlreadyPresent(ErrorResponseException ex) {
String code = ex.errorResponse() == null ? null : ex.errorResponse().code();
return "BucketAlreadyOwnedByYou".equals(code) || "BucketAlreadyExists".equals(code);
}
private IllegalStateException storageFailure(String operation, String objectKey, Exception cause) {
return new IllegalStateException("failed to " + operation + " object in MinIO: " + objectKey, cause);
}
@@ -477,6 +559,13 @@ public class OssStorageService {
return preferred == null || preferred.isBlank() ? fallback : preferred.trim();
}
private String requireStorageName(String value, String field) {
if (value == null || value.isBlank()) {
throw new IllegalArgumentException(field + " must not be blank");
}
return value.trim();
}
private record StorageLocation(String bucket, String objectKey) {
}