|
@@ -1,10 +1,16 @@
|
|
|
package com.backendsys.modules.sdk.douyincloud.tos.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.backendsys.exception.CustException;
|
|
|
+import com.backendsys.modules.common.config.security.utils.SecurityUtil;
|
|
|
import com.backendsys.modules.common.utils.CommonUtil;
|
|
|
import com.backendsys.modules.sdk.douyincloud.tos.service.DouyinTosService;
|
|
|
+import com.backendsys.modules.sdk.tencentcloud.cos.entity.Progress;
|
|
|
+import com.backendsys.modules.sse.entity.SseResponse;
|
|
|
+import com.backendsys.modules.sse.entity.SseResponseEnum;
|
|
|
+import com.backendsys.modules.sse.utils.SseUtil;
|
|
|
import com.backendsys.modules.upload.entity.SysUploadResult;
|
|
|
import com.volcengine.tos.TOSV2;
|
|
|
import com.volcengine.tos.TOSV2ClientBuilder;
|
|
@@ -14,26 +20,41 @@ import com.volcengine.tos.comm.event.DataTransferListener;
|
|
|
import com.volcengine.tos.comm.event.DataTransferStatus;
|
|
|
import com.volcengine.tos.comm.event.DataTransferType;
|
|
|
import com.volcengine.tos.model.object.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
|
// 对象接口 -> 上传对象
|
|
|
// https://www.volcengine.com/docs/6349/79898
|
|
|
@Service
|
|
|
public class DouyinTosServiceImpl implements DouyinTosService {
|
|
|
|
|
|
- private String REGION = "cn-beijing";
|
|
|
- private String ENDPOINT = "tos-cn-beijing.volces.com";
|
|
|
- private String DOMAIN = "https://tt8742f1ff1875cd2201-env-33n84bi3xr.tos-cn-beijing.volces.com";
|
|
|
- private String BUCKET_NAME = "tt8742f1ff1875cd2201-env-33n84bi3xr";
|
|
|
- private String ACCESS_KEY_ID = "AKLTNmFiMjBjMTZjZDYxNDlhZmI2OTc2M2U4MWZlODhhNGY";
|
|
|
- private String SECRET_ACCESS_KEY = "TVRjME9UUmhabUZtWW1JNU5EWXhORGhoWVdJME16Z3pNbUprWkRKa1lqTQ==";
|
|
|
+ @Autowired
|
|
|
+ private SseUtil sseUtil;
|
|
|
+
|
|
|
+ @Value("${spring.application.name}")
|
|
|
+ private String APPLICATION_NAME;
|
|
|
+ @Value("${douyin.tos.region}")
|
|
|
+ private String REGION;
|
|
|
+ @Value("${douyin.tos.endpoint}")
|
|
|
+ private String ENDPOINT;
|
|
|
+ @Value("${douyin.tos.domain}")
|
|
|
+ private String DOMAIN;
|
|
|
+ @Value("${douyin.tos.bucket-name}")
|
|
|
+ private String BUCKET_NAME;
|
|
|
+ @Value("${douyin.tos.access-key-id}")
|
|
|
+ private String ACCESS_KEY_ID;
|
|
|
+ @Value("${douyin.tos.secret-access-key}")
|
|
|
+ private String SECRET_ACCESS_KEY;
|
|
|
|
|
|
|
|
|
// [抖音云COS] 获取进度函数
|
|
|
- private static DataTransferListener getDataTransferListener() {
|
|
|
+ private DataTransferListener getDataTransferListener(String filename) {
|
|
|
+
|
|
|
return new DataTransferListener() {
|
|
|
// 自定义实现 DataTransferListener 的 dataTransferStatusChange 接口
|
|
|
@Override
|
|
@@ -42,24 +63,36 @@ public class DouyinTosServiceImpl implements DouyinTosService {
|
|
|
long once = dataTransferStatus.getRwOnceBytes();
|
|
|
long current = dataTransferStatus.getConsumedBytes();
|
|
|
long total = dataTransferStatus.getTotalBytes();
|
|
|
+ String state = "";
|
|
|
+ BigDecimal percentage = Convert.toBigDecimal(0);
|
|
|
|
|
|
if (dataTransferStatus.getType() == DataTransferType.DATA_TRANSFER_STARTED) {
|
|
|
+ state = "init";
|
|
|
System.out.println("putObject started.");
|
|
|
} else if (dataTransferStatus.getType() == DataTransferType.DATA_TRANSFER_RW) {
|
|
|
-
|
|
|
+ state = "inprogress";
|
|
|
// 计算百分比
|
|
|
- double percentage = NumberUtil.mul(current, 100) / total;
|
|
|
+ percentage = NumberUtil.round(NumberUtil.mul(current, 100) / total, 2);
|
|
|
System.out.printf("putObject progress: [%d / %d] = %.02f%% \n", current, total, percentage);
|
|
|
-
|
|
|
-// System.out.printf("putObject, send %d bytes once, has sent %d bytes, total %d bytes.\n",
|
|
|
-// dataTransferStatus.getRwOnceBytes(), dataTransferStatus.getConsumedBytes(),
|
|
|
-// dataTransferStatus.getTotalBytes());
|
|
|
-
|
|
|
} else if (dataTransferStatus.getType() == DataTransferType.DATA_TRANSFER_FAILED) {
|
|
|
+ state = "completed";
|
|
|
System.out.printf("putObject failed, has sent %d bytes, total %d bytes.\n", dataTransferStatus.getConsumedBytes(), total);
|
|
|
} else if (dataTransferStatus.getType() == DataTransferType.DATA_TRANSFER_SUCCEED) {
|
|
|
+ state = "completed";
|
|
|
System.out.printf("putObject succeed, has sent %d bytes, total %d bytes.\n", dataTransferStatus.getConsumedBytes(), total);
|
|
|
}
|
|
|
+
|
|
|
+ // [SSE] 进度回传
|
|
|
+ Progress progress = new Progress();
|
|
|
+ progress.setFilename(filename);
|
|
|
+ progress.setCurrent(current);
|
|
|
+ progress.setTotal(total);
|
|
|
+ progress.setPercent(percentage);
|
|
|
+ progress.setState(state);
|
|
|
+ String dataStr = (new SseResponse(SseResponseEnum.UPLOAD, progress)).toJsonStr();
|
|
|
+ String emitterKey = APPLICATION_NAME + "-userid-" + Convert.toStr(SecurityUtil.getUserId());
|
|
|
+ sseUtil.send(emitterKey, dataStr);
|
|
|
+
|
|
|
}
|
|
|
};
|
|
|
}
|
|
@@ -97,8 +130,9 @@ public class DouyinTosServiceImpl implements DouyinTosService {
|
|
|
// 在 ObjectMetaRequestOptions 中设置文件大小,可在进度条中显示 total 总长度,否则 DataTransferStatus.getTotalBytes 值为 -1。
|
|
|
ObjectMetaRequestOptions options = new ObjectMetaRequestOptions().setContentLength(multipartFile.getSize());
|
|
|
putObjectInput.setOptions(options);
|
|
|
+
|
|
|
// 自定义实现 DataTransferListener,实现进度条功能
|
|
|
- DataTransferListener listener = getDataTransferListener();
|
|
|
+ DataTransferListener listener = getDataTransferListener(filename);
|
|
|
putObjectInput.setDataTransferListener(listener);
|
|
|
|
|
|
// 上传对象
|