|
@@ -1,5 +1,6 @@
|
|
package com.backendsys.modules.sdk.tencent.cos.service.impl;
|
|
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.io.FileUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
@@ -8,11 +9,12 @@ import com.backendsys.entity.System.SysFile.SysFileDTO;
|
|
import com.backendsys.entity.Tencent.TencentCos.MultipartUploadRespDTO;
|
|
import com.backendsys.entity.Tencent.TencentCos.MultipartUploadRespDTO;
|
|
import com.backendsys.exception.CustException;
|
|
import com.backendsys.exception.CustException;
|
|
import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
|
|
import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
|
|
|
|
+import com.backendsys.modules.common.utils.CommonUtil;
|
|
import com.backendsys.modules.sdk.tencent.cos.entity.Progress;
|
|
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;
|
|
import com.backendsys.modules.sdk.tencent.cos.service.TencentCosService;
|
|
import com.backendsys.modules.sdk.tencent.cos.utils.TencentCosUtil;
|
|
import com.backendsys.modules.sdk.tencent.cos.utils.TencentCosUtil;
|
|
import com.backendsys.modules.sse.utils.SseUtil;
|
|
import com.backendsys.modules.sse.utils.SseUtil;
|
|
-import com.backendsys.utils.CommonUtil;
|
|
|
|
import com.qcloud.cos.COSClient;
|
|
import com.qcloud.cos.COSClient;
|
|
import com.qcloud.cos.ClientConfig;
|
|
import com.qcloud.cos.ClientConfig;
|
|
import com.qcloud.cos.auth.BasicCOSCredentials;
|
|
import com.qcloud.cos.auth.BasicCOSCredentials;
|
|
@@ -76,12 +78,14 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
}
|
|
}
|
|
|
|
|
|
// [腾讯云COS][高级接口] 获取进度函数
|
|
// [腾讯云COS][高级接口] 获取进度函数
|
|
- private void showTransferProgress(String md5, Transfer transfer) {
|
|
|
|
- // 这里的 Transfer 是异步上传结果 Upload 的父类
|
|
|
|
- System.out.println(transfer.getDescription());
|
|
|
|
|
|
+ private void showTransferProgress(String filename, Transfer transfer) {
|
|
|
|
+
|
|
|
|
+ Progress progress = new Progress();
|
|
|
|
+ progress.setState("Init");
|
|
|
|
+ sseUtil.send(httpRequestUtil.getUserId(), progress);
|
|
|
|
+
|
|
// 查询上传是否已经完成
|
|
// 查询上传是否已经完成
|
|
while (transfer.isDone() == false) {
|
|
while (transfer.isDone() == false) {
|
|
-
|
|
|
|
// 每秒获取一次进度
|
|
// 每秒获取一次进度
|
|
try { Thread.sleep(1000); } catch (InterruptedException e) { return; }
|
|
try { Thread.sleep(1000); } catch (InterruptedException e) { return; }
|
|
|
|
|
|
@@ -89,20 +93,24 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
long current = transferProgress.getBytesTransferred();
|
|
long current = transferProgress.getBytesTransferred();
|
|
long total = transferProgress.getTotalBytesToTransfer();
|
|
long total = transferProgress.getTotalBytesToTransfer();
|
|
double percent = transferProgress.getPercentTransferred();
|
|
double percent = transferProgress.getPercentTransferred();
|
|
- System.out.printf("upload Progress: [%d / %d] = %.02f%%\n", current, total, percent);
|
|
|
|
|
|
+ String state = Convert.toStr(transfer.getState());
|
|
|
|
+ System.out.printf("Upload progress: [%d / %d] = %.02f%% (%s)\n", current, total, percent, state);
|
|
|
|
|
|
- Progress progress = new Progress();
|
|
|
|
- progress.setCurrent(current);
|
|
|
|
- progress.setTotal(total);
|
|
|
|
- progress.setPercent(percent);
|
|
|
|
- sseUtil.send(httpRequestUtil.getUserId(), progress);
|
|
|
|
|
|
+ ProgressData progressData = new ProgressData();
|
|
|
|
+ progressData.setFilename(filename);
|
|
|
|
+ progressData.setCurrent(current);
|
|
|
|
+ progressData.setTotal(total);
|
|
|
|
+ progressData.setPercent(percent);
|
|
|
|
|
|
|
|
+ progress.setState(state);
|
|
|
|
+ progress.setData(progressData);
|
|
|
|
+ sseUtil.send(httpRequestUtil.getUserId(), progress);
|
|
|
|
+ // System.out.println(transfer.getState()); // state: (完成 Completed, 失败 Failed)
|
|
}
|
|
}
|
|
- // 完成了 Completed,或者失败了 Failed
|
|
|
|
- System.out.println(transfer.getState());
|
|
|
|
- sseUtil.send(httpRequestUtil.getUserId(), transfer.getState());
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
// [腾讯云COS] 上传对象
|
|
// [腾讯云COS] 上传对象
|
|
@Override
|
|
@Override
|
|
public UploadResult putObject(MultipartFile multipartFile, String object_key) {
|
|
public UploadResult putObject(MultipartFile multipartFile, String object_key) {
|
|
@@ -113,15 +121,12 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
try {
|
|
try {
|
|
|
|
|
|
// 如果不传 object_key,则默认使用临时文件夹 (/temp) + MD5文件名
|
|
// 如果不传 object_key,则默认使用临时文件夹 (/temp) + MD5文件名
|
|
- String md5 = DigestUtil.md5Hex(multipartFile.getInputStream());
|
|
|
|
|
|
+
|
|
|
|
+ String filename = CommonUtil.generateUUIDFilename(multipartFile);
|
|
if (StrUtil.isEmpty(object_key)) {
|
|
if (StrUtil.isEmpty(object_key)) {
|
|
- String suffix = FileUtil.extName(multipartFile.getOriginalFilename()).toLowerCase();
|
|
|
|
- String filename = md5 + "." + suffix;
|
|
|
|
object_key = "temp/" + filename;
|
|
object_key = "temp/" + filename;
|
|
}
|
|
}
|
|
-
|
|
|
|
- URL url = generateUrl(object_key, 15);
|
|
|
|
- System.out.println("url = " + url);
|
|
|
|
|
|
+ System.out.println("Upload object: " + object_key);
|
|
|
|
|
|
// 创建 PutObjectRequest 对象,PutObject 请求。(异步)
|
|
// 创建 PutObjectRequest 对象,PutObject 请求。(异步)
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
@@ -134,7 +139,7 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
// 高级上传
|
|
// 高级上传
|
|
TransferManager transferManager = TencentCosUtil.createTransferManager(cosClient);
|
|
TransferManager transferManager = TencentCosUtil.createTransferManager(cosClient);
|
|
Upload upload = transferManager.upload(putObjectRequest); // 返回一个异步结果Upload
|
|
Upload upload = transferManager.upload(putObjectRequest); // 返回一个异步结果Upload
|
|
- showTransferProgress(md5, upload); // 查询上传进度,直到上传结束
|
|
|
|
|
|
+ showTransferProgress(filename, upload); // 查询上传进度,直到上传结束
|
|
UploadResult uploadResult = upload.waitForUploadResult(); // 捕获可能出现的异常
|
|
UploadResult uploadResult = upload.waitForUploadResult(); // 捕获可能出现的异常
|
|
return uploadResult;
|
|
return uploadResult;
|
|
|
|
|