From 1ed123596cf9cfe6c9d86f3c8a5b5cc5a1b0a859 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 11 May 2023 16:24:38 +0600 Subject: [PATCH] add microutils and support of native platforms --- CHANGELOG.md | 2 ++ build.gradle | 18 ++++++++++++++- gradle/libs.versions.toml | 8 +++++++ .../dev/inmo/saucenaoapi/SauceNaoAPI.kt | 8 ++++--- .../dev/inmo/saucenaoapi/utils/MPPFile.kt | 23 ++++++++++++++++--- .../inmo/saucenaoapi/utils/ActualMPPFile.kt | 18 --------------- .../inmo/saucenaoapi/utils/ActualMPPFile.kt | 14 ----------- 7 files changed, 52 insertions(+), 39 deletions(-) delete mode 100644 src/jsMain/kotlin/dev/inmo/saucenaoapi/utils/ActualMPPFile.kt delete mode 100644 src/jvmMain/kotlin/dev/inmo/saucenaoapi/utils/ActualMPPFile.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cf96e9..d7cb945 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.16.0 +Add `MicroUtils` as used micro utils + * Versions: * `Kotlin`: `1.8.21` * `Ktor`: `2.3.0` diff --git a/build.gradle b/build.gradle index 7b70024..ec8d212 100644 --- a/build.gradle +++ b/build.gradle @@ -28,11 +28,19 @@ repositories { } kotlin { - jvm() + jvm { + compilations.main { + kotlinOptions { + jvmTarget = "1.8" + } + } + } js(IR) { browser() nodejs() } + linuxX64() + mingwX64() sourceSets { @@ -44,6 +52,9 @@ kotlin { api libs.kt.serialization api libs.klock api libs.ktor.client + api libs.microutils.common + api libs.microutils.ktor.common + api libs.microutils.mimetypes } } commonTest { @@ -65,3 +76,8 @@ kotlin { } } } + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a224cae..d1db6a8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -7,6 +7,10 @@ kt-coroutines = "1.6.4" klock = "3.4.0" ktor = "2.3.0" +microutils = "0.18.2" + +okio = "3.3.0" + gh-release = "2.4.1" [libraries] @@ -20,6 +24,10 @@ klock = { module = "com.soywiz.korlibs.klock:klock", version.ref = "klock" } ktor-client = { module = "io.ktor:ktor-client-core", version.ref = "ktor" } ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" } +microutils-common = { module = "dev.inmo:micro_utils.common", version.ref = "microutils" } +microutils-ktor-common = { module = "dev.inmo:micro_utils.ktor.common", version.ref = "microutils" } +microutils-mimetypes = { module = "dev.inmo:micro_utils.mime_types", version.ref = "microutils" } + buildscript-kt-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kt" } buildscript-kt-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kt" } buildscript-gh-release = { module = "com.github.breadmoirai:github-release", version.ref = "gh-release" } diff --git a/src/commonMain/kotlin/dev/inmo/saucenaoapi/SauceNaoAPI.kt b/src/commonMain/kotlin/dev/inmo/saucenaoapi/SauceNaoAPI.kt index bdac5d0..994b5a6 100644 --- a/src/commonMain/kotlin/dev/inmo/saucenaoapi/SauceNaoAPI.kt +++ b/src/commonMain/kotlin/dev/inmo/saucenaoapi/SauceNaoAPI.kt @@ -1,5 +1,7 @@ package dev.inmo.saucenaoapi +import dev.inmo.micro_utils.common.MPPFile +import dev.inmo.micro_utils.ktor.common.input import dev.inmo.saucenaoapi.exceptions.TooManyRequestsException import dev.inmo.saucenaoapi.exceptions.sauceNaoAPIException import dev.inmo.saucenaoapi.models.* @@ -189,7 +191,7 @@ data class SauceNaoAPI( resultsCount: Int? = null, minSimilarity: Float? = null ): SauceNaoAnswer = request( - file.input, + file.input(), file.contentType, resultsCount, minSimilarity @@ -206,7 +208,7 @@ data class SauceNaoAPI( resultsCount: Int? = null, minSimilarity: Float? = null ): SauceNaoAnswer = requestByDBs( - file.input, + file.input(), file.contentType, db, dbs, @@ -228,7 +230,7 @@ data class SauceNaoAPI( resultsCount: Int? = null, minSimilarity: Float? = null ): SauceNaoAnswer = requestByMasks( - file.input, + file.input(), file.contentType, mask, excludedMask, diff --git a/src/commonMain/kotlin/dev/inmo/saucenaoapi/utils/MPPFile.kt b/src/commonMain/kotlin/dev/inmo/saucenaoapi/utils/MPPFile.kt index 620eca5..641e11b 100644 --- a/src/commonMain/kotlin/dev/inmo/saucenaoapi/utils/MPPFile.kt +++ b/src/commonMain/kotlin/dev/inmo/saucenaoapi/utils/MPPFile.kt @@ -1,9 +1,26 @@ package dev.inmo.saucenaoapi.utils +import dev.inmo.micro_utils.common.MPPFile +import dev.inmo.micro_utils.common.filename +import dev.inmo.micro_utils.ktor.common.input +import dev.inmo.micro_utils.mime_types.KnownMimeTypes +import dev.inmo.micro_utils.mime_types.getMimeType import io.ktor.http.ContentType import io.ktor.utils.io.core.Input -expect class MPPFile +@Deprecated( + "MPPFile from microutils is preferable since 0.16.0", + ReplaceWith("MPPFile", "dev.inmo.micro_utils.common.MPPFile") +) +typealias MPPFile = MPPFile -expect val MPPFile.input: Input -expect val MPPFile.contentType: ContentType +@Deprecated( + "input() from microutils is preferable since 0.16.0", + ReplaceWith("this.input()", "dev.inmo.micro_utils.ktor.common.input") +) +val MPPFile.input: Input + get() = input() +val MPPFile.contentType: ContentType + get() = ContentType.parse( + getMimeType(stringWithExtension = filename.extension) ?.raw ?: KnownMimeTypes.Any.raw + ) diff --git a/src/jsMain/kotlin/dev/inmo/saucenaoapi/utils/ActualMPPFile.kt b/src/jsMain/kotlin/dev/inmo/saucenaoapi/utils/ActualMPPFile.kt deleted file mode 100644 index 5ed733e..0000000 --- a/src/jsMain/kotlin/dev/inmo/saucenaoapi/utils/ActualMPPFile.kt +++ /dev/null @@ -1,18 +0,0 @@ -package dev.inmo.saucenaoapi.utils - -import io.ktor.http.ContentType -import io.ktor.utils.io.core.ByteReadPacket -import io.ktor.utils.io.core.Input -import org.khronos.webgl.Int8Array -import org.w3c.files.File -import org.w3c.files.FileReaderSync - -actual typealias MPPFile = File - -actual val MPPFile.input: Input - get() { - val reader = FileReaderSync() - return ByteReadPacket(Int8Array(reader.readAsArrayBuffer(this)) as ByteArray) - } -actual val MPPFile.contentType: ContentType - get() = ContentType.parse(type) diff --git a/src/jvmMain/kotlin/dev/inmo/saucenaoapi/utils/ActualMPPFile.kt b/src/jvmMain/kotlin/dev/inmo/saucenaoapi/utils/ActualMPPFile.kt deleted file mode 100644 index 73fd4e5..0000000 --- a/src/jvmMain/kotlin/dev/inmo/saucenaoapi/utils/ActualMPPFile.kt +++ /dev/null @@ -1,14 +0,0 @@ -package dev.inmo.saucenaoapi.utils - -import io.ktor.http.ContentType -import io.ktor.utils.io.core.Input -import io.ktor.utils.io.streams.asInput -import java.io.File -import java.nio.file.Files - -actual typealias MPPFile = File - -actual val MPPFile.input: Input - get() = inputStream().asInput() -actual val MPPFile.contentType: ContentType - get() = ContentType.parse(Files.probeContentType(toPath()))