From 85668ff207051ec82386fa72c4dbd779d3e2e63a Mon Sep 17 00:00:00 2001 From: carbotaniuman Date: Fri, 1 Oct 2021 02:19:58 +0000 Subject: [PATCH] Minor cleanup --- build.gradle | 13 +- settings.gradle | 14 --- .../java/mdnet/cache/CachingInputStream.java | 118 ------------------ .../kotlin/mdnet/cache/CachingInputStream.kt | 114 +++++++++++++++++ src/main/kotlin/mdnet/server/ImageHandler.kt | 2 +- src/main/kotlin/mdnet/server/ImageServer.kt | 8 +- 6 files changed, 122 insertions(+), 147 deletions(-) delete mode 100644 src/main/java/mdnet/cache/CachingInputStream.java create mode 100644 src/main/kotlin/mdnet/cache/CachingInputStream.kt diff --git a/build.gradle b/build.gradle index 19ea743..7ea9d2c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,8 +1,8 @@ plugins { id "jacoco" id "java" - id "org.jetbrains.kotlin.jvm" version "1.5.10" - id "org.jetbrains.kotlin.kapt" version "1.5.10" + id "org.jetbrains.kotlin.jvm" version "1.5.21" + id "org.jetbrains.kotlin.kapt" version "1.5.21" id "application" id "com.github.johnrengelman.shadow" version "5.2.0" id "com.diffplug.spotless" version "5.8.2" @@ -93,18 +93,12 @@ java { tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { kotlinOptions { + freeCompilerArgs = ["-Xjsr305=strict"] jvmTarget = "1.8" } } spotless { - java { - targetExclude("build/generated/**/*") - eclipse() - removeUnusedImports() - trimTrailingWhitespace() - endWithNewline() - } kotlin { ktlint("0.40.0").userData(["disabled_rules": "no-wildcard-imports"]) licenseHeaderFile "license_header" @@ -120,7 +114,6 @@ tasks.register("generateVersion", Copy) { into "$buildDir/generated/java" expand templateContext } -//tasks.named(":kaptKotlin").dependsOn generateVersion compileJava.dependsOn generateVersion sourceSets.main.java.srcDir generateVersion diff --git a/settings.gradle b/settings.gradle index 935730c..6353639 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,15 +1 @@ -pluginManagement { - repositories { - gradlePluginPortal() - google() - } - resolutionStrategy { - eachPlugin { - if (requested.id.id == "com.squareup.sqldelight") { - useModule("com.squareup.sqldelight:gradle-plugin:${requested.version}") - } - } - } -} - rootProject.name = 'mangadex_at_home' diff --git a/src/main/java/mdnet/cache/CachingInputStream.java b/src/main/java/mdnet/cache/CachingInputStream.java deleted file mode 100644 index 9d25e7b..0000000 --- a/src/main/java/mdnet/cache/CachingInputStream.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package mdnet.cache; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.io.input.ProxyInputStream; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.concurrent.ExecutorService; - -import static org.apache.commons.io.IOUtils.EOF; - -public class CachingInputStream extends ProxyInputStream { - private final OutputStream cache; - private final ExecutorService executor; - private final Runnable onClose; - private boolean eofReached = false; - - public CachingInputStream(InputStream response, ExecutorService executor, OutputStream cache, Runnable onClose) { - super(response); - this.executor = executor; - this.cache = cache; - this.onClose = onClose; - } - - @Override - public void close() throws IOException { - if (eofReached) { - try { - in.close(); - } catch (IOException ignored) { - } - try { - cache.close(); - } catch (IOException ignored) { - } - onClose.run(); - } else { - executor.submit(() -> { - try { - IOUtils.copy(in, cache); - } catch (IOException ignored) { - } finally { - try { - in.close(); - } catch (IOException ignored) { - } - try { - cache.close(); - } catch (IOException ignored) { - } - onClose.run(); - } - }); - } - } - - @Override - public int read() throws IOException { - final int ch = super.read(); - if (ch != EOF) { - try { - cache.write(ch); - } catch (IOException ignored) { - // don't let write failures affect the image loading - } - } else { - eofReached = true; - } - return ch; - } - - @Override - public int read(final byte[] bts, final int st, final int end) throws IOException { - final int n = super.read(bts, st, end); - if (n != EOF) { - try { - cache.write(bts, st, n); - } catch (IOException ignored) { - // don't let write failures affect the image loading - } - } else { - eofReached = true; - } - return n; - } - - @Override - public int read(final byte[] bts) throws IOException { - final int n = super.read(bts); - if (n != EOF) { - try { - cache.write(bts, 0, n); - } catch (IOException ignored) { - // don't let write failures affect the image loading - } - } else { - eofReached = true; - } - return n; - } -} diff --git a/src/main/kotlin/mdnet/cache/CachingInputStream.kt b/src/main/kotlin/mdnet/cache/CachingInputStream.kt new file mode 100644 index 0000000..8e13a6d --- /dev/null +++ b/src/main/kotlin/mdnet/cache/CachingInputStream.kt @@ -0,0 +1,114 @@ +/* +Mangadex@Home +Copyright (c) 2020, MangaDex Network +This file is part of MangaDex@Home. + +MangaDex@Home is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +MangaDex@Home is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this MangaDex@Home. If not, see . +*/ +package mdnet.cache + +import org.apache.commons.io.IOUtils +import org.apache.commons.io.input.ProxyInputStream +import java.io.IOException +import java.io.InputStream +import java.io.OutputStream +import java.lang.Runnable +import java.util.concurrent.ExecutorService +import kotlin.Throws + +class CachingInputStream( + response: InputStream?, + private val executor: ExecutorService, + private val cache: OutputStream, + private val onClose: Runnable +) : ProxyInputStream(response) { + private var eofReached = false + + @Throws(IOException::class) + override fun close() { + if (eofReached) { + try { + `in`.close() + } catch (ignored: IOException) { + } + try { + cache.close() + } catch (ignored: IOException) { + } + onClose.run() + } else { + executor.submit { + try { + IOUtils.copy(`in`, cache) + } catch (ignored: IOException) { + } finally { + try { + `in`.close() + } catch (ignored: IOException) { + } + try { + cache.close() + } catch (ignored: IOException) { + } + onClose.run() + } + } + } + } + + @Throws(IOException::class) + override fun read(): Int { + val ch = super.read() + if (ch != IOUtils.EOF) { + try { + cache.write(ch) + } catch (ignored: IOException) { + // don't let write failures affect the image loading + } + } else { + eofReached = true + } + return ch + } + + @Throws(IOException::class) + override fun read(bts: ByteArray, st: Int, end: Int): Int { + val n = super.read(bts, st, end) + if (n != IOUtils.EOF) { + try { + cache.write(bts, st, n) + } catch (ignored: IOException) { + // don't let write failures affect the image loading + } + } else { + eofReached = true + } + return n + } + + @Throws(IOException::class) + override fun read(bts: ByteArray): Int { + val n = super.read(bts) + if (n != IOUtils.EOF) { + try { + cache.write(bts, 0, n) + } catch (ignored: IOException) { + // don't let write failures affect the image loading + } + } else { + eofReached = true + } + return n + } +} diff --git a/src/main/kotlin/mdnet/server/ImageHandler.kt b/src/main/kotlin/mdnet/server/ImageHandler.kt index de6a785..d164d7e 100644 --- a/src/main/kotlin/mdnet/server/ImageHandler.kt +++ b/src/main/kotlin/mdnet/server/ImageHandler.kt @@ -174,7 +174,7 @@ class ImageServer( companion object { private val LOGGER = LoggerFactory.getLogger(ImageServer::class.java) - private fun String.isImageMimetype() = this.toLowerCase().startsWith("image/") + private fun String.isImageMimetype() = this.lowercase().startsWith("image/") private fun baseHandler(): Filter = CachingFilters.Response.MaxAge(Clock.systemUTC(), Constants.MAX_AGE_CACHE) diff --git a/src/main/kotlin/mdnet/server/ImageServer.kt b/src/main/kotlin/mdnet/server/ImageServer.kt index 12532a8..8aac83c 100644 --- a/src/main/kotlin/mdnet/server/ImageServer.kt +++ b/src/main/kotlin/mdnet/server/ImageServer.kt @@ -86,7 +86,7 @@ fun getServer( val circuited = ResilienceFilters.CircuitBreak( circuitBreaker, - isError = { r: Response -> !r.status.successful } + isError = { r: Response -> r.status.serverError } ) val upstream = ClientFilters.MicrometerMetrics.RequestTimer(registry) @@ -102,9 +102,9 @@ fun getServer( FunctionCounter.builder( "client.sent", - statistics, - { it.bytesSent.get().toDouble() } - ).baseUnit(BaseUnits.BYTES).register(registry) + statistics + ) { it.bytesSent.get().toDouble() } + .baseUnit(BaseUnits.BYTES).register(registry) val verifier = TokenVerifier( tokenKey = remoteSettings.tokenKey,