|
@@ -1,6 +1,5 @@
|
|
|
package com.chelvc.framework.upload.support;
|
|
|
|
|
|
-import java.awt.*;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
@@ -10,13 +9,13 @@ import java.util.UUID;
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
import com.chelvc.framework.base.util.FileUtils;
|
|
|
+import com.chelvc.framework.base.util.StringUtils;
|
|
|
import com.chelvc.framework.upload.UploadHandler;
|
|
|
import com.chelvc.framework.upload.config.TencentUploadProperties;
|
|
|
import com.qcloud.cos.COSClient;
|
|
|
import com.qcloud.cos.model.ObjectMetadata;
|
|
|
import lombok.NonNull;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|
|
import org.springframework.stereotype.Component;
|
|
@@ -27,7 +26,6 @@ import org.springframework.stereotype.Component;
|
|
|
* @author Woody
|
|
|
* @date 2023/4/5
|
|
|
*/
|
|
|
-@Slf4j
|
|
|
@Component
|
|
|
@ConditionalOnBean(TencentUploadProperties.class)
|
|
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
|
@@ -36,67 +34,40 @@ public class TencentUploadHandler implements UploadHandler {
|
|
|
private final TencentUploadProperties properties;
|
|
|
|
|
|
/**
|
|
|
- * 将唯一标识转换成资源地址
|
|
|
+ * 将文件名称转换成URL地址
|
|
|
*
|
|
|
- * @param key 唯一标识
|
|
|
- * @return 资源地址
|
|
|
+ * @param filename 文件名称
|
|
|
+ * @return URL地址
|
|
|
*/
|
|
|
- private String key2uri(String key) {
|
|
|
+ private String name2url(String filename) {
|
|
|
return String.format(
|
|
|
"https://%s.cos.%s.myqcloud.com/%s",
|
|
|
- this.properties.getBucket(), this.properties.getRegion(), key
|
|
|
+ this.properties.getBucket(), this.properties.getRegion(), filename
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 文件上传
|
|
|
- *
|
|
|
- * @param stream 文件流
|
|
|
- * @param filename 文件名
|
|
|
- * @param metadata 文件信息
|
|
|
- * @return 文件访问地址
|
|
|
- */
|
|
|
- private String upload(InputStream stream, String filename, ObjectMetadata metadata) {
|
|
|
- if (log.isDebugEnabled()) {
|
|
|
- log.debug("Uploading stream: {}", filename);
|
|
|
- }
|
|
|
- String key = FileUtils.generateUniqueName(filename);
|
|
|
- this.client.putObject(this.properties.getBucket(), key, stream, metadata);
|
|
|
- return this.key2uri(key);
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public String upload(@NonNull File file) {
|
|
|
- if (log.isDebugEnabled()) {
|
|
|
- log.debug("Uploading file: {}", file);
|
|
|
- }
|
|
|
- String key = FileUtils.generateUniqueName(file);
|
|
|
- this.client.putObject(this.properties.getBucket(), key, file);
|
|
|
- return this.key2uri(key);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String upload(@NonNull InputStream stream, @NonNull String filename) {
|
|
|
- return this.upload(stream, filename, new ObjectMetadata());
|
|
|
+ String suffix = FileUtils.getSuffix(file);
|
|
|
+ String filename = StringUtils.uuid() + StringUtils.ifEmpty(suffix, "." + suffix);
|
|
|
+ this.client.putObject(this.properties.getBucket(), filename, file);
|
|
|
+ return this.name2url(filename);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String upload(@NonNull InputStream stream, @NonNull String filename, long size) {
|
|
|
- ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- metadata.setContentLength(size);
|
|
|
- return this.upload(stream, filename, metadata);
|
|
|
+ public String upload(@NonNull InputStream stream, @NonNull String suffix) {
|
|
|
+ String filename = StringUtils.uuid() + StringUtils.ifEmpty(suffix, "." + suffix);
|
|
|
+ this.client.putObject(this.properties.getBucket(), filename, stream, new ObjectMetadata());
|
|
|
+ return this.name2url(filename);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String upload(@NonNull BufferedImage image) throws IOException {
|
|
|
// 图片压缩
|
|
|
- int width = image.getWidth(null);
|
|
|
- int height = image.getHeight(null);
|
|
|
- BufferedImage compress = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
- compress.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
|
|
|
+ image = FileUtils.compress(image);
|
|
|
File target = new File(FileUtils.getUploadDirectory(), UUID.randomUUID().toString() + ".jpeg");
|
|
|
try (FileOutputStream output = new FileOutputStream(target)) {
|
|
|
- ImageIO.write(compress, "jpeg", output);
|
|
|
+ ImageIO.write(image, "jpeg", output);
|
|
|
}
|
|
|
|
|
|
// 上传图片
|