From 33c2cbf1ce7932653465e6d15016f3e27a4ca9a3 Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 1 Jan 2021 10:18:14 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20Framework-API=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=20MessageSender,=20=E7=A7=BB=E9=99=A4=20'getMessageIdentify()'?= =?UTF-8?q?=20=E6=96=B9=E6=B3=95,=20=E8=B0=83=E6=95=B4=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=96=B9=E6=B3=95;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Change] MessageSender 移除 'getMessageIdentify()' 方法, 更改 'getImageUrl(String)' 方法的返回值(String -> URL), 为 'getImageAsInputStream' 添加默认方法; --- .../bot/framework/message/MessageSender.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/MessageSender.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/MessageSender.java index f8db272..a2d283c 100644 --- a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/MessageSender.java +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/MessageSender.java @@ -21,6 +21,9 @@ import net.lamgc.cgj.bot.framework.Platform; import net.lamgc.cgj.bot.framework.message.exception.UploadImageException; import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; /** * 消息发送者. @@ -67,19 +70,12 @@ public interface MessageSender { */ int sendMessage(Message message); - /** - * 获取消息标识, 用于回复/撤回功能 - * @param msgId 消息Id, 通过 {@link #sendMessage(Message)} 发送消息获得, 或从 MessageEvent 中获得. - * @return 如果成功获取, 返回非null值, 如果不存在或无法获取, 返回 null. - */ - String getMessageIdentify(int msgId); - /** * 获取图片Url * @param imageIdentify 图片标识 * @return 返回图片Url */ - String getImageUrl(String imageIdentify); + URL getImageUrl(String imageIdentify); /** * 获取图片输入流 @@ -87,7 +83,26 @@ public interface MessageSender { * @return 返回图片输入流. * @throws IOException 当输入流获取发生异常时可抛出. */ - InputStream getImageAsInputStream(String imageIdentify) throws IOException; + default InputStream getImageAsInputStream(String imageIdentify) throws IOException { + URL imageUrl = getImageUrl(imageIdentify); + if (imageUrl == null) { + return null; + } + URLConnection connection = imageUrl.openConnection(); + connection.setDoInput(true); + if(connection instanceof HttpURLConnection) { + HttpURLConnection httpConnection = (HttpURLConnection) connection; + httpConnection.connect(); + if(httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { + throw new IOException("Http response error: " + + httpConnection.getResponseCode() + " " + httpConnection.getResponseMessage()); + } + return httpConnection.getInputStream(); + } else { + connection.connect(); + return connection.getInputStream(); + } + } /** * 上传图片.