mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 22:21:09 +08:00
feat: support presigned url upload
This commit is contained in:
@@ -74,4 +74,9 @@ public class UploadController {
|
||||
return ResponseEntity.internalServerError().body(Map.of("code", 3, "msg", "Upload failed"));
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/presign")
|
||||
public java.util.Map<String, String> presign(@RequestParam("filename") String filename) {
|
||||
return imageUploader.presignUpload(filename);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import com.qcloud.cos.auth.BasicCOSCredentials;
|
||||
import com.qcloud.cos.auth.COSCredentials;
|
||||
import com.qcloud.cos.model.ObjectMetadata;
|
||||
import com.qcloud.cos.model.PutObjectRequest;
|
||||
import com.qcloud.cos.http.HttpMethodName;
|
||||
import com.qcloud.cos.model.GeneratePresignedUrlRequest;
|
||||
import com.qcloud.cos.region.Region;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
||||
@@ -98,4 +100,25 @@ public class CosImageUploader extends ImageUploader {
|
||||
logger.warn("Failed to delete image {} from COS", key, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.util.Map<String, String> presignUpload(String filename) {
|
||||
String ext = "";
|
||||
int dot = filename.lastIndexOf('.');
|
||||
if (dot != -1) {
|
||||
ext = filename.substring(dot);
|
||||
}
|
||||
String randomName = java.util.UUID.randomUUID().toString().replace("-", "") + ext;
|
||||
String objectKey = UPLOAD_DIR + randomName;
|
||||
java.util.Date expiration = new java.util.Date(System.currentTimeMillis() + 15 * 60 * 1000L);
|
||||
GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectKey, HttpMethodName.PUT);
|
||||
req.setExpiration(expiration);
|
||||
java.net.URL url = cosClient.generatePresignedUrl(req);
|
||||
String fileUrl = baseUrl + "/" + objectKey;
|
||||
return java.util.Map.of(
|
||||
"uploadUrl", url.toString(),
|
||||
"fileUrl", fileUrl,
|
||||
"key", objectKey
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,14 @@ public abstract class ImageUploader {
|
||||
|
||||
protected abstract void deleteFromStore(String key);
|
||||
|
||||
/**
|
||||
* Generate a presigned PUT URL for direct browser upload.
|
||||
* Default implementation is unsupported.
|
||||
*/
|
||||
public java.util.Map<String, String> presignUpload(String filename) {
|
||||
throw new UnsupportedOperationException("presignUpload not supported");
|
||||
}
|
||||
|
||||
/** Extract COS URLs from text. */
|
||||
public Set<String> extractUrls(String text) {
|
||||
Set<String> set = new HashSet<>();
|
||||
|
||||
Reference in New Issue
Block a user