From 389754a39af298c7b6c9892b4037b02d18325b74 Mon Sep 17 00:00:00 2001 From: Samarium <28302241+Samarium150@users.noreply.github.com> Date: Sun, 21 Nov 2021 17:45:54 +0800 Subject: [PATCH] feat: add scheduled cache cleaning --- .editorconfig | 10 ++++++++ build.gradle.kts | 2 +- src/main/kotlin/MiraiConsoleThrowIt.kt | 4 ++-- src/main/kotlin/Utils.kt | 12 +++++++++- src/main/kotlin/command/Clean.kt | 32 +++++++++++++++++++++++--- src/main/kotlin/command/Throw.kt | 6 ++--- 6 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..6be21af --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +insert_final_newline = true + +[*.{kt, kts}] +max_line_length = 120 +tab_width = 4 +ij_continuation_indent_size = 4 +indent_size = 4 diff --git a/build.gradle.kts b/build.gradle.kts index 43d09dc..ac74778 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "com.github.samarium150" -version = "1.0.0" +version = "1.1.0" repositories { mavenLocal() diff --git a/src/main/kotlin/MiraiConsoleThrowIt.kt b/src/main/kotlin/MiraiConsoleThrowIt.kt index 6ec5a8d..d488e9f 100644 --- a/src/main/kotlin/MiraiConsoleThrowIt.kt +++ b/src/main/kotlin/MiraiConsoleThrowIt.kt @@ -27,7 +27,7 @@ object MiraiConsoleThrowIt: KotlinPlugin( JvmPluginDescription( id = "com.github.samarium150.mirai-console-throw-it", name = "mirai-console-throw-it", - version = "1.0.0" + version = "1.1.0" ) { author("Samarium150") }, @@ -50,4 +50,4 @@ object MiraiConsoleThrowIt: KotlinPlugin( Clean.unregister() logger.info("Plugin mirai-console-throw-it unloaded") } -} \ No newline at end of file +} diff --git a/src/main/kotlin/Utils.kt b/src/main/kotlin/Utils.kt index aa917b0..0d20984 100644 --- a/src/main/kotlin/Utils.kt +++ b/src/main/kotlin/Utils.kt @@ -25,6 +25,7 @@ import java.awt.geom.Ellipse2D import java.awt.image.BufferedImage import java.io.File import java.net.URL +import java.time.ZoneId import java.util.* import javax.imageio.ImageIO @@ -71,4 +72,13 @@ object Utils { } } } -} \ No newline at end of file + + fun getMidnight(): Date { + val date = Date.from( + Date().toInstant().atZone(ZoneId.systemDefault()) + .toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant() + ) + date.time += 24 * 60 * 60 * 1000 + return date + } +} diff --git a/src/main/kotlin/command/Clean.kt b/src/main/kotlin/command/Clean.kt index c1ac435..3cb0358 100644 --- a/src/main/kotlin/command/Clean.kt +++ b/src/main/kotlin/command/Clean.kt @@ -23,6 +23,7 @@ import net.mamoe.mirai.console.command.SimpleCommand import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors import net.mamoe.mirai.console.util.ConsoleExperimentalApi import java.io.File +import java.util.* object Clean: SimpleCommand( MiraiConsoleThrowIt, @@ -34,11 +35,36 @@ object Clean: SimpleCommand( @ExperimentalCommandDescriptors override val prefixOptional: Boolean = true + private val cacheDir = File(MiraiConsoleThrowIt.dataPath) + + private val scheduler = Timer() + + private val task = object : TimerTask() { + override fun run() { + Utils.cleanupDirectory(cacheDir) + .onFailure { + MiraiConsoleThrowIt.logger.error("定时清理缓存失败", it) + } + .onSuccess { + MiraiConsoleThrowIt.logger.info("定时清理缓存成功") + } + } + } + + init { + scheduler.scheduleAtFixedRate(task, Utils.getMidnight(), 24 * 60 * 60 * 1000) + } + + @Suppress("unused") + fun finalize() { + task.cancel() + scheduler.cancel() + } + @Suppress("unused") @Handler suspend fun CommandSender.handle() { - val dir = File(MiraiConsoleThrowIt.dataPath) - Utils.cleanupDirectory(dir) + Utils.cleanupDirectory(cacheDir) .onFailure { exception -> run { MiraiConsoleThrowIt.logger.error(exception) @@ -49,4 +75,4 @@ object Clean: SimpleCommand( sendMessage("清理成功") } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/command/Throw.kt b/src/main/kotlin/command/Throw.kt index 2c3d770..c073988 100644 --- a/src/main/kotlin/command/Throw.kt +++ b/src/main/kotlin/command/Throw.kt @@ -18,7 +18,7 @@ package com.github.samarium150.command import com.github.samarium150.MiraiConsoleThrowIt import com.github.samarium150.Utils -import net.mamoe.mirai.console.command.CommandSender +import net.mamoe.mirai.console.command.CommandSenderOnMessage import net.mamoe.mirai.console.command.SimpleCommand import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors import net.mamoe.mirai.console.util.ConsoleExperimentalApi @@ -53,7 +53,7 @@ object Throw: SimpleCommand( @Suppress("unused") @Handler - suspend fun CommandSender.handle(target: User) { + suspend fun CommandSenderOnMessage<*>.handle(target: User) { val resultPath = MiraiConsoleThrowIt.dataPath + target.id + ".png" val result = File(resultPath) if (!result.exists()) { @@ -70,4 +70,4 @@ object Throw: SimpleCommand( sendMessage(target.uploadImage(result)) } } -} \ No newline at end of file +}