|
@@ -1,5 +1,6 @@
|
|
|
package com.tzld.piaoquan.api.util;
|
|
package com.tzld.piaoquan.api.util;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
@@ -33,6 +34,7 @@ import com.tzld.piaoquan.api.model.vo.SignatureVO;
|
|
|
import com.tzld.piaoquan.api.model.vo.StsTokenVO;
|
|
import com.tzld.piaoquan.api.model.vo.StsTokenVO;
|
|
|
import com.tzld.piaoquan.growth.common.utils.DateUtils;
|
|
import com.tzld.piaoquan.growth.common.utils.DateUtils;
|
|
|
import com.tzld.piaoquan.growth.common.utils.RandomUtil;
|
|
import com.tzld.piaoquan.growth.common.utils.RandomUtil;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -63,6 +65,7 @@ import java.util.*;
|
|
|
* @version 1.0.0
|
|
* @version 1.0.0
|
|
|
*/
|
|
*/
|
|
|
@Component
|
|
@Component
|
|
|
|
|
+@Slf4j
|
|
|
public class AliOssFileTool extends AliOssConfig {
|
|
public class AliOssFileTool extends AliOssConfig {
|
|
|
|
|
|
|
|
private static Logger logger = Logger.getLogger(AliOssFileTool.class);
|
|
private static Logger logger = Logger.getLogger(AliOssFileTool.class);
|
|
@@ -100,6 +103,14 @@ public class AliOssFileTool extends AliOssConfig {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static String saveInPublic(String bucketName, String ossKey, InputStream inputStream, String contentType) {
|
|
|
|
|
+ bucketName = getBucket(bucketName);
|
|
|
|
|
+ ObjectMetadata om = new ObjectMetadata();
|
|
|
|
|
+ om.setContentType(contentType);
|
|
|
|
|
+ getOssClient().putObject(bucketName, ossKey, inputStream, om);
|
|
|
|
|
+ return ossKey;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static String moveFile(EnumPublicBuckets sourceBucket, String sourceKey, EnumPublicBuckets targetBucket, String targetKey) {
|
|
public static String moveFile(EnumPublicBuckets sourceBucket, String sourceKey, EnumPublicBuckets targetBucket, String targetKey) {
|
|
|
getOssClient().copyObject(getBucket(sourceBucket.getBucketName()), sourceKey,
|
|
getOssClient().copyObject(getBucket(sourceBucket.getBucketName()), sourceKey,
|
|
|
getBucket(targetBucket.getBucketName()), targetKey);
|
|
getBucket(targetBucket.getBucketName()), targetKey);
|
|
@@ -1473,4 +1484,24 @@ public class AliOssFileTool extends AliOssConfig {
|
|
|
return policy;
|
|
return policy;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static String downloadAndSaveInOSS(String fileName, String url, String contentType) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (url.startsWith(CdnUtil.VIDEO_CDN_URL_HOST)
|
|
|
|
|
+ || url.startsWith(CdnUtil.DOWNLOAD_CDN_URL_HOST_PICTURE)) {
|
|
|
|
|
+ return url;
|
|
|
|
|
+ }
|
|
|
|
|
+ byte[] fileData = HttpUtil.downloadBytes(url);
|
|
|
|
|
+ if (fileData == null || fileData.length == 0) {
|
|
|
|
|
+ log.warn("downloadAndSaveInOSS 下载media失败,URL: {}", url);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(fileData);
|
|
|
|
|
+ return CdnUtil.getOssHttpUrl(saveInPublic(EnumPublicBuckets.PUBBUCKET.getBucketName(),
|
|
|
|
|
+ fileName, inputStream, contentType));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("downloadAndSaveInOSS 下载转存OSS失败,URL: {}", url, e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|