|
@@ -4,9 +4,9 @@ import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
-import com.chelvc.framework.base.config.HttpClientProperties;
|
|
|
+import com.chelvc.framework.base.util.HttpUtils;
|
|
|
import com.chelvc.framework.common.util.AssertUtils;
|
|
|
-import com.chelvc.framework.common.util.FileUtils;
|
|
|
+import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
|
import com.chelvc.framework.upload.UploadHandler;
|
|
|
import com.chelvc.framework.upload.config.UploadProperties;
|
|
@@ -16,6 +16,12 @@ import com.qcloud.cos.auth.BasicCOSCredentials;
|
|
|
import com.qcloud.cos.http.HttpProtocol;
|
|
|
import com.qcloud.cos.model.ObjectMetadata;
|
|
|
import com.qcloud.cos.region.Region;
|
|
|
+import com.tencentcloudapi.cdn.v20180606.CdnClient;
|
|
|
+import com.tencentcloudapi.cdn.v20180606.models.PurgeUrlsCacheRequest;
|
|
|
+import com.tencentcloudapi.common.Credential;
|
|
|
+import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
|
|
+import com.tencentcloudapi.common.profile.ClientProfile;
|
|
|
+import com.tencentcloudapi.common.profile.HttpProfile;
|
|
|
import lombok.NonNull;
|
|
|
|
|
|
/**
|
|
@@ -25,63 +31,74 @@ import lombok.NonNull;
|
|
|
* @date 2024/1/30
|
|
|
*/
|
|
|
public class TencentUploadHandler implements UploadHandler {
|
|
|
- private final String path;
|
|
|
- private final String region;
|
|
|
- private final COSClient client;
|
|
|
+ private final String domain;
|
|
|
+ private final String directory;
|
|
|
+ private final COSClient cos;
|
|
|
+ private final CdnClient cdn;
|
|
|
|
|
|
- public TencentUploadHandler(@NonNull UploadProperties.Client properties, @NonNull HttpClientProperties httpClient) {
|
|
|
- this.path = AssertUtils.nonempty(properties.getPath(), () -> "Tencent upload path is missing");
|
|
|
- this.region = AssertUtils.nonempty(properties.getRegion(), () -> "Tencent upload region is missing");
|
|
|
+ public TencentUploadHandler(@NonNull UploadProperties properties) {
|
|
|
+ String appid = AssertUtils.nonempty(properties.getAppid(), () -> "Tencent upload appid is missing");
|
|
|
+ String secret = AssertUtils.nonempty(properties.getSecret(), () -> "Tencent upload secret is missing");
|
|
|
+ String region = AssertUtils.nonempty(properties.getRegion(), () -> "Tencent upload region is missing");
|
|
|
+ this.domain = AssertUtils.nonempty(properties.getDomain(), () -> "Tencent upload access domain is missing");
|
|
|
+ this.directory = AssertUtils.nonempty(properties.getDirectory(), () -> "Tencent upload directory is missing");
|
|
|
|
|
|
- // 构建客户端配置
|
|
|
+ // 初始化COS客户端
|
|
|
ClientConfig config = new ClientConfig();
|
|
|
- config.setRegion(new Region(this.region));
|
|
|
+ config.setRegion(new Region(region));
|
|
|
config.setHttpProtocol(HttpProtocol.https);
|
|
|
- config.setMaxConnectionsCount(httpClient.getMaxTotal());
|
|
|
- config.setConnectionTimeout(httpClient.getConnectTimeout());
|
|
|
- config.setSocketTimeout(httpClient.getSocketTimeout());
|
|
|
- config.setConnectionRequestTimeout(httpClient.getConnectionRequestTimeout());
|
|
|
+ config.setMaxConnectionsCount(properties.getHttpclient().getMaxTotal());
|
|
|
+ config.setConnectionTimeout(properties.getHttpclient().getConnectTimeout());
|
|
|
+ config.setSocketTimeout(properties.getHttpclient().getSocketTimeout());
|
|
|
+ config.setConnectionRequestTimeout(properties.getHttpclient().getConnectionRequestTimeout());
|
|
|
+ this.cos = new COSClient(new BasicCOSCredentials(appid, secret), config);
|
|
|
|
|
|
- // 初始化客户端实例
|
|
|
- String appid = AssertUtils.nonempty(properties.getAppid(), () -> "Tencent upload appid is missing");
|
|
|
- String secret = AssertUtils.nonempty(properties.getSecret(), () -> "Tencent upload secret is missing");
|
|
|
- this.client = new COSClient(new BasicCOSCredentials(appid, secret), config);
|
|
|
+ // 初始化CDN客户端
|
|
|
+ HttpProfile http = new HttpProfile();
|
|
|
+ http.setConnTimeout(properties.getHttpclient().getConnectTimeout());
|
|
|
+ http.setReadTimeout(properties.getHttpclient().getSocketTimeout());
|
|
|
+ ClientProfile profile = new ClientProfile();
|
|
|
+ profile.setHttpProfile(http);
|
|
|
+ Credential credential = new Credential(appid, secret);
|
|
|
+ this.cdn = new CdnClient(credential, StringUtils.EMPTY, profile);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 将文件名称转换成URL地址
|
|
|
- *
|
|
|
- * @param filename 文件名称
|
|
|
- * @return URL地址
|
|
|
- */
|
|
|
- private String name2url(String filename) {
|
|
|
- return "https://" + this.path + ".cos." + this.region + ".myqcloud.com/" + filename;
|
|
|
+ @Override
|
|
|
+ public void refreshContentDeliveryNetwork(@NonNull String... urls) {
|
|
|
+ if (ObjectUtils.isEmpty(urls)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ PurgeUrlsCacheRequest request = new PurgeUrlsCacheRequest();
|
|
|
+ request.setUrls(urls);
|
|
|
+ request.setArea("mainland");
|
|
|
+ try {
|
|
|
+ this.cdn.PurgeUrlsCache(request);
|
|
|
+ } catch (TencentCloudSDKException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String upload(@NonNull File file) throws IOException {
|
|
|
- String suffix = FileUtils.getSuffix(file);
|
|
|
- String filename = StringUtils.uuid() + StringUtils.ifEmpty(suffix, name -> "." + name, StringUtils.EMPTY);
|
|
|
- this.client.putObject(this.path, filename, file);
|
|
|
- return this.name2url(filename);
|
|
|
+ public String upload(@NonNull File file, @NonNull String filename) throws IOException {
|
|
|
+ this.cos.putObject(this.directory, filename, file);
|
|
|
+ return HttpUtils.url(this.domain, filename);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String upload(@NonNull InputStream stream, String suffix) throws IOException {
|
|
|
- return this.upload(stream, suffix, stream.available());
|
|
|
+ public String upload(@NonNull InputStream stream, @NonNull String filename) throws IOException {
|
|
|
+ return this.upload(stream, filename, stream.available());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String upload(@NonNull InputStream stream, String suffix, long length) throws IOException {
|
|
|
+ public String upload(@NonNull InputStream stream, @NonNull String filename, long length) throws IOException {
|
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
|
metadata.setContentLength(length);
|
|
|
- String filename = StringUtils.uuid() + StringUtils.ifEmpty(suffix, name -> "." + name, StringUtils.EMPTY);
|
|
|
- this.client.putObject(this.path, filename, stream, metadata);
|
|
|
- return this.name2url(filename);
|
|
|
+ this.cos.putObject(this.directory, filename, stream, metadata);
|
|
|
+ return HttpUtils.url(this.domain, filename);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void destroy() {
|
|
|
- this.client.shutdown();
|
|
|
+ this.cos.shutdown();
|
|
|
}
|
|
|
}
|