From d9a98148b74300ff939c86710467fb7c10e0d5bc Mon Sep 17 00:00:00 2001 From: AviKav Date: Tue, 4 Aug 2020 21:44:59 -0400 Subject: [PATCH] Revert "Merge branch 'fix-4xx-caching' into expermential_buffer_size_knob" This reverts commit dcd05dc6a1c210fd005de2b4bb4d956ad5798c6d, reversing changes made to 3d4843debda638d6ce7e4734f973a6482a1d17d5. --- build.gradle | 158 +++++++++--------- .../kotlin/mdnet/base/server/ImageServer.kt | 33 +--- 2 files changed, 85 insertions(+), 106 deletions(-) diff --git a/build.gradle b/build.gradle index 25ce7a6..55ed557 100644 --- a/build.gradle +++ b/build.gradle @@ -1,79 +1,79 @@ -plugins { - id "java" - id "org.jetbrains.kotlin.jvm" version "1.3.72" - id "application" - id "com.github.johnrengelman.shadow" version "5.2.0" - id "com.diffplug.gradle.spotless" version "4.4.0" - id "dev.afanasev.sekret" version "0.0.3" -} - -group = "com.mangadex" -version = "git describe --tags --dirty".execute().text.trim() -mainClassName = "mdnet.base.Main" - -repositories { - mavenCentral() - jcenter() -} - -configurations { - runtime.exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core" - runtime.exclude group: "com.sun.mail", module: "javax.mail" -} - -dependencies { - compileOnly group: "dev.afanasev", name: "sekret-annotation", version: "0.0.3" - - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" - implementation "org.jetbrains.kotlin:kotlin-reflect" - - implementation group: "commons-io", name: "commons-io", version: "2.7" - - implementation group: "org.http4k", name: "http4k-core", version: "$http_4k_version" - implementation group: "org.http4k", name: "http4k-format-jackson", version: "$http_4k_version" - implementation group: "com.fasterxml.jackson.datatype", name: "jackson-datatype-jsr310", version: "2.11.1" - implementation group: "org.http4k", name: "http4k-client-apache", version: "$http_4k_version" - implementation group: "org.http4k", name: "http4k-server-netty", version: "$http_4k_version" - runtimeOnly group: "io.netty", name: "netty-tcnative-boringssl-static", version: "2.0.30.Final" - - implementation group: "ch.qos.logback", name: "logback-classic", version: "1.3.0-alpha4" - implementation group: "org.jetbrains.exposed", name: "exposed-core", version: "$exposed_version" - implementation group: "org.jetbrains.exposed", name: "exposed-dao", version: "$exposed_version" - implementation group: "org.jetbrains.exposed", name: "exposed-jdbc", version: "$exposed_version" - - implementation group: "org.xerial", name: "sqlite-jdbc", version: "3.30.1" - - implementation "com.goterl.lazycode:lazysodium-java:4.3.0" - implementation "net.java.dev.jna:jna:5.5.0" -} - -java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 -} - -spotless { - lineEndings 'UNIX' - java { - targetExclude("build/generated/**/*") - eclipse() - removeUnusedImports() - trimTrailingWhitespace() - endWithNewline() - } - kotlin { - ktlint() - trimTrailingWhitespace() - endWithNewline() - } -} - -tasks.register("generateVersion", Copy) { - def templateContext = [version: version] - inputs.properties templateContext - from "src/template/java" - into "$buildDir/generated/java" - expand templateContext -} - -sourceSets.main.java.srcDir generateVersion.outputs.files +plugins { + id "java" + id "org.jetbrains.kotlin.jvm" version "1.3.72" + id "application" + id "com.github.johnrengelman.shadow" version "5.2.0" + id "com.diffplug.gradle.spotless" version "4.4.0" + id "dev.afanasev.sekret" version "0.0.3" +} + +group = "com.mangadex" +version = "git describe --tags --dirty".execute().text.trim() +mainClassName = "mdnet.base.Main" + +repositories { + mavenCentral() + jcenter() +} + +configurations { + runtime.exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core" + runtime.exclude group: "com.sun.mail", module: "javax.mail" +} + +dependencies { + compileOnly group: "dev.afanasev", name: "sekret-annotation", version: "0.0.3" + + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + implementation "org.jetbrains.kotlin:kotlin-reflect" + + implementation group: "commons-io", name: "commons-io", version: "2.7" + + implementation group: "org.http4k", name: "http4k-core", version: "$http_4k_version" + implementation group: "org.http4k", name: "http4k-format-jackson", version: "$http_4k_version" + implementation group: "com.fasterxml.jackson.datatype", name: "jackson-datatype-jsr310", version: "2.11.1" + implementation group: "org.http4k", name: "http4k-client-apache", version: "$http_4k_version" + implementation group: "org.http4k", name: "http4k-server-netty", version: "$http_4k_version" + runtimeOnly group: "io.netty", name: "netty-tcnative-boringssl-static", version: "2.0.30.Final" + + implementation group: "ch.qos.logback", name: "logback-classic", version: "1.3.0-alpha4" + implementation group: "org.jetbrains.exposed", name: "exposed-core", version: "$exposed_version" + implementation group: "org.jetbrains.exposed", name: "exposed-dao", version: "$exposed_version" + implementation group: "org.jetbrains.exposed", name: "exposed-jdbc", version: "$exposed_version" + + implementation group: "org.xerial", name: "sqlite-jdbc", version: "3.30.1" + + implementation "com.goterl.lazycode:lazysodium-java:4.2.6" + implementation "net.java.dev.jna:jna:5.5.0" +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +spotless { + lineEndings 'UNIX' + java { + targetExclude("build/generated/**/*") + eclipse() + removeUnusedImports() + trimTrailingWhitespace() + endWithNewline() + } + kotlin { + ktlint() + trimTrailingWhitespace() + endWithNewline() + } +} + +tasks.register("generateVersion", Copy) { + def templateContext = [version: version] + inputs.properties templateContext + from "src/template/java" + into "$buildDir/generated/java" + expand templateContext +} + +sourceSets.main.java.srcDir generateVersion.outputs.files diff --git a/src/main/kotlin/mdnet/base/server/ImageServer.kt b/src/main/kotlin/mdnet/base/server/ImageServer.kt index 4b7b92a..3ab7d10 100644 --- a/src/main/kotlin/mdnet/base/server/ImageServer.kt +++ b/src/main/kotlin/mdnet/base/server/ImageServer.kt @@ -138,25 +138,13 @@ class ImageServer( } val imageId = printHexString(rc4Bytes) - // These shouldn't be var but I don't want to touch the original conditional logic - var snapshot = cache.getUnsafe(imageId.toCacheId()) - var imageDatum = synchronized(database) { + val snapshot = cache.getUnsafe(imageId.toCacheId()) + val imageDatum = synchronized(database) { transaction(database) { ImageDatum.findById(imageId) } } - // TODO: Cleanup messy conditionals - - // Ensure that cached file isn't a non-image from the Great Cache Propagation - val flaresFault = imageDatum != null && imageDatum.contentType.isImageMimetype().not() - - if (flaresFault) { - snapshot?.close(); snapshot = null - cache.removeUnsafe(imageId.toCacheId()); imageDatum = null - LOGGER.warn { "Removing cache file for $sanitizedUri left over the Great Cache Propagation" } - } - if (snapshot != null && imageDatum != null) { request.handleCacheHit(sanitizedUri, getRc4(rc4Bytes), snapshot, imageDatum) } else { @@ -186,8 +174,6 @@ class ImageServer( } } - private fun String.isImageMimetype() = this.toLowerCase().startsWith("image/") - private fun Request.handleCacheHit(sanitizedUri: String, cipher: Cipher, snapshot: DiskLruCache.Snapshot, imageDatum: ImageDatum): Response { // our files never change, so it's safe to use the browser cache return if (this.header("If-Modified-Since") != null) { @@ -231,26 +217,19 @@ class ImageServer( val mdResponse = client(Request(Method.GET, "${serverSettings.imageServer}$sanitizedUri")) - val contentType = mdResponse.header("Content-Type") - val contentLength = mdResponse.header("Content-Length") - val lastModified = mdResponse.header("Last-Modified") - if (mdResponse.status != Status.OK) { LOGGER.trace { "Upstream query for $sanitizedUri errored with status ${mdResponse.status}" } - mdResponse.close() - return Response(mdResponse.status) - } - contentType!! - - if (!contentType.isImageMimetype()) { - LOGGER.trace { "Upstream query for $sanitizedUri returned bad mimetype $contentType" } mdResponse.close() return Response(mdResponse.status) } LOGGER.trace { "Upstream query for $sanitizedUri succeeded" } + val contentType = mdResponse.header("Content-Type")!! + val contentLength = mdResponse.header("Content-Length") + val lastModified = mdResponse.header("Last-Modified") + val editor = cache.editUnsafe(imageId.toCacheId()) // A null editor means that this file is being written to