SDKTencentCOSServiceImpl.java 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. package com.backendsys.service.SDKService.SDKTencent;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.crypto.digest.DigestUtil;
  4. import cn.hutool.http.HttpUtil;
  5. import cn.hutool.json.JSONObject;
  6. import com.backendsys.aspect.HttpRequestAspect;
  7. import com.backendsys.entity.SDKEntity.TencentCOSCredentialsDTO;
  8. import com.backendsys.entity.System.SysFile.SysFileCategoryDTO;
  9. import com.backendsys.entity.System.SysFile.SysFileDTO;
  10. import com.backendsys.entity.Tencent.TencentCos.MultipartUploadRespDTO;
  11. import com.backendsys.entity.Tencent.TencentCos.MultipartUploadDTO;
  12. import com.backendsys.entity.Tencent.TencentCos.TempCredentialsDTO;
  13. import com.backendsys.entity.Tencent.TencentCos.UploadOriginDTO;
  14. import com.backendsys.entity.Tencent.TencentCos.UploadOriginResp;
  15. import com.backendsys.exception.CustException;
  16. //import com.backendsys.mapper.System.SysFileCategoryMapper;
  17. //import com.backendsys.mapper.System.SysFileMapper;
  18. //import com.backendsys.service.System.SysFileService;
  19. import com.backendsys.utils.CommonUtil;
  20. import com.backendsys.utils.MapUtil;
  21. import com.qcloud.cos.COSClient;
  22. import com.qcloud.cos.ClientConfig;
  23. import com.qcloud.cos.auth.BasicCOSCredentials;
  24. import com.qcloud.cos.auth.COSCredentials;
  25. import com.qcloud.cos.exception.CosServiceException;
  26. import com.qcloud.cos.model.*;
  27. import com.qcloud.cos.region.Region;
  28. import com.qcloud.cos.transfer.TransferManager;
  29. import com.qcloud.cos.utils.IOUtils;
  30. import com.tencent.cloud.CosStsClient;
  31. import com.tencent.cloud.Response;
  32. import net.coobird.thumbnailator.Thumbnails;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.beans.factory.annotation.Value;
  35. import org.springframework.core.ParameterizedTypeReference;
  36. import org.springframework.http.HttpHeaders;
  37. import org.springframework.http.HttpMethod;
  38. import org.springframework.http.MediaType;
  39. import org.springframework.stereotype.Service;
  40. import org.springframework.util.StringUtils;
  41. import org.springframework.web.multipart.MultipartFile;
  42. import org.springframework.web.reactive.function.client.WebClient;
  43. import org.springframework.web.util.UriComponentsBuilder;
  44. import java.io.*;
  45. import java.net.*;
  46. import java.nio.file.Path;
  47. import java.nio.file.Paths;
  48. import java.util.*;
  49. import java.util.concurrent.CompletableFuture;
  50. import java.util.concurrent.ExecutorService;
  51. import java.util.concurrent.Executors;
  52. import java.util.regex.Matcher;
  53. import java.util.regex.Pattern;
  54. @Service
  55. public class SDKTencentCOSServiceImpl implements SDKTencentCOSService {
  56. @Autowired
  57. private HttpRequestAspect httpRequestAspect;
  58. @Value("${tencent.cos.secret-id-temp}")
  59. private String secretIdTemp;
  60. @Value("${tencent.cos.secret-key}-temp")
  61. private String secretKeyTemp;
  62. @Value("${tencent.cos.secret-id}")
  63. private String secretId;
  64. @Value("${tencent.cos.secret-key}")
  65. private String secretKey;
  66. @Value("${tencent.cos.region}")
  67. private String region;
  68. @Value("${tencent.cos.bucket-name}")
  69. private String bucketName;
  70. @Value("${tencent.cos.accessible-domain}")
  71. private String accessibleDomain;
  72. @Value("${tencent.cos.max-size}")
  73. private long maxSize;
  74. @Autowired
  75. private SDKTencentService sdkTencentService;
  76. // @Autowired
  77. // private SysFileMapper sysFileMapper;
  78. // @Autowired
  79. // private SysFileService sysFileService;
  80. //
  81. // @Autowired
  82. // private SysFileCategoryMapper sysFileCategoryMapper;
  83. /**
  84. * 生成cos客户端
  85. * COSClient cosClient = getClient(secretId, secretKey);
  86. */
  87. private COSClient getClient(String secretId, String secretKey) {
  88. // 初始化用户身份信息(secretId, secretKey)
  89. COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
  90. // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
  91. Region region1 = new Region(region);
  92. ClientConfig clientConfig = new ClientConfig(region1);
  93. clientConfig.setRegion(region1);
  94. // 生成cos客户端
  95. COSClient cosClient = new COSClient(cred, clientConfig);
  96. return cosClient;
  97. }
  98. // /**
  99. // * 临时密钥生成及使用指引
  100. // * https://www.tencentcloud.com/zh/document/product/436/14048
  101. // */
  102. // @Override
  103. // public TempCredentialsDTO getTempCredentials(String allowPrefix) {
  104. //
  105. // TreeMap<String, Object> KLingConfig = new TreeMap<String, Object>();
  106. // try {
  107. //
  108. // // 密钥,使用子账号密钥,授权遵循最小权限指引
  109. // KLingConfig.put("secretId", secretId);
  110. // KLingConfig.put("secretKey", secretKey);
  111. //
  112. // // 临时密钥有效时长,单位是秒,默认 120 秒,目前主账号最长 2 小时(即 7200 秒),子账号最长 36 小时(即 129600)秒
  113. // Integer durationSeconds = 120;
  114. // KLingConfig.put("durationSeconds", durationSeconds);
  115. // KLingConfig.put("bucket", bucketName);
  116. // KLingConfig.put("region", region);
  117. //
  118. // // 获取当前时间戳(毫秒)
  119. // long nowDateTime = System.currentTimeMillis() / 1000L;
  120. // // 计算1800秒后的时间戳
  121. // long expireDateTime = nowDateTime + durationSeconds;
  122. //
  123. // // 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子:a.jpg 或者 a/* 或者 * 。
  124. // // 如果填写了“*”,将允许用户访问所有资源;除非业务需要,否则请按照最小权限原则授予用户相应的访问权限范围。
  125. // KLingConfig.put("allowPrefix", allowPrefix);
  126. //
  127. // // 密钥的权限列表。简单上传、表单上传和分片上传需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
  128. // String[] allowActions = new String[] {
  129. // //简单上传操作
  130. // "name/cos:PutObject",
  131. // //表单上传对象
  132. // "name/cos:PostObject",
  133. // //分块上传:初始化分块操作
  134. // "name/cos:InitiateMultipartUpload",
  135. // //分块上传:List 进行中的分块上传
  136. // "name/cos:ListMultipartUploads",
  137. // //分块上传:List 已上传分块操作
  138. // "name/cos:ListParts",
  139. // //分块上传:上传分块操作
  140. // "name/cos:UploadPart",
  141. // //分块上传:完成所有分块上传操作
  142. // "name/cos:CompleteMultipartUpload",
  143. // //取消分块上传操作
  144. // "name/cos:AbortMultipartUpload"
  145. // };
  146. // // "name/cos:*"
  147. //
  148. // KLingConfig.put("allowActions", allowActions);
  149. //
  150. // // -- 在后台 存储桶->设置策略 -------------------------------------------------------------
  151. //
  152. // // 限制上传文件的大小(cos:content-length)
  153. // // 限制上传文件的类型(cos:content-type)
  154. // // https://cloud.tencent.com/document/product/436/71307#content-length
  155. //
  156. //
  157. //
  158. // Response response = CosStsClient.getCredential(KLingConfig);
  159. //
  160. // TempCredentialsDTO dto = new TempCredentialsDTO();
  161. // dto.setRegion(region);
  162. // dto.setBucket(bucketName);
  163. // dto.setAllow_prefix(allowPrefix.replace("*", ""));
  164. // dto.setTmp_secret_id(response.credentials.tmpSecretId);
  165. // dto.setTmp_secret_key(response.credentials.tmpSecretKey);
  166. // dto.setSession_token(response.credentials.sessionToken);
  167. // dto.setStart_time(nowDateTime);
  168. // dto.setExpired_time(expireDateTime);
  169. // dto.setDuration_seconds(durationSeconds);
  170. //
  171. // return dto;
  172. //
  173. // } catch (Exception e) {
  174. // e.printStackTrace();
  175. // throw new CustException(e.getMessage());
  176. // }
  177. //
  178. // }
  179. // /**
  180. // * [储存桶操作] 获得当前储存桶的文件列表
  181. // * https://cloud.tencent.com/document/product/436/65938
  182. // */
  183. // @Override
  184. // public Map<String, Object> getBucketList(String next_marker) {
  185. // COSClient cosClient = getClient(secretId, secretKey);
  186. //
  187. // ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
  188. // // 设置 bucket 名称
  189. // listObjectsRequest.setBucketName(bucketName);
  190. //
  191. //
  192. //
  193. //
  194. //
  195. // // 设置列出的对象名以 prefix 为前缀
  196. // listObjectsRequest.setPrefix("/materials/1/");
  197. //// listObjectsRequest.setPrefix("");
  198. //
  199. //
  200. //
  201. //
  202. //
  203. // // 设置最大列出多少个对象, 一次 listobject 最大支持1000
  204. // listObjectsRequest.setMaxKeys(5);
  205. //
  206. //
  207. // // 设置被截断开始的位置
  208. // if (next_marker != null) {
  209. // listObjectsRequest.setMarker(next_marker);
  210. // }
  211. //
  212. // // 保存列出的结果
  213. // ObjectListing objectListing = null;
  214. //
  215. // try {
  216. // objectListing = cosClient.listObjects(listObjectsRequest);
  217. // System.out.println(objectListing);
  218. //
  219. // } catch (CosServiceException e) {
  220. // e.printStackTrace();
  221. // }
  222. //
  223. // // object summary 表示此次列出的对象列表
  224. // List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
  225. // System.out.println(cosObjectSummaries);
  226. //
  227. // List<Map<String, Object>> list = new ArrayList<>();
  228. //
  229. // for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {
  230. //
  231. // // 对象的 key
  232. // String key = cosObjectSummary.getKey();
  233. // // 对象的 etag
  234. // String etag = cosObjectSummary.getETag();
  235. // // 对象的长度
  236. // long fileSize = cosObjectSummary.getSize();
  237. // // 对象的存储类型
  238. // String storageClasses = cosObjectSummary.getStorageClass();
  239. //
  240. // System.out.println("key = " + key);
  241. // System.out.println("etag = " + etag);
  242. // System.out.println("fileSize = " + fileSize);
  243. // System.out.println("storageClasses = " + storageClasses);
  244. // System.out.println("------------------------------------------");
  245. //
  246. // Map<String, Object> map = new LinkedHashMap<>();
  247. // map.put("key", cosObjectSummary.getKey());
  248. // map.put("etag", cosObjectSummary.getETag());
  249. // map.put("file_size", cosObjectSummary.getSize());
  250. // map.put("storage_classes", cosObjectSummary.getStorageClass());
  251. // list.add(map);
  252. //
  253. // }
  254. //
  255. // String nextMarker = null;
  256. // if (objectListing.isTruncated()) {
  257. // // 表示还没有列完,被截断了
  258. // // 下一次开始的位置
  259. // nextMarker = objectListing.getNextMarker();
  260. // System.out.println("isTruncated! nextMarker:");
  261. // System.out.println(nextMarker);
  262. // }
  263. //
  264. // Map<String, Object> result = new LinkedHashMap<>();
  265. // result.put("next_marker", nextMarker);
  266. // result.put("list", list);
  267. //
  268. // return result;
  269. // }
  270. /**
  271. * 在调用 [AI数智人-定制接口] 前,需要将素材文件上传到指定的云端存储。
  272. * - 通过接口获取上传临时令牌,接口返回令牌后,由前端上传对应的文件到对象存储
  273. */
  274. /**
  275. * 文档中心 > 腾讯云智能数智人 > API 接入 > 2D真人小样本形象定制 API 文档-v0.2.1 > 上传素材文件到云端存储 > 获取上传临时令牌
  276. * https://cloud.tencent.com/document/product/1240/96067
  277. */
  278. /**
  279. * 获取上传临时令牌 (返回远程参数)
  280. */
  281. private Map<String, Object> getIvhTempCredentialsOrigin(String appKey, String accessToken) {
  282. // 获得 Signature 签名
  283. Map<String, Object> signature = sdkTencentService.getSignature(appKey, accessToken);
  284. // 构建带参数的 URI (注意参数不要编码) (signature 由于会带+号所以需要改成这种写法)
  285. String url = "https://gw.tvs.qq.com/v2/ivh/assetmanager/customservice/getuploadcredentials";
  286. URI uriWithParams = UriComponentsBuilder.fromUriString(url)
  287. .queryParam("appkey", signature.get("appkey"))
  288. .queryParam("signature", "{signature}")
  289. .queryParam("timestamp", signature.get("timestamp"))
  290. .build(signature.get("signature"));
  291. String jsonStr = "{\"Payload\": {}}";
  292. JSONObject bodyData = new JSONObject(jsonStr);
  293. // Http 远程调用
  294. Map<String, Object> resp = WebClient.create()
  295. .method(HttpMethod.POST)
  296. .uri(uriWithParams)
  297. .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
  298. .bodyValue(bodyData)
  299. .retrieve()
  300. .bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
  301. })
  302. .block();
  303. /*
  304. resp:
  305. {
  306. "Header": {
  307. "RequestID": "gze0f59a6c17104906483254895",
  308. "SessionID": "gze0f59a6c17104906483254896",
  309. "DialogID": "",
  310. "Code": 0,
  311. "Message": "ok"
  312. },
  313. "Payload": {
  314. "ExpiredTime": 1710494248,
  315. "PathPrefix": "https://virtualhuman-cos-prod-1251316161.cos.accelerate.myqcloud.com/customer-pipeline/884/70c53bfe-c0e1-481a-8ae3-4e76f03d2971/",
  316. "StartTime": 1710490648,
  317. "Credentials": {
  318. "Token": "owlVQ..",
  319. "TmpSecretId": "AKIDxTiqWc9i0bSsPJy2uduUCJOTxVvMYxrWiJTS0V4OtTEm_UQqiq-ECwf38Xtrtl44",
  320. "TmpSecretKey": "zmsLEogWD0lAY/SWHnjH8XfzrDs0Vicfd4RGECehBCE="
  321. }
  322. }
  323. }
  324. */
  325. return resp;
  326. }
  327. /**
  328. * 获取上传临时令牌 (返回格式化后的参数)
  329. */
  330. public TencentCOSCredentialsDTO getIvhTempCredentials(String appKey, String accessToken) {
  331. Map<String, Object> resp = getIvhTempCredentialsOrigin(appKey, accessToken);
  332. // 判断是否获取成功
  333. Integer respHeaderCode = (Integer) MapUtil.get(resp, "Header.Code");
  334. if (respHeaderCode != 0) {
  335. String respHeaderMessage = (String) MapUtil.get(resp, "Header.Message");
  336. throw new RuntimeException(respHeaderMessage);
  337. }
  338. TencentCOSCredentialsDTO credentialsDTO = new TencentCOSCredentialsDTO();
  339. credentialsDTO.setStart_time((Integer) MapUtil.get(resp, "Payload.StartTime"));
  340. credentialsDTO.setExpired_time((Integer) MapUtil.get(resp, "Payload.ExpiredTime"));
  341. credentialsDTO.setTmp_secret_id((String) MapUtil.get(resp, "Payload.Credentials.TmpSecretId"));
  342. credentialsDTO.setTmp_secret_key((String) MapUtil.get(resp, "Payload.Credentials.TmpSecretKey"));
  343. credentialsDTO.setSession_token((String) MapUtil.get(resp, "Payload.Credentials.Token"));
  344. // 通过 PathPrefix 获得 bucket 和 region
  345. String pathPrefix = (String) MapUtil.get(resp, "Payload.PathPrefix");
  346. Pattern pattern = Pattern.compile("https://([a-zA-Z0-9\\-]+)\\.cos\\.(\\w+)\\.myqcloud\\.com/.*");
  347. Matcher matcher = pattern.matcher(pathPrefix);
  348. if (matcher.find()) {
  349. credentialsDTO.setBucket(matcher.group(1));
  350. credentialsDTO.setRegion(matcher.group(2));
  351. }
  352. credentialsDTO.setPath_prefix(pathPrefix);
  353. int startIndex = pathPrefix.indexOf("customer-pipeline");
  354. String lastPath = pathPrefix.substring(startIndex);
  355. credentialsDTO.setKey_dir(lastPath);
  356. /*
  357. credentialsDTO:
  358. {
  359. "bucket": "virtualhuman-cos-prod-1251316161",
  360. "region": "accelerate",
  361. "path_prefix": "https://virtualhuman-cos-prod-1251316161.cos.accelerate.myqcloud.com/customer-pipeline/884/3e52ec21-02ea-403c-8d1c-ebb727ceac1f/",
  362. "key_dir": "customer-pipeline/884/3e52ec21-02ea-403c-8d1c-ebb727ceac1f/",
  363. "tmp_secret_id": "AKIDqv8LDYIWVdqq5PrNpGD9J4YY0p8QQ7qXe2ZNV78W_y9wXFGXe6z4wkRJu0OQ3jhE",
  364. "tmp_secret_key": "8TQWL6+YweYQc2yDCNBDe2/V/qdNZYhYczSS6QdUcwk=",
  365. "session_token": "czCmJWynL6j78eXE..",
  366. "start_time": 1712253511,
  367. "expired_time": 1712257111
  368. }
  369. */
  370. return credentialsDTO;
  371. }
  372. /**
  373. * 高级操作
  374. */
  375. public TransferManager createTransferManager(COSClient cosClient) {
  376. // 自定义线程池大小,建议在客户端与 COS 网络充足(例如使用腾讯云的 CVM,同地域上传 COS)的情况下,设置成16或32即可,可较充分的利用网络资源
  377. // 对于使用公网传输且网络带宽质量不高的情况,建议减小该值,避免因网速过慢,造成请求超时。
  378. ExecutorService threadPool = Executors.newFixedThreadPool(32);
  379. // 传入一个 threadpool, 若不传入线程池,默认 TransferManager 中会生成一个单线程的线程池。
  380. TransferManager transferManager = new TransferManager(cosClient, threadPool);
  381. return transferManager;
  382. }
  383. public void shutdownTransferManager(TransferManager transferManager) {
  384. // 指定参数为 true, 则同时会关闭 transferManager 内部的 COSClient 实例。
  385. // 指定参数为 false, 则不会关闭 transferManager 内部的 COSClient 实例。
  386. transferManager.shutdownNow(true);
  387. }
  388. // /**
  389. // * 压缩图片 (入参 InputStream,出参 File)
  390. // */
  391. // public File compressImage(InputStream inputStream, String suffix) throws IOException {
  392. //
  393. // System.out.println("suffix: " + suffix);
  394. //
  395. // // 将文件内容写入临时文件
  396. //// String tempFilename = CommonUtil.generateFilename(suffix);
  397. //// String tempFilenameThumb = tempFilename.replaceAll("." + suffix, "-thumb." + suffix);
  398. //
  399. //// System.out.println("tempFilename: " + tempFilename);
  400. //// System.out.println("tempFilenameThumb: " + tempFilenameThumb);
  401. // System.out.println("-------------------------------------------");
  402. //
  403. // File tempFile = File.createTempFile("tmp", "." + suffix);
  404. // File tempFileThumb = File.createTempFile("tmp", "." + suffix);
  405. //
  406. // System.out.println("tempFile: " + tempFile);
  407. // System.out.println("tempFileThumb: " + tempFileThumb);
  408. //
  409. // try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
  410. // System.out.println("IOUtils.copy start: ");
  411. // IOUtils.copy(inputStream, outputStream);
  412. // } catch (Exception e) {
  413. // System.out.println("IOUtils.copy Error:");
  414. // System.out.println(e);
  415. // }
  416. // // 创建缩略图
  417. // boolean isImage = Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "tiff", "webp").contains(suffix.toLowerCase());
  418. // if (isImage) {
  419. // File sourceFile = new File(tempFile.getPath());
  420. // File sourceFileThumb = new File(tempFileThumb.getPath());
  421. // // 使用Thumbnailator生成缩略图并写入到临时文件
  422. // System.out.println("Thumbnails start");
  423. // try {
  424. // System.out.println("sourceFile:");
  425. // System.out.println(sourceFile);
  426. // System.out.println("sourceFileThumb:");
  427. // System.out.println(sourceFileThumb);
  428. // Thumbnails.of(sourceFile).size(60, 60).outputQuality(0.2).toFile(sourceFileThumb);
  429. // tempFileThumb.delete();
  430. // } catch (Exception e) {
  431. // System.out.println(e);
  432. // }
  433. // System.out.println("Thumbnails end");
  434. // return sourceFile;
  435. // }
  436. // return null;
  437. // }
  438. /**
  439. * [小文件] 将 远程文件 异步保存到 COS储存桶
  440. * 注意:
  441. * - 由于是异步上传,可能访问会有延迟
  442. * - 如果文件过大,可能会造成内存溢出,所以最好 不要作为上传大文件去使用,不要作为公开接口
  443. */
  444. public UploadOriginResp uploadOrigin(UploadOriginDTO uploadOriginDTO) throws IOException {
  445. if (uploadOriginDTO.getUrl() == null) throw new CustException("图片不能为空");
  446. // 获得自身 user_id
  447. Long user_id = httpRequestAspect.getUserId();
  448. // 获得远程 url
  449. URL url = new URL(uploadOriginDTO.getUrl());
  450. // 获得文件元数据,并判断文件是否失效或过期
  451. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  452. connection.connect();
  453. int responseCode = connection.getResponseCode();
  454. if (responseCode != HttpURLConnection.HTTP_OK) throw new CustException("文件失效或已过期");
  455. // 获取系统默认的临时目录
  456. Path tempDirPath = Paths.get(System.getProperty("java.io.tmpdir"));
  457. // 下载文件到本地 (可能会有性能问题?)
  458. HttpUtil.downloadFile(uploadOriginDTO.getUrl(), tempDirPath.toFile());
  459. // 获得 文件名
  460. String filename = Paths.get(url.getPath()).getFileName().toString();
  461. // 获得 后缀名
  462. String suffix = StringUtils.getFilenameExtension(filename);
  463. // 判断 是否为图片
  464. boolean isImage = Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "tiff", "webp").contains(suffix.toLowerCase());
  465. // 获得 文件名 (缩略图)
  466. String filenameThumb = isImage ? filename.replaceAll("." + suffix, "-thumb." + suffix) : null;
  467. File downloadFile = new File(tempDirPath + File.separator + filename);
  468. File downloadFileThumb = isImage ? new File(File.createTempFile("tmp", "." + suffix).getPath()) : null;
  469. if (isImage) {
  470. // 压缩图片
  471. Thumbnails.of(downloadFile.getPath()).size(60, 60).outputQuality(0.2).toFile(downloadFileThumb);
  472. }
  473. // 获得 要保存的路径/文件名
  474. String key = uploadOriginDTO.getDir() + "/" + filename;
  475. String keyThumb = isImage ? uploadOriginDTO.getDir() + "/" + filenameThumb : null;
  476. String remotePath = accessibleDomain + "/" + key;
  477. String remotePathThumb = isImage ? accessibleDomain + "/" + keyThumb : null;
  478. // 创建返回值实体类
  479. UploadOriginResp resp = new UploadOriginResp();
  480. resp.setUrl(accessibleDomain + "/" + key);
  481. resp.setUrl_thumb(isImage ? accessibleDomain + "/" + keyThumb : null);
  482. resp.setOrigin_url(uploadOriginDTO.getUrl());
  483. // 返回文件元数据 (类型,大小,最后更新时间)
  484. resp.setContent_type(connection.getContentType());
  485. resp.setContent_length(connection.getContentLength());
  486. resp.setLast_modified(connection.getLastModified());
  487. // -- 创建异步上传任务 ---------------------------------------------------------------------
  488. CompletableFuture.runAsync(() -> {
  489. try {
  490. // -- 将文件流保存到对象储存桶 ------------------------------------------------------
  491. COSClient cosClient = getClient(secretId, secretKey);
  492. // // 检查对象是否存在于存储桶中
  493. // if (checkObjectExist(cosClient, key)) {
  494. // System.out.println(key + " 已存在,不需要上传");
  495. // } else {
  496. // 上传原图
  497. PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, downloadFile);
  498. PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
  499. System.out.println("putObjectResult: " + putObjectResult.toString());
  500. // 上传缩略图
  501. if (isImage) {
  502. PutObjectRequest putObjectRequestThumb = new PutObjectRequest(bucketName, keyThumb, downloadFileThumb);
  503. PutObjectResult putObjectResultThumb = cosClient.putObject(putObjectRequestThumb);
  504. System.out.println("putObjectResultThumb: " + putObjectResultThumb.toString());
  505. }
  506. // -- [DB] 插入数据 -------------------------------------
  507. SysFileDTO fileDTO = new SysFileDTO();
  508. fileDTO.setUser_id(user_id);
  509. fileDTO.setName(filename);
  510. fileDTO.setFile_key(key);
  511. fileDTO.setFile_remote_path(remotePath);
  512. if (isImage) {
  513. fileDTO.setFile_remote_path_thumb(remotePathThumb);
  514. }
  515. fileDTO.setSize(Long.valueOf(connection.getContentLength()));
  516. InputStream inputStream = new FileInputStream(downloadFile);
  517. String md5 = DigestUtil.md5Hex(inputStream);
  518. fileDTO.setMd5(md5);
  519. // fileDTO.setCategory_id(getCategoryIdBySuffix(suffix));
  520. // sysFileMapper.insertFile(fileDTO);
  521. System.out.println("insert success");
  522. // }
  523. } catch (IOException e) {
  524. throw new RuntimeException(e);
  525. }
  526. });
  527. return resp;
  528. }
  529. //
  530. // /**
  531. // * 查询分块上传任务
  532. // */
  533. // public List<String> listMultipartUploads(String key){
  534. //
  535. // COSClient cosClient = getClient(secretId, secretKey);
  536. //
  537. // ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
  538. // // 每次请求最多列出多少个
  539. // listMultipartUploadsRequest.setMaxUploads(100);
  540. // // 设置要查询的分块上传任务的目标前缀,直接设置要查询的 key
  541. // listMultipartUploadsRequest.setPrefix(key);
  542. //
  543. // MultipartUploadListing multipartUploadListing = null;
  544. //
  545. // boolean found = false;
  546. // List<String> list = new ArrayList<>();
  547. // do {
  548. // multipartUploadListing = cosClient.listMultipartUploads(listMultipartUploadsRequest);
  549. // List<MultipartUpload> multipartUploads = multipartUploadListing.getMultipartUploads();
  550. //
  551. // for (MultipartUpload mUpload : multipartUploads) {
  552. // if (mUpload.getKey().equals(key)) {
  553. // System.out.println(mUpload.getUploadId());
  554. // list.add(mUpload.getUploadId());
  555. // //found = true;
  556. // //break;
  557. // }
  558. // }
  559. //
  560. // //if (found) {
  561. // // break;
  562. // //}
  563. // listMultipartUploadsRequest.setKeyMarker(multipartUploadListing.getNextKeyMarker());
  564. // listMultipartUploadsRequest.setUploadIdMarker(multipartUploadListing.getNextUploadIdMarker());
  565. // } while (multipartUploadListing.isTruncated());
  566. //
  567. // if (!found) {
  568. // System.out.printf("do not found upload task with key: %s\n", key);
  569. // }
  570. //
  571. // return list;
  572. // }
  573. // /**
  574. // * 简单上传
  575. // */
  576. // public MultipartUploadRespDTO simpleUpload(MultipartFile file) {
  577. //
  578. // // 检查上传的文件是否为空
  579. // if (file == null || file.isEmpty()) throw new CustException("file 上传文件不能为空");
  580. //
  581. // // 判断文件大小是否超过
  582. // if (file.getSize() > maxSize) {
  583. // throw new CustException("上传文件不能大于 " + maxSize/1024/1024 + " MB,如有需要请使用大文件上传功能");
  584. // }
  585. //
  586. // // 生成cos客户端
  587. // COSClient cosClient = getClient(secretId, secretKey);
  588. //
  589. // MultipartUploadRespDTO resp = new MultipartUploadRespDTO();
  590. // try {
  591. //
  592. // String md5 = DigestUtil.md5Hex(file.getInputStream());
  593. // String suffix = FileUtil.extName(file.getOriginalFilename()).toLowerCase();
  594. // String filename = md5 + "." + suffix;
  595. // String filenameThumb = filename.replaceAll("." + suffix, "-thumb." + suffix);
  596. // String key = "temp/" + filename;
  597. // String keyThumb = "temp/" + filenameThumb;
  598. // String remotePath = accessibleDomain + "/" + key;
  599. // String remotePathThumb = accessibleDomain + "/" + keyThumb;
  600. //
  601. //// // 获得文件分类列表
  602. //// SysFileCategoryDTO categoryDTO = new SysFileCategoryDTO();
  603. //// List<SysFileCategoryDTO> categoryList = sysFileCategoryMapper.queryFileCategoryList(categoryDTO);
  604. //
  605. // // 获得图片分类的后缀 (待做),再进行以下判断
  606. //
  607. // // 还有删除也要同时删除缩略图
  608. //
  609. // // 还有大文件分片也要做缩略图
  610. //
  611. // // 还有 视频要做截图生成
  612. //
  613. //
  614. //
  615. //
  616. //
  617. // boolean isImage = Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "tiff", "webp").contains(suffix.toLowerCase());
  618. //
  619. //
  620. //
  621. //
  622. //
  623. //
  624. //
  625. // // 检查对象是否存在于存储桶中
  626. // if (checkObjectExist(cosClient, key)) {
  627. // // -- [COS] 对象存在时,直接返回路径 -------------------------------
  628. // resp.setIs_fast_upload(true);
  629. //
  630. // } else {
  631. // // 获取文件内容
  632. // InputStream inputStream = file.getInputStream();
  633. //
  634. // // 将文件内容写入临时文件
  635. // String tempFilename = CommonUtil.generateFilename(null);
  636. // File tempFile = File.createTempFile(tempFilename, null);
  637. // try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
  638. // IOUtils.copy(inputStream, outputStream);
  639. // }
  640. // // 创建 PutObjectRequest 对象,PutObject 请求。(异步)
  641. // PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, tempFile);
  642. // cosClient.putObject(putObjectRequest);
  643. //
  644. //
  645. // // 创建缩略图
  646. // if (isImage) {
  647. // File tempFileThumb = File.createTempFile(tempFilename, "." + suffix);
  648. // String sourceFilePath = tempFileThumb.getPath();
  649. // File sourceFile = new File(sourceFilePath);
  650. // // 使用Thumbnailator生成缩略图并写入到临时文件
  651. // Thumbnails.of(tempFile).size(60, 60).outputQuality(0.2).toFile(tempFileThumb);
  652. // PutObjectRequest putObjectRequestThumb = new PutObjectRequest(bucketName, keyThumb, sourceFile);
  653. // cosClient.putObject(putObjectRequestThumb);
  654. // resp.setPath_thumb(remotePathThumb);
  655. // tempFileThumb.delete();
  656. // }
  657. //
  658. // tempFile.delete();
  659. //
  660. // }
  661. //
  662. // // -- [DB] 查询数据 ---------------------------------------------
  663. //// SysFileDTO fileDetail = sysFileMapper.queryFileByKey(key);
  664. //// // 如果表中没有数据,则添加数据
  665. //// if (fileDetail == null) {
  666. // // -- [DB] 插入数据 -----------------------------------------
  667. // SysFileDTO fileDTO = new SysFileDTO();
  668. // fileDTO.setUser_id(httpRequestAspect.getUserId());
  669. // fileDTO.setName(filename);
  670. // fileDTO.setFile_key(key);
  671. // fileDTO.setFile_remote_path(remotePath);
  672. // if (isImage) {
  673. // fileDTO.setFile_remote_path_thumb(remotePathThumb);
  674. // }
  675. //// fileDTO.setCategory_id(getCategoryIdBySuffix(suffix));
  676. // fileDTO.setSize(file.getSize());
  677. // fileDTO.setMd5(md5);
  678. // sysFileMapper.insertFile(fileDTO);
  679. //// }
  680. // // -------------------------------------------------------------
  681. //
  682. // resp.setFilename(filename);
  683. // resp.setKey(key);
  684. // resp.setMd5(md5);
  685. // resp.setSize(file.getSize());
  686. // resp.setPath(remotePath);
  687. //
  688. // } catch (IOException e) {
  689. // throw new CustException(e.getMessage());
  690. // } finally {
  691. // if (cosClient != null) {
  692. // cosClient.shutdown();
  693. // }
  694. // }
  695. // return resp;
  696. //
  697. // }
  698. // // -- 检查对象是否存在于存储桶中 ---------------------------------------------------------------------
  699. // private Boolean checkObjectExist(COSClient cosClient, String key) {
  700. // try {
  701. // return cosClient.doesObjectExist(bucketName, key);
  702. // } catch (CosServiceException e) {
  703. // e.printStackTrace();
  704. // }
  705. // return false;
  706. // }
  707. // // -- 初始化分块任务 -------------------------------------------------------------------------------
  708. // private MultipartUploadRespDTO initiateMultipartUpload(COSClient cosClient, MultipartFile file, String key, String filename){
  709. // System.out.println("-- initiateMultipartUpload --");
  710. // try {
  711. // //
  712. // InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
  713. // // 分块上传的过程中,仅能通过初始化分块指定文件上传之后的 metadata, 需要的头部可以在这里指定
  714. // ObjectMetadata objectMetadata = new ObjectMetadata();
  715. // request.setObjectMetadata(objectMetadata);
  716. // //
  717. // InitiateMultipartUploadResult initResult = cosClient.initiateMultipartUpload(request);
  718. //
  719. // MultipartUploadRespDTO resp = new MultipartUploadRespDTO();
  720. // resp.setUpload_id(initResult.getUploadId());
  721. // resp.setKey(key);
  722. // resp.setSize(file.getSize());
  723. // resp.setFilename(filename);
  724. // System.out.println("-------------");
  725. // System.out.println(filename);
  726. // System.out.println("-------------");
  727. // return resp;
  728. //
  729. // } catch (CosServiceException e) {
  730. // throw new CustException(e.getMessage());
  731. // }
  732. // }
  733. // // -- 上传分块 ------------------------------------------------------------------------------------
  734. // private MultipartUploadRespDTO uploadPart(COSClient cosClient, MultipartUploadDTO multipartUploadDTO) {
  735. // System.out.println("-- uploadPart --");
  736. // try {
  737. // InputStream inputStream = multipartUploadDTO.getFile().getInputStream();
  738. //
  739. // UploadPartRequest uploadPartRequest = new UploadPartRequest();
  740. // uploadPartRequest.setBucketName(bucketName);
  741. // uploadPartRequest.setKey(multipartUploadDTO.getKey());
  742. // uploadPartRequest.setUploadId(multipartUploadDTO.getUpload_id());
  743. // uploadPartRequest.setInputStream(inputStream);
  744. //
  745. // // 设置分块的长度 (分块文件大小)
  746. // uploadPartRequest.setPartSize(multipartUploadDTO.getFile().getSize());
  747. // // 设置要上传的分块编号,从1开始
  748. // uploadPartRequest.setPartNumber(multipartUploadDTO.getIndex());
  749. //
  750. // // [COS] 上传分块
  751. // UploadPartResult uploadPartResult = cosClient.uploadPart(uploadPartRequest);
  752. // PartETag partETag = uploadPartResult.getPartETag();
  753. //
  754. // MultipartUploadRespDTO resp = new MultipartUploadRespDTO();
  755. // resp.setUpload_id(multipartUploadDTO.getUpload_id());
  756. // resp.setKey(multipartUploadDTO.getKey());
  757. // resp.setIndex(partETag.getPartNumber());
  758. // resp.setCount(multipartUploadDTO.getCount());
  759. // resp.setChunk_md5(partETag.getETag());
  760. // resp.setChunk_size(uploadPartRequest.getPartSize());
  761. // return resp;
  762. //
  763. // } catch (CosServiceException e) {
  764. // if ("NoSuchUpload".equals(e.getErrorCode())) {
  765. // throw new CustException("找不到相关的 upload_id 或 key 信息,任务已终止或已完成");
  766. // }
  767. // throw new CustException(e.getMessage());
  768. // } catch (IOException e) {
  769. // throw new RuntimeException(e);
  770. // }
  771. // }
  772. // // -- 查询已上传的分块 ------------------------------------------------------------------------------
  773. // public List<PartETag> listParts(String uploadId, String key) {
  774. // COSClient cosClient = getClient(secretId, secretKey);
  775. //
  776. // // 用于保存已上传的分片信息
  777. // List<PartETag> partETags = new LinkedList<>();
  778. //
  779. // PartListing partListing = null;
  780. // ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
  781. // do {
  782. // try {
  783. // partListing = cosClient.listParts(listPartsRequest);
  784. // } catch (CosServiceException e) {
  785. // throw e;
  786. // }
  787. // for (PartSummary partSummary : partListing.getParts()) {
  788. // partETags.add(new PartETag(partSummary.getPartNumber(), partSummary.getETag()));
  789. // }
  790. // listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker());
  791. // } while (partListing.isTruncated());
  792. // return partETags;
  793. // }
  794. // // -- 合并分块上传 ---------------------------------------------------------------------------------
  795. // private MultipartUploadRespDTO completeMultipartUpload(COSClient cosClient, MultipartUploadDTO multipartUploadDTO) {
  796. // System.out.println("-- completeMultipartUpload --");
  797. // // 保存已上传的分片信息,实际情况里,这里的内容是从上传分块接口中获取到的
  798. // List<PartETag> partETags = listParts(multipartUploadDTO.getUpload_id(), multipartUploadDTO.getKey());
  799. //
  800. // // 分片上传结束后,调用 complete 完成分片上传
  801. // CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, multipartUploadDTO.getKey(), multipartUploadDTO.getUpload_id(), partETags);
  802. // try {
  803. // CompleteMultipartUploadResult completeResult = cosClient.completeMultipartUpload(completeMultipartUploadRequest);
  804. //
  805. // MultipartUploadRespDTO resp = new MultipartUploadRespDTO();
  806. // resp.setRequest_id(completeResult.getRequestId());
  807. // resp.setPath(accessibleDomain + "/" + multipartUploadDTO.getKey());
  808. // return resp;
  809. //
  810. // } catch (CosServiceException e) {
  811. //
  812. // // -- [DB] 删除 uploadId 对应的分块信息 ---------------------
  813. // sysFileMapper.deleteFile(multipartUploadDTO.getKey());
  814. //
  815. // // 终止任务
  816. // abortMultipartUpload(multipartUploadDTO);
  817. //
  818. // System.out.println("getErrorCode:");
  819. // System.out.println(e.getErrorCode());
  820. // System.out.println(e.getMessage());
  821. // throw new CustException("合并失败,无效的分块,请重新上传", 6001, "(" + e.getErrorCode() + ") " + e.getMessage());
  822. // }
  823. // }
  824. //// // -- 获得分类 CategoryID (通过文件后缀名) -----------------------------------------------------------
  825. //// private Long getCategoryIdBySuffix(String suffix) {
  826. //// SysFileCategoryDTO sysFileCategoryDTO = new SysFileCategoryDTO();
  827. //// List<SysFileCategoryDTO> categoryList = sysFileCategoryMapper.queryFileCategoryList(sysFileCategoryDTO);
  828. //// Optional<Long> firstMatchedId = categoryList.stream().filter(dto -> dto.getSuffix().toLowerCase().contains(suffix)).map(SysFileCategoryDTO::getId).findFirst();
  829. //// Long id = firstMatchedId.orElse(null);
  830. //// return id;
  831. //// }
  832. // // -----------------------------------------------------------------------------------------------
  833. //
  834. // /**
  835. // * 分块上传 (1判断是否存在,2初始化,3上传分块,4合并分块)
  836. // * - https://cloud.tencent.com/document/product/436/65935
  837. // * - https://cloud.tencent.com/document/product/436/14112
  838. // * @param multipartUploadDTO
  839. // * @return
  840. // */
  841. // public MultipartUploadRespDTO multipartUpload(MultipartUploadDTO multipartUploadDTO) {
  842. //
  843. // // -- 参数校验 ------------------------------------------------------
  844. // if (multipartUploadDTO.getFile() == null || multipartUploadDTO.getFile().isEmpty()) throw new CustException("file 上传文件不能为空");
  845. //
  846. // // 当存在 uploadId 时,即是分块上传阶段
  847. // if (multipartUploadDTO.getUpload_id() != null) {
  848. // if (multipartUploadDTO.getKey() == null) throw new CustException("key 不能为空");
  849. // if (multipartUploadDTO.getIndex() == null) throw new CustException("index 不能为空");
  850. // if (multipartUploadDTO.getCount() == null) throw new CustException("count 不能为空");
  851. // if (multipartUploadDTO.getIndex() > multipartUploadDTO.getCount()) {
  852. // throw new CustException("index 不能大于 count");
  853. // }
  854. // }
  855. // // -----------------------------------------------------------------
  856. //
  857. // COSClient cosClient = getClient(secretId, secretKey);
  858. //
  859. // MultipartUploadRespDTO resp = new MultipartUploadRespDTO();
  860. //
  861. // // [秒传] 检查对象是否存在,存在即 "秒传"
  862. // try {
  863. //
  864. // String md5 = DigestUtil.md5Hex(multipartUploadDTO.getFile().getInputStream());
  865. // String suffix = FileUtil.extName(multipartUploadDTO.getFile().getOriginalFilename()).toLowerCase();
  866. // String filename = md5 + "." + suffix;
  867. // String key = "temp/" + filename;
  868. //
  869. // // 检查对象是否存在于存储桶中
  870. // if (checkObjectExist(cosClient, key)) {
  871. //
  872. // String remotePath = accessibleDomain + "/" + key;
  873. //
  874. // // -- [COS] 对象存在时,直接返回路径 -------------------------------
  875. // resp.setKey(key);
  876. // resp.setPath(remotePath);
  877. // resp.setIs_fast_upload(true);
  878. //
  879. // // -- [DB] 查询数据 ---------------------------------------------
  880. //// SysFileDTO fileDetail = sysFileMapper.queryFileByKey(key);
  881. //// // 如果表中没有数据,则添加数据
  882. //// if (fileDetail == null) {
  883. // // -- [DB] 插入数据 -----------------------------------------
  884. // SysFileDTO fileDTO = new SysFileDTO();
  885. // fileDTO.setUser_id(httpRequestAspect.getUserId());
  886. // fileDTO.setName(filename);
  887. // fileDTO.setFile_key(key);
  888. // fileDTO.setFile_remote_path(remotePath);
  889. //// fileDTO.setCategory_id(getCategoryIdBySuffix(suffix));
  890. //
  891. // // 获得文件大小
  892. // fileDTO.setSize(CommonUtil.getRemoteFileSize(remotePath));
  893. //
  894. // fileDTO.setMd5(md5);
  895. // sysFileMapper.insertFile(fileDTO);
  896. //// }
  897. // // -------------------------------------------------------------
  898. //
  899. // } else {
  900. //
  901. // // 对象不存在时,走创建流程
  902. // String uploadId = multipartUploadDTO.getUpload_id();
  903. // if (uploadId == null || uploadId.isEmpty()) {
  904. //
  905. // // -- [DB] 查询数据 (By 文件Key) ------------------------------
  906. // SysFileDTO fileDetail = sysFileMapper.queryFileByKey(key);
  907. // if (fileDetail != null) {
  908. // resp.setUpload_id(fileDetail.getChunk_upload_id());
  909. // resp.setKey(fileDetail.getFile_key());
  910. // resp.setSize(fileDetail.getSize());
  911. // resp.setFilename(filename);
  912. //// resp.setIndex(fileDetail.getChunk_current_index());
  913. //// resp.setCount(fileDetail.getChunk_count());
  914. // // 返回 { uploadId, key }
  915. // // 缺少 { chunk_md5, chunk_size }
  916. // } else {
  917. //
  918. //// // 上传要求每个分块大小必须大于 1MB,所以整个文件也不能小于 1MB
  919. //// if (multipartUploadDTO.getFile().getSize() < 1 * 1024 * 1024) {
  920. //// throw new CustException("上传文件不能小于1MB");
  921. //// }
  922. //
  923. // // -- [初始化] 第一次上传,无 uploadId ---------------------
  924. // resp = initiateMultipartUpload(cosClient, multipartUploadDTO.getFile(), key, filename);
  925. // // 返回 { uploadId, key }
  926. //
  927. // // -- [DB] 插入数据 -------------------------------------
  928. // SysFileDTO fileDTO = new SysFileDTO();
  929. // fileDTO.setUser_id(httpRequestAspect.getUserId());
  930. // fileDTO.setName(filename);
  931. // fileDTO.setFile_key(key);
  932. // fileDTO.setChunk_upload_id(resp.getUpload_id());
  933. // fileDTO.setSize(resp.getSize());
  934. // fileDTO.setMd5(md5);
  935. //// fileDTO.setCategory_id(getCategoryIdBySuffix(suffix));
  936. // sysFileMapper.insertFile(fileDTO);
  937. // }
  938. // // ---------------------------------------------------------
  939. //
  940. // } else {
  941. //
  942. // // 分块上传要求每个分块大小必须大于 1MB (最后一个例外)
  943. // if (resp.getIndex() != resp.getCount()) {
  944. // if (multipartUploadDTO.getFile().getSize() < 1 * 1024 * 1024) {
  945. // throw new CustException("分块文件不能小于1MB");
  946. // }
  947. // }
  948. //
  949. // // -- [DB] 查询数据 (By 直传Key) ------------------------------
  950. // SysFileDTO fileDetail = sysFileMapper.queryFileByKey(multipartUploadDTO.getKey());
  951. //
  952. // // -- [分块上传] 带着 uploadId 来,开始上传分块 ------------------
  953. // resp = uploadPart(cosClient, multipartUploadDTO);
  954. // // 返回 { uploadId, key, index, part_etag, size }
  955. //
  956. // // -- [合并分块] 当最后一块上传完成时,合并分块 --------------------
  957. // if (resp.getIndex() != null && resp.getCount() != null && resp.getIndex() == resp.getCount()) {
  958. //
  959. // MultipartUploadRespDTO complete = completeMultipartUpload(cosClient, multipartUploadDTO);
  960. //
  961. // // 附上最后一次上传的分块信息
  962. // complete.setUpload_id(resp.getUpload_id());
  963. // complete.setKey(resp.getKey());
  964. // complete.setSize(resp.getSize());
  965. // complete.setIndex(resp.getIndex());
  966. // complete.setCount(resp.getCount());
  967. // complete.setChunk_md5(resp.getChunk_md5());
  968. // complete.setChunk_size(resp.getChunk_size());
  969. // // 以及返回 { request_id, path }
  970. // resp = complete;
  971. //
  972. // }
  973. //
  974. //
  975. // if (fileDetail != null) {
  976. //
  977. // System.out.println("fileDetail != null !");
  978. // // -- [DB] 更新数据 -------------------------------------
  979. //
  980. // SysFileDTO fileDTO = new SysFileDTO();
  981. // fileDTO.setUser_id(httpRequestAspect.getUserId());
  982. // fileDTO.setFile_key(resp.getKey());
  983. //
  984. // fileDTO.setChunk_current_index(resp.getIndex());
  985. // fileDTO.setChunk_count(resp.getCount());
  986. //
  987. // // 没有完成分块时
  988. // if (resp.getPath() != null) {
  989. //
  990. // Long size = fileDetail.getSize() != null ? fileDetail.getSize() : 0;
  991. // fileDTO.setSize(size + resp.getChunk_size());
  992. //
  993. // fileDTO.setFile_remote_path(resp.getPath());
  994. // fileDTO.setChunk_current_index(null);
  995. // fileDTO.setChunk_count(null);
  996. // fileDTO.setChunk_upload_id("");
  997. // }
  998. //
  999. // sysFileMapper.updateFile(fileDTO);
  1000. // }
  1001. //
  1002. // // ------------------------------------------------------
  1003. // }
  1004. // }
  1005. //
  1006. // } catch (IOException e) {
  1007. // throw new CustException(e.getMessage());
  1008. // } finally {
  1009. // if (cosClient != null) {
  1010. // cosClient.shutdown();
  1011. // }
  1012. // }
  1013. //
  1014. // return resp;
  1015. // }
  1016. //
  1017. // /**
  1018. // * 终止分块上传任务
  1019. // */
  1020. // public Map<String, Object> abortMultipartUpload(MultipartUploadDTO multipartUploadDTO){
  1021. //
  1022. // COSClient cosClient = getClient(secretId, secretKey);
  1023. //
  1024. // String key = multipartUploadDTO.getKey();
  1025. // String uploadId = multipartUploadDTO.getUpload_id();
  1026. //
  1027. // AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, uploadId);
  1028. // try {
  1029. // cosClient.abortMultipartUpload(abortMultipartUploadRequest);
  1030. // } catch (CosServiceException e) {
  1031. // System.out.println("Service");
  1032. // System.out.println(e.getErrorCode());
  1033. // System.out.println(e.getMessage());
  1034. // if ("NoSuchUpload".equals(e.getErrorCode())) {
  1035. // throw new CustException("找不到相关的 upload_id 或 key 信息,任务已终止或已完成");
  1036. // }
  1037. // throw new CustException(e.getMessage());
  1038. // }
  1039. //
  1040. // // 确保不再使用 cosClient 之后,关闭即可
  1041. // cosClient.shutdown();
  1042. //
  1043. // // -- [DB] 删除数据 --------------------------------------------------
  1044. // sysFileMapper.deleteFile(key);
  1045. //
  1046. // Map<String, Object> resp = new LinkedHashMap<>();
  1047. // resp.put("key", key);
  1048. // resp.put("upload_id", uploadId);
  1049. //
  1050. // return resp;
  1051. // }
  1052. //
  1053. //
  1054. //
  1055. // /**
  1056. // * 删除文件
  1057. // */
  1058. // public Map<String, Object> removeUploadFile(SysFileDTO sysFileDTO) {
  1059. //
  1060. // String name = sysFileDTO.getName();
  1061. // String suffix = StringUtils.getFilenameExtension(name.toLowerCase());
  1062. //
  1063. // String nameThumb = name.replaceAll("." + suffix, "-thumb." + suffix);
  1064. //
  1065. // String key = "temp/" + name;
  1066. // String keyThumb = "temp/" + nameThumb;
  1067. //
  1068. // // -- [COS] 删除对象 --------------------------------------
  1069. // COSClient cosClient = getClient(secretId, secretKey);
  1070. // cosClient.deleteObject(bucketName, key);
  1071. // cosClient.deleteObject(bucketName, keyThumb);
  1072. // cosClient.shutdown();
  1073. //
  1074. // // -- [DB] 判断记录是否存在 --------------------------------
  1075. // SysFileDTO fileDetail = sysFileMapper.queryFileByKey(key);
  1076. // if (fileDetail == null) throw new CustException("文件记录不存在");
  1077. //
  1078. // // -- [DB] 删除存储桶中的文件 -------------------------------
  1079. // // -- 创建异步上传任务 ---------------------------------------------------------------------
  1080. // CompletableFuture.runAsync(() -> {
  1081. // sysFileMapper.deleteFile(key);
  1082. // sysFileMapper.deleteFile(keyThumb);
  1083. // });
  1084. //
  1085. // return Map.of("key", key);
  1086. // }
  1087. }