Skip to content

Commit

Permalink
feat: スポイラーファイル投稿時、文字起こしもスポイラーする。日本語フォント対応 (#91)
Browse files Browse the repository at this point in the history
* feat: スポイラーファイル投稿時、文字起こしもスポイラーする。日本語フォント対応

* fix: dockerfileにおけるcurlインストール漏れ修正

* Update src/main/kotlin/com/jaoafa/vcspeaker/tts/MessageProcessor.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

---------

Co-authored-by: yuuaHP <identity@yuua.dev>
  • Loading branch information
book000 and yuuahp authored May 13, 2024
1 parent aa87022 commit 579996b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ FROM azul/zulu-openjdk-alpine:17-latest as runner
WORKDIR /app

# hadolint ignore=DL3018
RUN apk add --update --no-cache libstdc++ msttcorefonts-installer fontconfig tzdata && \
RUN apk add --update --no-cache libstdc++ msttcorefonts-installer fontconfig curl tzdata && \
cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && \
echo "Asia/Tokyo" > /etc/timezone && \
apk del tzdata && \
curl -O https://moji.or.jp/wp-content/ipafont/IPAexfont/IPAexfont00301.zip && \
mkdir -p /usr/share/fonts/ipa && \
unzip -o -d /usr/share/fonts/ipa/ IPAexfont00301.zip "*.ttf" && \
update-ms-fonts

COPY --from=builder /build/build/libs/vcspeaker-*.jar /app/vcspeaker-kt.jar
Expand Down
18 changes: 13 additions & 5 deletions src/main/kotlin/com/jaoafa/vcspeaker/tts/MessageProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ object MessageProcessor {
}

// 画像解析を行う
val isSpoiler = firstAttachment.isSpoiler
val binaryArray = firstAttachment.download()
try {
val visionApi = VisionApi()
Expand All @@ -69,17 +70,18 @@ object MessageProcessor {

// 画像解析結果を返信する
val editedImage = visionApi.drawTextAnnotations(binaryArray)
val filePath = editedImage.outputTempFile()
val filePath = editedImage.outputTempFile(isSpoiler)
val visionApiCounterStore = VisionApiCounterStore.get()

val requestedCount = visionApiCounterStore?.count ?: 0
val requestLimit = VisionApiCounterStore.VISION_API_LIMIT
val remainingRequests = requestLimit - requestedCount

val spoilerPrefixSuffix = if (isSpoiler) "||" else ""
message.reply {
embeds = mutableListOf(
EmbedBuilder().apply {
description = "```$embedDescription```"
description = "$spoilerPrefixSuffix```$embedDescription```$spoilerPrefixSuffix"
thumbnail = EmbedBuilder.Thumbnail().apply {
url = "attachment://${filePath.fileName}"
}
Expand All @@ -91,7 +93,12 @@ object MessageProcessor {
addFile(filePath)
}

return "$shortDescription を含む画像ファイル $moreFileRead"
return if (isSpoiler) {
// スポイラーファイルの場合は、スポイラー画像ファイルとして読み上げ
"スポイラー画像ファイル $moreFileRead"
} else {
"$shortDescription を含む画像ファイル $moreFileRead"
}
} catch (_: VisionApi.VisionApiLimitExceededException) {
// 月のリクエスト数が上限に達している場合、ファイル名のみを読み上げる
return "画像ファイル ${firstAttachment.filename} $moreFileRead"
Expand Down Expand Up @@ -133,8 +140,9 @@ object MessageProcessor {
}
}

private fun ImmutableImage.outputTempFile(): Path {
val tempFile = File.createTempFile("image", ".png")
private fun ImmutableImage.outputTempFile(isSpoiler: Boolean): Path {
val prefix = if (isSpoiler) "SPOILER_" else "Image_"
val tempFile = File.createTempFile(prefix, ".png")
this.output(PngWriter(), tempFile)
return tempFile.toPath()
}
Expand Down

0 comments on commit 579996b

Please sign in to comment.