Skip to content

Commit

Permalink
一時ファイルの操作関連の処理を最適化
Browse files Browse the repository at this point in the history
  • Loading branch information
kosugikun committed Mar 29, 2023
1 parent d1dc852 commit cc2b1f0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/resourceBundles.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/main/java/dev/cosgy/textToSpeak/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Bot(val waiter: EventWaiter, val config: BotConfig, val settingsManager: S
log.warn("JDAのシャットダウンが中断されました。")
}

dictionary?.close()
// 一時ファイルを削除
try {
FileUtils.cleanDirectory(File("tmp"))
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/cosgy/textToSpeak/BotConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class BotConfig(private val prompt: Prompt) {
maxMessageCount = config.getInt("maxmessagecount")
winJTalkDir = config.getString("winjtalkdir")
helpToDm = config.getBoolean("helptodm")
dBots = ownerId == 334091398263341056L
dBots = ownerId == 334091398263341056
var write = false

// validate bot token
Expand Down
43 changes: 16 additions & 27 deletions src/main/java/dev/cosgy/textToSpeak/audio/VoiceCreation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.apache.commons.io.FileUtils
import org.slf4j.LoggerFactory
import java.io.*
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.text.BreakIterator
import java.util.*
Expand Down Expand Up @@ -93,8 +94,8 @@ class VoiceCreation( // 各種設定の値を保持するためのフィール
// テキストファイルを作成するメソッド
@Throws(FileNotFoundException::class, UnsupportedEncodingException::class)
private fun createTmpTextFile(guildId: String, fileId: String, message: String): String {
val filePath = "tmp" + File.separator + guildId + File.separator + fileId + ".txt"
PrintWriter(filePath, characterCode).use { writer -> writer.write(message) }
val filePath = Paths.get("tmp", guildId, "$fileId.txt").toString()
PrintWriter(filePath, characterCode).use { it.write(message) }
return filePath
}

Expand Down Expand Up @@ -136,22 +137,13 @@ class VoiceCreation( // 各種設定の値を保持するためのフィール
}

// 必要なディレクトリを作成するメソッド
@Throws(IOException::class)
private fun createDirectories(guildId: String) {
createDirectory("tmp")
createDirectory("tmp" + File.separator + guildId)
createDirectory("wav")
createDirectory("wav" + File.separator + guildId)
}

// ディレクトリを作成するメゾット
@Throws(IOException::class)
private fun createDirectory(directory: String) {
val path = Paths.get(directory)
if (!Files.exists(path)) {
Files.createDirectory(path)
logger.info("Created directory: $directory")
}
private fun createDirectories(guildId: String) {
val tmpPath: Path = Paths.get("tmp", guildId)
val wavPath: Path = Paths.get("wav", guildId)
Files.createDirectories(tmpPath)
Files.createDirectories(wavPath)
}

// ギルドに関連する一時ファイルや音声ファイルを削除するメソッド
Expand All @@ -170,20 +162,17 @@ class VoiceCreation( // 各種設定の値を保持するためのフィール
}
}

val voices: ArrayList<String>
// 利用可能な音声名を取得するメソッド
val voices: List<String>
get() {
val filter = FilenameFilter { _: File?, str: String -> str.endsWith("htsvoice") }
val dir = File(voiceDirectory!!)
val list = dir.listFiles(filter)
val voices = ArrayList<String>()
for (file in list!!) {
voices.add(file.name.replace(".htsvoice", ""))
}
logger.debug("Available voices: $voices")
return voices
val voiceDir = File(requireNotNull(voiceDirectory) { "voiceDirectory is null" })
return voiceDir.listFiles { _, name -> name.endsWith(".htsvoice") }
?.map { file -> file.nameWithoutExtension }
?.toList()
.orEmpty()
.also { logger.debug("Available voices: $it") }
}


companion object {
private val logger = LoggerFactory.getLogger(VoiceCreation::class.java)
private val IS_WINDOWS = System.getProperty("os.name").lowercase(Locale.getDefault()).startsWith("win")
Expand Down

0 comments on commit cc2b1f0

Please sign in to comment.