Browse Source

增加封面有效性校验

xueyiming 2 months ago
parent
commit
f9b4dd12d1

+ 14 - 10
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/Impl/MessageAttachmentServiceImpl.java

@@ -4,7 +4,9 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.growth.common.common.base.CommonResponse;
 import com.tzld.piaoquan.growth.common.common.constant.MessageConstant;
+import com.tzld.piaoquan.growth.common.common.enums.ExceptionCodeEnum;
 import com.tzld.piaoquan.growth.common.common.enums.MessageAttachmentTypeEnum;
+import com.tzld.piaoquan.growth.common.common.exception.CustomizeException;
 import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
 import com.tzld.piaoquan.growth.common.component.ProxyHttpPoolClient;
 import com.tzld.piaoquan.growth.common.dao.mapper.GuaranteesVideoMapper;
@@ -17,10 +19,7 @@ import com.tzld.piaoquan.growth.common.model.vo.GuaranteedParam;
 import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
 import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
 import com.tzld.piaoquan.growth.common.service.WeComSendService;
-import com.tzld.piaoquan.growth.common.utils.DateUtil;
-import com.tzld.piaoquan.growth.common.utils.FileUtils;
-import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
-import com.tzld.piaoquan.growth.common.utils.ToolUtils;
+import com.tzld.piaoquan.growth.common.utils.*;
 import lombok.extern.slf4j.Slf4j;
 import lombok.val;
 import org.apache.commons.lang3.StringUtils;
@@ -152,10 +151,13 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
                 return voidCommonResponse;
             }
         }
-
-        Map<Long, VideoDetail> coverMap = getVideoDetail(videoIds);
-        Map<Long, MessageAttachment> messageAttachmentMap = createMessageAttachmentMap(videoIds, coverMap);
-
+        Map<Long, MessageAttachment> messageAttachmentMap;
+        try {
+            Map<Long, VideoDetail> coverMap = getVideoDetail(videoIds);
+            messageAttachmentMap = createMessageAttachmentMap(videoIds, coverMap);
+        } catch (CustomizeException e) {
+            return CommonResponse.create(500, e.getMessage());
+        }
         List<MessageAttachment> messageAttachmentList = new ArrayList<>();
         for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
             addMessageAttachments(videoParam, messageAttachmentMap, guaranteedParam.getDate(), messageAttachmentList);
@@ -215,8 +217,10 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
         for (Long videoId : videoIds) {
             VideoDetail videoDetail = coverMap.get(videoId);
             if (videoDetail == null || StringUtils.isEmpty(videoDetail.getCover()) || StringUtils.isEmpty(videoDetail.getTitle())) {
-                LarkRobotUtil.sendMessage("获取视频详情异常,请查看" + videoId);
-                throw new RuntimeException("获取视频详情异常");
+                throw new CustomizeException(ExceptionCodeEnum.PARAMS_ERROR, "获取视频详情异常,请查看:" + videoId);
+            }
+            if (!ImageUrlValidator.isValidImageUrl(videoDetail.getCover())) {
+                throw new CustomizeException(ExceptionCodeEnum.PARAMS_ERROR, "保底视频封面异常,请查看:" + videoId);
             }
             MessageAttachment messageAttachment = new MessageAttachment();
             messageAttachment.setMiniprogramVideoId(videoId);

+ 59 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/utils/ImageUrlValidator.java

@@ -0,0 +1,59 @@
+package com.tzld.piaoquan.growth.common.utils;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.ProtocolException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.List;
+
+public class ImageUrlValidator {
+
+    // 常见图片类型列表
+    private static final List<String> IMAGE_CONTENT_TYPES = Arrays.asList(
+            "image/jpeg", "image/png", "image/gif", "image/bmp",
+            "image/webp", "image/svg+xml", "image/tiff"
+    );
+
+    // 连接超时时间(毫秒)
+    private static final int CONNECTION_TIMEOUT = 5000;
+    // 读取超时时间(毫秒)
+    private static final int READ_TIMEOUT = 5000;
+
+    /**
+     * 验证图片URL是否有效
+     * @param imageUrl 图片URL
+     * @return 如果URL有效且指向图片返回true,否则返回false
+     */
+    public static boolean isValidImageUrl(String imageUrl) {
+        if (imageUrl == null || imageUrl.trim().isEmpty()) {
+            return false;
+        }
+
+        HttpURLConnection connection = null;
+        try {
+            URL url = new URL(imageUrl);
+            connection = (HttpURLConnection) url.openConnection();
+            connection.setConnectTimeout(CONNECTION_TIMEOUT);
+            connection.setReadTimeout(READ_TIMEOUT);
+            connection.setRequestMethod("HEAD"); // 使用HEAD请求减少数据传输
+
+            int responseCode = connection.getResponseCode();
+            if (responseCode != HttpURLConnection.HTTP_OK) {
+                return false;
+            }
+
+            String contentType = connection.getContentType();
+            return contentType != null &&
+                    IMAGE_CONTENT_TYPES.stream().anyMatch(contentType::contains);
+
+        } catch (IOException e) {
+            return false;
+        } finally {
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+    }
+}