diff --git a/src/main/kotlin/info/alinadace/sakuramiki/service/randomphoto/RandomPhotoService.kt b/src/main/kotlin/info/alinadace/sakuramiki/service/randomphoto/RandomPhotoService.kt index b4c9c0b..41b94eb 100644 --- a/src/main/kotlin/info/alinadace/sakuramiki/service/randomphoto/RandomPhotoService.kt +++ b/src/main/kotlin/info/alinadace/sakuramiki/service/randomphoto/RandomPhotoService.kt @@ -1,17 +1,20 @@ package info.alinadace.sakuramiki.service.randomphoto -import com.alibaba.fastjson.JSON import com.mavis.service.AlistService import info.alinadace.sakuramiki.annotation.BotFunction import info.alinadace.sakuramiki.service.Service import info.alinadace.sakuramiki.service.randomphoto.domain.FileList import info.alinadace.sakuramiki.service.randomphoto.domain.SingleFile +import info.alinadace.sakuramiki.util.ImageUtil import io.github.kloping.qqbot.api.v2.FriendMessageEvent import io.github.kloping.qqbot.api.v2.GroupMessageEvent import io.github.kloping.qqbot.api.v2.MessageV2Event +import io.github.kloping.qqbot.entities.ex.Image import io.github.kloping.qqbot.entities.ex.PlainText -import io.github.kloping.qqbot.entities.qqpd.Channel import jakarta.annotation.Resource +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import java.io.ByteArrayInputStream /** * @author Kane @@ -20,9 +23,14 @@ import jakarta.annotation.Resource @BotFunction(FriendMessageEvent::class, GroupMessageEvent::class) class RandomPhotoService : Service { + companion object { + val log: Logger = LoggerFactory.getLogger(this::class.java) + } + @Resource lateinit var alistService: AlistService + /** * 服务入口 */ @@ -55,16 +63,18 @@ class RandomPhotoService : Service { event.send("文件获取失败") return } - val map = HashMap() - map["file_type"] = 1 - map["url"] = file.data.rawURL - map["srv_send_msg"] = false - val sendFile = event.bot.userBaseV2.sendFile(event.sender.cid, JSON.toJSONString(map), Channel.SEND_MESSAGE_HEADERS) - val message = HashMap() - message["content"] = " " - message["msg_type"] = 7 - message["media"] = sendFile.file_info - message["event_id"] = event.id - event.bot.userBaseV2.send(event.sender.cid, JSON.toJSONString(message), Channel.SEND_MESSAGE_HEADERS) + log.info("文件信息: {}", file) + event.send("图片下载中...") + val imageByte = ImageUtil.download(file.data.rawURL) + log.info("图片大小: {}", imageByte?.size) + if (imageByte == null) { + event.send("图片下载失败") + return + } + val inputStream = ByteArrayInputStream(imageByte) + val outputStream = ImageUtil.compression(inputStream) + val image = Image(outputStream.toByteArray()) + log.info("图片压缩后大小: {}", image.bytes.size) + event.send(image) } } diff --git a/src/main/kotlin/info/alinadace/sakuramiki/util/ImageUtil.kt b/src/main/kotlin/info/alinadace/sakuramiki/util/ImageUtil.kt index f7952db..e0b8d37 100644 --- a/src/main/kotlin/info/alinadace/sakuramiki/util/ImageUtil.kt +++ b/src/main/kotlin/info/alinadace/sakuramiki/util/ImageUtil.kt @@ -2,8 +2,12 @@ package info.alinadace.sakuramiki.util import net.coobird.thumbnailator.Thumbnails import okhttp3.Request +import java.awt.Color +import java.awt.image.BufferedImage import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream +import java.io.InputStream +import javax.imageio.ImageIO /** * @author Kane @@ -12,6 +16,20 @@ import java.io.ByteArrayOutputStream class ImageUtil { companion object { private const val MAX_SIZE = 1024 * 1024 + fun compression(stream: InputStream): ByteArrayOutputStream { + val read = ImageIO.read(stream) + val image = read.getScaledInstance(1920, 1080, java.awt.Image.SCALE_DEFAULT) + val tag = BufferedImage(1920, 1080, BufferedImage.TYPE_INT_RGB) + val graphics = tag.graphics + graphics.color = Color.GREEN + graphics.drawImage(image, 0, 0, null) + graphics.dispose() + return ByteArrayOutputStream().also { + ImageIO.write(tag, "PNG", it) + } + } + + @Deprecated(message = "此方法被证实压缩大文件后依旧无法发出") fun compression(bytes: ByteArray): ByteArray { val size = bytes.size var ratio = if (size > MAX_SIZE) { diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index e8295d2..1151e59 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -13,3 +13,8 @@ mybatis-plus: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl bot: is-sandbox: true + +logging: + level: + root: debug + sql: debug diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index bd26353..7ccf1a1 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -10,3 +10,8 @@ spring: password: FydsaZsFm5w26We6 bot: is-sandbox: false + +logging: + level: + root: error + sql: error