refactor(randomphoto): 重构随机图片服务

- 添加日志记录功能
- 优化图片下载和压缩逻辑
- 更新发送图片的方法
- 增加调试和生产环境的日志配置
This commit is contained in:
Grand-cocoa 2025-01-13 11:39:32 +08:00
parent c76c906881
commit c9209d1b25
4 changed files with 51 additions and 13 deletions

View File

@ -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<MessageV2Event> {
companion object {
val log: Logger = LoggerFactory.getLogger(this::class.java)
}
@Resource
lateinit var alistService: AlistService
/**
* 服务入口
*/
@ -55,16 +63,18 @@ class RandomPhotoService : Service<MessageV2Event> {
event.send("文件获取失败")
return
}
val map = HashMap<String, Any>()
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<String, Any>()
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)
}
}

View File

@ -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) {

View File

@ -13,3 +13,8 @@ mybatis-plus:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
bot:
is-sandbox: true
logging:
level:
root: debug
sql: debug

View File

@ -10,3 +10,8 @@ spring:
password: FydsaZsFm5w26We6
bot:
is-sandbox: false
logging:
level:
root: error
sql: error