refactor(randomphoto): 重构随机图片服务
- 添加日志记录功能 - 优化图片下载和压缩逻辑 - 更新发送图片的方法 - 增加调试和生产环境的日志配置
This commit is contained in:
parent
c76c906881
commit
c9209d1b25
@ -1,17 +1,20 @@
|
|||||||
package info.alinadace.sakuramiki.service.randomphoto
|
package info.alinadace.sakuramiki.service.randomphoto
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON
|
|
||||||
import com.mavis.service.AlistService
|
import com.mavis.service.AlistService
|
||||||
import info.alinadace.sakuramiki.annotation.BotFunction
|
import info.alinadace.sakuramiki.annotation.BotFunction
|
||||||
import info.alinadace.sakuramiki.service.Service
|
import info.alinadace.sakuramiki.service.Service
|
||||||
import info.alinadace.sakuramiki.service.randomphoto.domain.FileList
|
import info.alinadace.sakuramiki.service.randomphoto.domain.FileList
|
||||||
import info.alinadace.sakuramiki.service.randomphoto.domain.SingleFile
|
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.FriendMessageEvent
|
||||||
import io.github.kloping.qqbot.api.v2.GroupMessageEvent
|
import io.github.kloping.qqbot.api.v2.GroupMessageEvent
|
||||||
import io.github.kloping.qqbot.api.v2.MessageV2Event
|
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.ex.PlainText
|
||||||
import io.github.kloping.qqbot.entities.qqpd.Channel
|
|
||||||
import jakarta.annotation.Resource
|
import jakarta.annotation.Resource
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import java.io.ByteArrayInputStream
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Kane
|
* @author Kane
|
||||||
@ -20,9 +23,14 @@ import jakarta.annotation.Resource
|
|||||||
@BotFunction(FriendMessageEvent::class, GroupMessageEvent::class)
|
@BotFunction(FriendMessageEvent::class, GroupMessageEvent::class)
|
||||||
class RandomPhotoService : Service<MessageV2Event> {
|
class RandomPhotoService : Service<MessageV2Event> {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val log: Logger = LoggerFactory.getLogger(this::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
lateinit var alistService: AlistService
|
lateinit var alistService: AlistService
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务入口
|
* 服务入口
|
||||||
*/
|
*/
|
||||||
@ -55,16 +63,18 @@ class RandomPhotoService : Service<MessageV2Event> {
|
|||||||
event.send("文件获取失败")
|
event.send("文件获取失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val map = HashMap<String, Any>()
|
log.info("文件信息: {}", file)
|
||||||
map["file_type"] = 1
|
event.send("图片下载中...")
|
||||||
map["url"] = file.data.rawURL
|
val imageByte = ImageUtil.download(file.data.rawURL)
|
||||||
map["srv_send_msg"] = false
|
log.info("图片大小: {}", imageByte?.size)
|
||||||
val sendFile = event.bot.userBaseV2.sendFile(event.sender.cid, JSON.toJSONString(map), Channel.SEND_MESSAGE_HEADERS)
|
if (imageByte == null) {
|
||||||
val message = HashMap<String, Any>()
|
event.send("图片下载失败")
|
||||||
message["content"] = " "
|
return
|
||||||
message["msg_type"] = 7
|
}
|
||||||
message["media"] = sendFile.file_info
|
val inputStream = ByteArrayInputStream(imageByte)
|
||||||
message["event_id"] = event.id
|
val outputStream = ImageUtil.compression(inputStream)
|
||||||
event.bot.userBaseV2.send(event.sender.cid, JSON.toJSONString(message), Channel.SEND_MESSAGE_HEADERS)
|
val image = Image(outputStream.toByteArray())
|
||||||
|
log.info("图片压缩后大小: {}", image.bytes.size)
|
||||||
|
event.send(image)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,12 @@ package info.alinadace.sakuramiki.util
|
|||||||
|
|
||||||
import net.coobird.thumbnailator.Thumbnails
|
import net.coobird.thumbnailator.Thumbnails
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
|
import java.awt.Color
|
||||||
|
import java.awt.image.BufferedImage
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
|
import java.io.InputStream
|
||||||
|
import javax.imageio.ImageIO
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Kane
|
* @author Kane
|
||||||
@ -12,6 +16,20 @@ import java.io.ByteArrayOutputStream
|
|||||||
class ImageUtil {
|
class ImageUtil {
|
||||||
companion object {
|
companion object {
|
||||||
private const val MAX_SIZE = 1024 * 1024
|
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 {
|
fun compression(bytes: ByteArray): ByteArray {
|
||||||
val size = bytes.size
|
val size = bytes.size
|
||||||
var ratio = if (size > MAX_SIZE) {
|
var ratio = if (size > MAX_SIZE) {
|
||||||
|
@ -13,3 +13,8 @@ mybatis-plus:
|
|||||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||||
bot:
|
bot:
|
||||||
is-sandbox: true
|
is-sandbox: true
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
root: debug
|
||||||
|
sql: debug
|
||||||
|
@ -10,3 +10,8 @@ spring:
|
|||||||
password: FydsaZsFm5w26We6
|
password: FydsaZsFm5w26We6
|
||||||
bot:
|
bot:
|
||||||
is-sandbox: false
|
is-sandbox: false
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
root: error
|
||||||
|
sql: error
|
||||||
|
Loading…
Reference in New Issue
Block a user