From dee9f245cd463669dbf707e05a2872d4466cbf9b Mon Sep 17 00:00:00 2001 From: peng <peng.com> Date: 星期四, 14 八月 2025 09:44:14 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/dev' into dev --- framework/src/main/java/cn/lili/utils/COSUtil.java | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 101 insertions(+), 15 deletions(-) diff --git a/framework/src/main/java/cn/lili/utils/COSUtil.java b/framework/src/main/java/cn/lili/utils/COSUtil.java index 23a2c23..6f1a28e 100644 --- a/framework/src/main/java/cn/lili/utils/COSUtil.java +++ b/framework/src/main/java/cn/lili/utils/COSUtil.java @@ -18,6 +18,11 @@ import com.tencent.cloud.cos.util.Jackson; import lombok.RequiredArgsConstructor; import org.apache.commons.collections4.CollectionUtils; +//import org.bytedeco.javacv.*; +//import org.bytedeco.opencv.global.opencv_imgproc; +//import org.bytedeco.opencv.opencv_core.Mat; +//import org.bytedeco.opencv.opencv_core.Size; +//import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; @@ -37,6 +42,100 @@ public class COSUtil { private final COSConfigProperty cosConfigProperty; + + /** + * 浠庣綉缁滆棰慤RL鎴彇绗竴绉掔敾闈紝杩斿洖MultipartFile绫诲瀷 + * @param videoUrl 缃戠粶瑙嗛鍦板潃 + * @param width 灏侀潰瀹藉害 + * @param height 灏侀潰楂樺害 + * @return 灏侀潰鍥剧墖鐨凪ultipartFile瀵硅薄 + * @throws Exception 澶勭悊寮傚父 + */ + public MultipartFile captureVideoCoverAsMultipart(String videoUrl, Integer width, Integer height) throws Exception { +// // 璁剧疆榛樿瀹介珮 +// int targetWidth = width != null && width > 0 ? width : 800; +// int targetHeight = height != null && height > 0 ? height : 600; +// +// // 鐢熸垚鍞竴鏂囦欢鍚嶏紙鐢ㄤ簬MultipartFile鐨勫師濮嬫枃浠跺悕锛� +// String fileName = UUID.randomUUID().toString() + ".jpg"; +// +// // 浣跨敤鍐呭瓨娴佸鐞嗗浘鐗囷紝閬垮厤涓存椂鏂囦欢 +// ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); +// FFmpegFrameGrabber grabber = null; +// +// try { +// // 鍒濆鍖栬棰戞姄鍙栧櫒 +// grabber = new FFmpegFrameGrabber(videoUrl); +// grabber.start(); +// +// // 瀹氫綅鍒扮涓�绉� +// grabber.setTimestamp(1000000); // 1绉� = 1,000,000寰 +// +// // 鑾峰彇瑙嗛甯� +// Frame frame = grabber.grabImage(); +// if (frame == null) { +// throw new RuntimeException("鏃犳硶鑾峰彇瑙嗛甯э紝鍙兘瑙嗛鏍煎紡涓嶆敮鎸佹垨URL鏃犳晥"); +// } +// +// // 杞崲涓篗at骞惰皟鏁村昂瀵� +// OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat(); +// Mat mat = converter.convert(frame); +// Mat resizedMat = new Mat(); +// opencv_imgproc.resize(mat, resizedMat, new Size(targetWidth, targetHeight)); +// +// // 灏嗗鐞嗗悗鐨勫抚鍐欏叆鍐呭瓨娴� +// Java2DFrameConverter java2dConverter = new Java2DFrameConverter(); +// ImageIO.write( +// java2dConverter.getBufferedImage(converter.convert(resizedMat)), +// "jpg", +// outputStream +// ); +// +// // 灏嗗唴瀛樻祦杞崲涓篗ultipartFile +// ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); +// return new MockMultipartFile( +// "file", // 琛ㄥ崟瀛楁鍚嶏紙鍙嚜瀹氫箟锛� +// fileName, // 鍘熷鏂囦欢鍚� +// "image/jpeg", // 鏂囦欢绫诲瀷 +// inputStream // 鏂囦欢娴� +// ); +// } finally { +// // 閲婃斁璧勬簮 +// if (grabber != null) { +// try { +// grabber.stop(); +// grabber.release(); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } +// outputStream.close(); +// } + return null; + } + + + + + /** + * 浠庡畬鏁碪RL涓彁鍙朇OS鏂囦欢key + * @param fullUrl 瀹屾暣URL + * @return COS鏂囦欢key + */ + public String extractFileKeyFromUrl(String fullUrl) { + // 鍘婚櫎鍗忚鍜屽煙鍚嶉儴鍒� + String endpoint = cosConfigProperty.getEndpoint(); + if (fullUrl.startsWith(endpoint)) { + return fullUrl.substring(endpoint.length() + 1); + } + // 濡傛灉URL鍖呭惈bucket鍚嶇О + String bucketUrl = "https://" + cosConfigProperty.getBucket() + "." + endpoint; + if (fullUrl.startsWith(bucketUrl)) { + return fullUrl.substring(bucketUrl.length() + 1); + } + // 濡傛灉宸茬粡鏄浉瀵硅矾寰勶紝鐩存帴杩斿洖 + return fullUrl; + } /** * 鑾峰彇sts涓存椂璁块棶鍑瘉 @@ -114,6 +213,7 @@ cosSTS.setStsEndTime(cosSTS.getStsStartTime() + cosConfigProperty.getDurationSeconds() - 30); cosSTS.setBucket(cosConfigProperty.getBucket()); cosSTS.setRegion(cosConfigProperty.getRegion()); + cosSTS.setEndpoint(cosConfigProperty.getEndpoint()); return cosSTS; } catch (Exception e) { e.printStackTrace(); @@ -224,21 +324,7 @@ * @return */ public String getPreviewUrl(String fileKey) { - COSClient cosClient = this.initClient(); - // 璁剧疆绛惧悕杩囨湡鏃堕棿(鍙��), 鑻ユ湭杩涜璁剧疆鍒欓粯璁や娇鐢� ClientConfig 涓殑绛惧悕杩囨湡鏃堕棿(1灏忔椂) - // 杩欓噷璁剧疆绛惧悕鍦ㄥ崐涓皬鏃跺悗杩囨湡 - Date expirationDate = new Date(System.currentTimeMillis() + cosConfigProperty.getUrlExpireMinute() * 60 * 1000); - // 濉啓鏈璇锋眰鐨勫弬鏁帮紝闇�涓庡疄闄呰姹傜浉鍚岋紝鑳藉闃叉鐢ㄦ埛绡℃敼姝ょ鍚嶇殑 HTTP 璇锋眰鐨勫弬鏁� - Map<String, String> params = new HashMap<String, String>(); - // 濉啓鏈璇锋眰鐨勫ご閮紝闇�涓庡疄闄呰姹傜浉鍚岋紝鑳藉闃叉鐢ㄦ埛绡℃敼姝ょ鍚嶇殑 HTTP 璇锋眰鐨勫ご閮� - Map<String, String> headers = new HashMap<String, String>(); - // 璇锋眰鐨� HTTP 鏂规硶锛屼笂浼犺姹傜敤 PUT锛屼笅杞借姹傜敤 GET锛屽垹闄よ姹傜敤 DELETE - HttpMethodName method = HttpMethodName.GET; - URL url = cosClient.generatePresignedUrl(cosConfigProperty.getBucket(), fileKey, expirationDate, method, headers, params); - System.out.println(url.toString()); - // 纭鏈繘绋嬩笉鍐嶄娇鐢� cosClient 瀹炰緥涔嬪悗锛屽叧闂嵆鍙� - cosClient.shutdown(); - return url.toString(); + return cosConfigProperty.getEndpoint() + "/" + fileKey; } -- Gitblit v1.8.0