CdnUtil.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.tzld.piaoquan.api.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.IAcsClient;
  5. import com.aliyuncs.cdn.model.v20141111.DescribeDomainBpsDataRequest;
  6. import com.aliyuncs.cdn.model.v20141111.DescribeDomainBpsDataResponse;
  7. import com.aliyuncs.exceptions.ClientException;
  8. import com.aliyuncs.exceptions.ServerException;
  9. import com.aliyuncs.profile.DefaultProfile;
  10. import com.stuuudy.commons.external.filestorage.enums.EnumFileType;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import java.time.LocalDateTime;
  15. import java.time.format.DateTimeFormatter;
  16. import java.util.Objects;
  17. public class CdnUtil {
  18. private static final Logger LOGGER = LoggerFactory.getLogger(CdnUtil.class);
  19. // public static final String VIDEO_CDN_URL_HOST = "https://xycdn.yishihui.com/";
  20. public static final String VIDEO_CDN_URL_HOST = "http://rescdn.yishihui.com/";
  21. public static final String PICTURE_CDN_URL_HOST_PICTURE = "http://rescdn.yishihui.com/";
  22. public static final String DOWNLOAD_CDN_URL_HOST_PICTURE = "https://rescdn.yishihui.com/";
  23. private static IAcsClient client;
  24. static {
  25. if (Objects.isNull(client)) {
  26. DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAInlg8tWvBeiJe", "hDu5v2nrOmdY0AjlqGnZkW57mxCOLO");
  27. /** use STS Token
  28. DefaultProfile profile = DefaultProfile.getProfile(
  29. "<your-region-id>", // The region ID
  30. "<your-access-key-id>", // The AccessKey ID of the RAM account
  31. "<your-access-key-secret>", // The AccessKey Secret of the RAM account
  32. "<your-sts-token>"); // STS Token
  33. **/
  34. client = new DefaultAcsClient(profile);
  35. }
  36. }
  37. public static Double getDescribeDomainBpsData() {
  38. DescribeDomainBpsDataRequest request = new DescribeDomainBpsDataRequest();
  39. //需要时区为GMT
  40. LocalDateTime endTime = LocalDateTime.now().plusHours(-8);
  41. LocalDateTime startTime = endTime.plusMinutes(-10);
  42. DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  43. String[] starts = startTime.format(df).split(" ");
  44. String[] ends = endTime.format(df).split(" ");
  45. String start = starts[0] + "T" + starts[1] + "Z";
  46. String end = ends[0] + "T" + ends[1] + "Z";
  47. request.setStartTime(start);
  48. request.setEndTime(end);
  49. LOGGER.info("getDescribeDomainBpsData request = {}", JSON.toJSONString(request));
  50. try {
  51. DescribeDomainBpsDataResponse response = client.getAcsResponse(request);
  52. LOGGER.info("getDescribeDomainBpsData response = {}", JSON.toJSONString(response));
  53. if (Objects.isNull(response) || Objects.isNull(response.getBpsDataPerInterval())
  54. || response.getBpsDataPerInterval().isEmpty() || response.getBpsDataPerInterval().size() <= 1) {
  55. //只有1条可能不准确
  56. return null;
  57. }
  58. DescribeDomainBpsDataResponse.DataModule module1 = response.getBpsDataPerInterval().get(1);
  59. if (Objects.nonNull(module1) && Objects.equals("0", module1.getValue())) {
  60. return null;
  61. }
  62. DescribeDomainBpsDataResponse.DataModule module = response.getBpsDataPerInterval().get(0);
  63. if (Objects.nonNull(module) && Objects.nonNull(module.getValue())) {
  64. return Double.parseDouble(module.getValue()) / 1000 / 1000 / 1000;
  65. }
  66. } catch (ServerException e) {
  67. LOGGER.error("getDescribeDomainBpsData ServerException", e);
  68. } catch (ClientException e) {
  69. LOGGER.error("getDescribeDomainBpsData ClientException", e);
  70. }
  71. return null;
  72. }
  73. public static String getCdnUrlHost(EnumFileType fileType) {
  74. if (Objects.isNull(fileType)) {
  75. return null;
  76. }
  77. switch (fileType) {
  78. case VIDEO:
  79. return VIDEO_CDN_URL_HOST;
  80. case PICTURE:
  81. return PICTURE_CDN_URL_HOST_PICTURE;
  82. }
  83. return null;
  84. }
  85. // public static void main(String[] args_) throws Exception {
  86. // DescribeDomainBpsDataRequest request = new DescribeDomainBpsDataRequest();
  87. // //需要时区为GMT
  88. // LocalDateTime endTime = LocalDateTime.now().plusHours(-8);
  89. // LocalDateTime startTime = endTime.plusMinutes(-10);
  90. // DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  91. // String[] starts = startTime.format(df).split(" ");
  92. // String[] ends = endTime.format(df).split(" ");
  93. // String start = starts[0] + "T" + starts[1] + "Z";
  94. // String end = ends[0] + "T" + ends[1] + "Z";
  95. // request.setStartTime(start);
  96. // request.setEndTime(end);
  97. // System.out.println("---" + JSON.toJSONString(request));
  98. // try {
  99. // DescribeDomainBpsDataResponse response = client.getAcsResponse(request);
  100. //
  101. // System.out.println("---" + JSON.toJSONString(response));
  102. // DescribeDomainBpsDataResponse.DataModule module = response.getBpsDataPerInterval().get(0);
  103. //
  104. // System.out.println("---" + module.getValue());
  105. // double d = Double.valueOf(module.getValue()) / 1000 / 1000 / 1000;
  106. // System.out.println(d);
  107. //
  108. //// System.out.println(new Gson().toJson(response));
  109. // } catch (ServerException e) {
  110. // e.printStackTrace();
  111. // } catch (ClientException e) {
  112. // System.out.println("ErrCode:" + e.getErrCode());
  113. // System.out.println("ErrMsg:" + e.getErrMsg());
  114. // System.out.println("RequestId:" + e.getRequestId());
  115. // }
  116. //
  117. // }
  118. public static String getFileExtensionFromUrl(String uri) {
  119. if (StringUtils.isBlank(uri)) {
  120. return "";
  121. }
  122. String fileExtension = "";
  123. try {
  124. if (uri.contains(".")) {
  125. int lastDotIndex = uri.lastIndexOf('.');
  126. if (lastDotIndex > 0) {
  127. fileExtension = uri.substring(lastDotIndex + 1);
  128. }
  129. }
  130. } catch (Exception e) {
  131. LOGGER.error("getFileExtensionFromURL error", e);
  132. }
  133. return fileExtension;
  134. }
  135. public static void main(String[] args) {
  136. String url = "ad/material_1564536000000.mp4";
  137. System.out.println(getFileExtensionFromUrl(url));
  138. }
  139. }