Skip to content

Commit 180dbb4

Browse files
committed
refactor(yogurt): use ktfs 0.2.0 and modern DSL
1 parent d8a18b0 commit 180dbb4

9 files changed

Lines changed: 22 additions & 23 deletions

File tree

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ktor = "3.4.2"
1313

1414
milky = "1.2.0"
1515
acidify-codec = "0.1.0"
16-
ktfs = "0.1.1"
16+
ktfs = "0.2.0"
1717
kompress = "1.3.1"
1818
qr-matrix = "0.1.0"
1919
xmlutil = "1.0.0-rc2"

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/Initialization.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import org.ntqqrev.yogurt.YogurtApp.t
1515
import org.ntqqrev.yogurt.util.logHandler
1616

1717
suspend fun Application.initializePC(): Bot = withFs {
18-
val sessionStore: SessionStore = if (exists(sessionStorePath)) {
18+
val sessionStore: SessionStore = if (sessionStorePath.exists) {
1919
SessionStore.fromJson(sessionStorePath.readText())
2020
} else SessionStore.empty()
2121

2222
var signProvider: SignProvider
2323
var appInfo: AppInfo
2424

2525
fun readCustomAppInfo(): AppInfo {
26-
return if (exists(customAppInfoPath)) {
26+
return if (customAppInfoPath.exists) {
2727
AppInfo.fromJson(customAppInfoPath.readText())
2828
} else {
2929
throw IllegalStateException("未在 $customAppInfoPath 下找到自定义 AppInfo 文件")
@@ -88,7 +88,7 @@ suspend fun Application.initializeAndroid(): AndroidBot = withFs {
8888
require(config.protocol.uin != 0L && config.protocol.password.isNotEmpty()) {
8989
"使用 Android 协议登录时,请在配置文件中填写 uin 和 password 字段"
9090
}
91-
val sessionStore: AndroidSessionStore = if (exists(androidSessionStorePath)) {
91+
val sessionStore: AndroidSessionStore = if (androidSessionStorePath.exists) {
9292
AndroidSessionStore.fromJson(androidSessionStorePath.readText()).takeIf {
9393
it.uin == config.protocol.uin && it.password == config.protocol.password
9494
} ?: run {
@@ -103,7 +103,7 @@ suspend fun Application.initializeAndroid(): AndroidBot = withFs {
103103
password = config.protocol.password
104104
).also {
105105
t.println("未找到 Android SessionStore,正在创建新的 SessionStore 并保存到文件...")
106-
androidSessionStorePath.write(it.toJson())
106+
androidSessionStorePath.writeText(it.toJson())
107107
}
108108
val signProvider: AndroidSignProvider = if (!config.protocol.androidUseLegacySign) {
109109
AndroidUrlSignProvider(config.protocol.signApiUrl)
@@ -123,7 +123,7 @@ suspend fun Application.initializeAndroid(): AndroidBot = withFs {
123123
val appInfo: AndroidAppInfo = when (config.protocol.version) {
124124
"fetched" -> throw IllegalStateException("Android 协议不支持通过 Sign API 获取 AppInfo,请使用内置版本或自定义版本")
125125

126-
"custom" -> if (exists(customAppInfoPath)) {
126+
"custom" -> if (customAppInfoPath.exists) {
127127
AndroidAppInfo.fromJson(customAppInfoPath.readText())
128128
} else {
129129
throw IllegalStateException("未在 $customAppInfoPath 下找到自定义 AppInfo 文件")

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/YogurtApp.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ object YogurtApp {
6565
Milky Version: ${BuildKonfig.milkyVersion} ($milkyVersion)
6666
Build Time: ${BuildKonfig.buildTime}
6767
Listen Address: ${config.milky.http.host}:${config.milky.http.port}${config.milky.http.prefix}
68-
Data Directory: ${withFs { resolve(Path(".")) }}
68+
Data Directory: ${withFs { Path(".").absolute }}
6969
""".trimIndent()
7070
)
7171

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/config/Common.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ val jsonModule = Json {
7575
}
7676

7777
fun loadConfigAndUpdate(): YogurtConfigV3 = withFs {
78-
if (!exists(configPath)) {
78+
if (!configPath.exists) {
7979
val defaultConfig = YogurtConfigV3()
80-
configPath.write(jsonModule.encodeToString(defaultConfig))
81-
println("配置文件已生成于 ${resolve(configPath)}")
80+
configPath.writeText(jsonModule.encodeToString(defaultConfig))
81+
println("配置文件已生成于 ${configPath.absolute}")
8282
println("请根据需要进行修改,修改完成后按 Enter 键继续...")
8383
readln()
8484
}
@@ -91,7 +91,7 @@ fun loadConfigAndUpdate(): YogurtConfigV3 = withFs {
9191
else -> error("不支持的配置版本 $configVersion")
9292
}
9393

94-
configPath.write(jsonModule.encodeToString(config))
94+
configPath.writeText(jsonModule.encodeToString(config))
9595

9696
return@withFs config
9797
}

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/scripting/Configure.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ private suspend fun QuickJs.loadScripts() = withFs {
210210
.resolve<AbstractBot>()
211211
.createLogger("ScriptLoader")
212212

213-
if (!exists(scriptsPath)) {
213+
if (!scriptsPath.exists) {
214214
createDirectories(scriptsPath)
215215
}
216216

217-
val scripts = list(scriptsPath)
217+
val scripts = scriptsPath.children
218218
.filter { it.name.endsWith(".yogurtx.js") }
219219
.map {
220220
ctx.async {

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/util/QRCodeUtils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ fun Application.configureQRCodeDisplay() = launch {
5151
""".trimIndent()
5252
)
5353
withFs {
54-
qrCodePath.write(it.png)
55-
t.println("二维码文件已保存至 ${resolve(qrCodePath)}")
54+
qrCodePath.writeBytes(it.png)
55+
t.println("二维码文件已保存至 ${qrCodePath.absolute}")
5656
}
5757
}
5858
}

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/util/ResourceUtils.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ suspend fun resolveUri(uri: String): MediaSource = withContext(Dispatchers.IO) {
2525
when {
2626
uri.startsWith("file://") -> withFs {
2727
val filePath = Path(uri.removePrefix("file://").decodeURLPart())
28-
if (!exists(filePath)) {
28+
if (!filePath.exists) {
2929
throw IOException("File not found: $filePath")
3030
}
3131
LocalFileMediaSource(filePath)
@@ -58,9 +58,9 @@ fun createTempFile(kind: String, ext: String = "tmp"): Path = withFs {
5858
SystemTemporaryDirectory,
5959
"yogurt-$kind-${Random.nextLong().toULong().toString(16)}.$ext",
6060
)
61-
} while (exists(candidate))
61+
} while (candidate.exists)
6262
// Touch the file
63-
sink(candidate).buffered().use { }
63+
candidate.writeText("")
6464
candidate
6565
}
6666

@@ -77,8 +77,7 @@ abstract class LazyMediaSource : MediaSource() {
7777

7878
class LocalFileMediaSource(val path: Path) : LazyMediaSource() {
7979
override val size: Long = withFs {
80-
metadataOrNull(path)?.size
81-
?: throw IOException("File not found: $path")
80+
path.metadataOrNull?.size ?: throw IOException("File not found: $path")
8281
}
8382

8483
override fun openRawSource(): RawSource = withFs {

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/util/SecurityUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import kotlinx.io.files.Path
44
import org.ntqqrev.ktfs.withFs
55

66
val isDockerEnv: Boolean by lazy {
7-
withFs { exists(Path("/.dockerenv")) }
7+
withFs { Path("/.dockerenv").exists }
88
}

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/util/SessionStoreUtils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ fun Application.configureSessionStoreAutoSave() = launch {
1717
if (isPC) {
1818
bot.eventFlow.filterIsInstance<SessionStoreUpdatedEvent>().collect {
1919
logger.i { "SessionStore 已更新,正在保存至文件..." }
20-
withFs { sessionStorePath.write(it.sessionStore.toJson()) }
20+
withFs { sessionStorePath.writeText(it.sessionStore.toJson()) }
2121
}
2222
} else if (isAndroid) {
2323
bot.eventFlow.filterIsInstance<AndroidSessionStoreUpdatedEvent>().collect {
2424
logger.i { "Android SessionStore 已更新,正在保存至文件..." }
25-
withFs { androidSessionStorePath.write(it.sessionStore.toJson()) }
25+
withFs { androidSessionStorePath.writeText(it.sessionStore.toJson()) }
2626
}
2727
}
2828
}

0 commit comments

Comments
 (0)