|
@@ -1,15 +1,10 @@
|
|
|
package com.backendsys.modules.sdk.tencent.cos.service.impl;
|
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
-import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.hutool.crypto.digest.DigestUtil;
|
|
|
-import com.backendsys.entity.System.SysFile.SysFileCategoryDTO;
|
|
|
-import com.backendsys.entity.System.SysFile.SysFileDTO;
|
|
|
-import com.backendsys.entity.Tencent.TencentCos.MultipartUploadRespDTO;
|
|
|
import com.backendsys.exception.CustException;
|
|
|
import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
|
|
|
-import com.backendsys.modules.common.utils.CommonUtil;
|
|
|
+import com.backendsys.modules.common.utils.FilenameUtil;
|
|
|
import com.backendsys.modules.sdk.tencent.cos.entity.Progress;
|
|
|
import com.backendsys.modules.sdk.tencent.cos.entity.ProgressData;
|
|
|
import com.backendsys.modules.sdk.tencent.cos.service.TencentCosService;
|
|
@@ -29,8 +24,6 @@ import com.qcloud.cos.transfer.Transfer;
|
|
|
import com.qcloud.cos.transfer.TransferManager;
|
|
|
import com.qcloud.cos.transfer.TransferProgress;
|
|
|
import com.qcloud.cos.transfer.Upload;
|
|
|
-import com.qcloud.cos.utils.IOUtils;
|
|
|
-import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.joda.time.DateTime;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -39,14 +32,15 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
-
|
|
|
-import static org.aspectj.weaver.tools.cache.SimpleCacheFactory.path;
|
|
|
|
|
|
+// 图片处理概述
|
|
|
+// https://cloud.tencent.com/document/product/436/42215
|
|
|
+//// // https://cloud.tencent.com/document/product/460/6929
|
|
|
+//// // https://github.com/tencentyun/cos-java-sdk-v5/blob/master/src/main/java/com/qcloud/cos/demo/ci/BasicImageProcessing.java
|
|
|
@Service
|
|
|
public class TencentCosServiceImpl implements TencentCosService {
|
|
|
|
|
@@ -105,13 +99,16 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
progress.setState(state);
|
|
|
progress.setData(progressData);
|
|
|
sseUtil.send(httpRequestUtil.getUserId(), progress);
|
|
|
- // System.out.println(transfer.getState()); // state: (完成 Completed, 失败 Failed)
|
|
|
- }
|
|
|
|
|
|
+ // state: (完成 Completed, 失败 Failed)
|
|
|
+ // System.out.println(transfer.getState());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- // [腾讯云COS] 上传对象
|
|
|
+ /**
|
|
|
+ * [腾讯云COS] 上传对象
|
|
|
+ * - 如果对象是图片,则同时产生缩略图
|
|
|
+ */
|
|
|
@Override
|
|
|
public UploadResult putObject(MultipartFile multipartFile, String object_key) {
|
|
|
|
|
@@ -121,25 +118,49 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
try {
|
|
|
|
|
|
// 如果不传 object_key,则默认使用临时文件夹 (/temp) + MD5文件名
|
|
|
-
|
|
|
- String filename = CommonUtil.generateUUIDFilename(multipartFile);
|
|
|
- if (StrUtil.isEmpty(object_key)) {
|
|
|
- object_key = "temp/" + filename;
|
|
|
- }
|
|
|
+ String filename = FilenameUtil.generateUUIDFilename(multipartFile);
|
|
|
+ if (StrUtil.isEmpty(object_key)) object_key = "temp/" + filename;
|
|
|
System.out.println("Upload object: " + object_key);
|
|
|
|
|
|
- // 创建 PutObjectRequest 对象,PutObject 请求。(异步)
|
|
|
+ // [配置]
|
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
|
+ // 设置流大小 (只有设置了才有进度)
|
|
|
metadata.setContentLength(multipartFile.getSize());
|
|
|
+
|
|
|
+// // [图片处理] -----------------------------------------------------------
|
|
|
+//// // 设置请求头规则 (基础图片处理) (当 fileid 与 ObjectKey 相同时,处理后的图片将覆盖原图存储;不相同时将分别存储原图和处理后图片)
|
|
|
+//// // https://cloud.tencent.com/document/product/460/6929
|
|
|
+//// // https://github.com/tencentyun/cos-java-sdk-v5/blob/master/src/main/java/com/qcloud/cos/demo/ci/BasicImageProcessing.java
|
|
|
+// /*
|
|
|
+// [支持处理的原图格式]
|
|
|
+// - 普通格式: jpg/jpeg、png、bmp、webp、tiff、gif
|
|
|
+// - 高级格式: avif、heif/heic、tpg、apng、psd、svg、raw、ai、eps、dng、cr3、crw、mos、erf、3fr、fff、kdc、dcr、rw2、pef、sr2、srf、arw、nef、nrw、orf、mef、mrw
|
|
|
+// */
|
|
|
+// // 判断图片类型
|
|
|
+// List<String> imageType = Arrays.asList("jpg", "jpeg", "png", "bmp", "webp", "tiff", "gif");
|
|
|
+// boolean isImage = imageType.contains(FilenameUtil.getFilenameSuffix(multipartFile, false).toLowerCase());
|
|
|
+// if (isImage) {
|
|
|
+// // 选用样式1,并限定缩略图的宽高最小值为100×100 绝对质量为60
|
|
|
+// String rule = "imageView2/1/w/100/h/100/q/60";
|
|
|
+//// String rule = "imageMogr2/thumbnail/!50p";
|
|
|
+// String object_key_thumb = "/" + FilenameUtil.insertThumbSuffix(object_key);
|
|
|
+// String object_key_thumb_encode = URLEncoder.encode(object_key_thumb, "UTF-8");
|
|
|
+// String picOperations = "{\"is_pic_info\":1,\"rules\":[{\"fileid\":\"" + object_key_thumb_encode + "\",\"rule\":\"" + rule + "\"}]}";
|
|
|
+// System.out.println("picOperations = " + picOperations);
|
|
|
+// metadata.setHeader("Pic-Operations", picOperations);
|
|
|
+// }
|
|
|
+// // --------------------------------------------------------------------
|
|
|
+
|
|
|
+ // 创建 PutObjectRequest 对象,PutObject 请求。(异步)
|
|
|
PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, object_key, multipartFile.getInputStream(), metadata);
|
|
|
|
|
|
// 普通上传 (没有进度)
|
|
|
-// cosClient.putObject(putObjectRequest);
|
|
|
+// // cosClient.putObject(putObjectRequest);
|
|
|
|
|
|
// 高级上传
|
|
|
TransferManager transferManager = TencentCosUtil.createTransferManager(cosClient);
|
|
|
Upload upload = transferManager.upload(putObjectRequest); // 返回一个异步结果Upload
|
|
|
- showTransferProgress(filename, upload); // 查询上传进度,直到上传结束
|
|
|
+ showTransferProgress(filename, upload); // 查询上传进度,直到上传结束
|
|
|
UploadResult uploadResult = upload.waitForUploadResult(); // 捕获可能出现的异常
|
|
|
return uploadResult;
|
|
|
|
|
@@ -215,6 +236,7 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
public void deleteObject(String object_key) {
|
|
|
COSClient cosClient = getClient();
|
|
|
cosClient.deleteObject(BUCKET_NAME, object_key);
|
|
|
+ // cosClient.deleteObject(BUCKET_NAME, FilenameUtil.insertThumbSuffix(object_key));
|
|
|
cosClient.shutdown();
|
|
|
}
|
|
|
|