mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-20 05:50:53 +08:00
Merge pull request #255 from nagisa77/codex/upload-images-to-dynamic_assert-folder
Use dynamic_assert directory for COS uploads
This commit is contained in:
@@ -28,6 +28,7 @@ public class CosImageUploader extends ImageUploader {
|
||||
private final COSClient cosClient;
|
||||
private final String bucketName;
|
||||
private final String baseUrl;
|
||||
private static final String UPLOAD_DIR = "dynamic_assert/";
|
||||
private static final Logger logger = LoggerFactory.getLogger(CosImageUploader.class);
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(2,
|
||||
new CustomizableThreadFactory("cos-upload-"));
|
||||
@@ -71,18 +72,19 @@ public class CosImageUploader extends ImageUploader {
|
||||
ext = filename.substring(dot);
|
||||
}
|
||||
String randomName = UUID.randomUUID().toString().replace("-", "") + ext;
|
||||
logger.debug("Generated object key {}", randomName);
|
||||
String objectKey = UPLOAD_DIR + randomName;
|
||||
logger.debug("Generated object key {}", objectKey);
|
||||
|
||||
ObjectMetadata meta = new ObjectMetadata();
|
||||
meta.setContentLength(data.length);
|
||||
PutObjectRequest req = new PutObjectRequest(
|
||||
bucketName,
|
||||
randomName,
|
||||
objectKey,
|
||||
new ByteArrayInputStream(data),
|
||||
meta);
|
||||
logger.debug("Sending PutObject request to bucket {}", bucketName);
|
||||
cosClient.putObject(req);
|
||||
String url = baseUrl + "/" + randomName;
|
||||
String url = baseUrl + "/" + objectKey;
|
||||
logger.debug("Upload successful, accessible at {}", url);
|
||||
return url;
|
||||
}, executor);
|
||||
|
||||
@@ -18,6 +18,6 @@ class CosImageUploaderTest {
|
||||
String url = uploader.upload("data".getBytes(), "img.png").join();
|
||||
|
||||
verify(client).putObject(any(PutObjectRequest.class));
|
||||
assertTrue(url.matches("http://cos.example.com/[a-f0-9]{32}\\.png"));
|
||||
assertTrue(url.matches("http://cos.example.com/dynamic_assert/[a-f0-9]{32}\\.png"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user