package com.siwei.apply.utils; import com.siwei.apply.domain.NodeAttachment; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import java.net.URI; import java.security.MessageDigest; import java.util.*; /** * 服务类工具 */ public class ServiceUtil { private static final long EXPIRY_TIME = 10 * 60 * 1000; // 10 minutes in milliseconds private static final String SECRET_KEY = "siwei"; // Secret key for signature private static final String TIME_KEY = "?time="; // time private static final String DOMAIN = "http://10.224.1.227:15000/prod-api/apply"; // doman /** * 获取指定目录下的子children list * * @param dirName 目录名称 * @param nodeAttachment 附件信息 * @return */ public static List> getDirChildrenList(String dirName, NodeAttachment nodeAttachment) { List> list = new ArrayList<>(); if (Objects.nonNull(nodeAttachment) && Objects.nonNull(nodeAttachment.getAttachment())) { Map attachment = nodeAttachment.getAttachment(); //第一曾经children 所有目录list List childrenList = (List) attachment.get("children"); if (CollectionUtils.isNotEmpty(childrenList)) { for (Map childMap : childrenList) { String childName = (String) childMap.get("name"); //if (childName.equalsIgnoreCase(dirName)) { if (StringUtils.containsIgnoreCase(childName, dirName)) { list = (List>) childMap.get("children"); break; } } } } return list; } public static Map getDirChildrenMap(String dirName, NodeAttachment nodeAttachment) { Map retMap = null; if (Objects.nonNull(nodeAttachment) && Objects.nonNull(nodeAttachment.getAttachment())) { Map attachment = nodeAttachment.getAttachment(); //第一层级children 所有目录list List childrenList = (List) attachment.get("children"); if (CollectionUtils.isNotEmpty(childrenList)) { for (Map childMap : childrenList) { String childName = (String) childMap.get("name"); if (StringUtils.containsIgnoreCase(childName, dirName)) { retMap = childMap; break; } } } } return retMap; } /** * * @param nodeAttachment * @throws Exception */ public static void convertFilePath(NodeAttachment nodeAttachment) throws Exception { String attachmentId = nodeAttachment.getId(); String timestamp = String.valueOf(System.currentTimeMillis()); if (Objects.nonNull(nodeAttachment.getAttachment())) { Map attachment = nodeAttachment.getAttachment(); //第一层级children 所有目录list List childrenList = (List) attachment.get("children"); if (CollectionUtils.isNotEmpty(childrenList)) { for (Map childMap : childrenList) { String childName = (String) childMap.get("name"); childMap.put("path", "--");//这里隐藏目录路径 List> list = (List>) childMap.get("children"); for (Map map : list) { String fileName = map.get("name"); if(StringUtils.isNotBlank(fileName)){ String SignedUrl = attachmentId + "/" + childName + "/" + fileName; String signedRes = fileUrlSigned(SignedUrl, timestamp); String path = DOMAIN+ "/public/offer/download/"+signedRes+"/"+timestamp+"/"+attachmentId+"/"+childName+"/"+fileName; map.put("path", path); } } } } } } // Validate the URL by checking its timestamp and signature public static boolean isUrlValid222(String url, long originalTimestamp) { try { // Extract the timestamp and signature from the URL URI uri = URI.create(url); String query = uri.getQuery(); String[] params = query.split("&"); long timestamp = 0; String signature = null; // Parse timestamp and signature for (String param : params) { if (param.startsWith("timestamp=")) { timestamp = Long.parseLong(param.split("=")[1]); } else if (param.startsWith("signature=")) { signature = param.split("=")[1]; } } // Check if the link is expired (10 minutes expiry) if (System.currentTimeMillis() - timestamp > EXPIRY_TIME) { return false; } // Verify the signature String originalUrl = url.split("&signature=")[0]; String expectedSignature = generateSignature(originalUrl); return expectedSignature.equals(signature); } catch (Exception e) { e.printStackTrace(); return false; } } // Validate the URL by checking its timestamp and signature public static boolean isUrlValid(String originalUrl, String signature, Long originalTimestamp) { try { originalUrl += TIME_KEY + originalTimestamp; // Check if the link is expired (10 minutes expiry) if (System.currentTimeMillis() - originalTimestamp > EXPIRY_TIME) { return false; } String expectedSignature = generateSignature(originalUrl); return expectedSignature.equals(signature); } catch (Exception e) { e.printStackTrace(); return false; } } // Convert byte array to Hex private static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02x", b)); } return sb.toString(); } // Generate a hash-based signature for the URL private static String generateSignature(String data) throws Exception { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest((data + SECRET_KEY).getBytes("UTF-8")); return bytesToHex(hash); } // Generate the signed URL public static String fileUrlSigned(String baseUrl, String timestamp) throws Exception { String urlWithTimestamp = baseUrl + TIME_KEY + timestamp; return generateSignature(urlWithTimestamp); } }