mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-07-04 03:14:40 -05:00
fix windows paths
This commit is contained in:
@@ -32,7 +32,10 @@ import okhttp3.OkHttpClient
|
|||||||
import okhttp3.Protocol
|
import okhttp3.Protocol
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
|
import okhttp3.ResponseBody.Companion.asResponseBody
|
||||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||||
|
import okio.buffer
|
||||||
|
import okio.source
|
||||||
import org.jetbrains.exposed.sql.insert
|
import org.jetbrains.exposed.sql.insert
|
||||||
import org.jetbrains.exposed.sql.insertAndGetId
|
import org.jetbrains.exposed.sql.insertAndGetId
|
||||||
import org.jetbrains.exposed.sql.select
|
import org.jetbrains.exposed.sql.select
|
||||||
@@ -50,7 +53,7 @@ import java.io.File
|
|||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.io.FileNotFoundException
|
import java.io.FileNotFoundException
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.net.URL
|
import java.net.URLDecoder
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import java.util.zip.ZipFile
|
import java.util.zip.ZipFile
|
||||||
@@ -435,19 +438,28 @@ class LocalSource : HttpSource() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private object FileSystemInterceptor : Interceptor {
|
private object FileSystemInterceptor : Interceptor {
|
||||||
fun fakeUrlFrom(path: String) = "http://$path"
|
fun fakeUrlFrom(path: String): String = "http://$path"
|
||||||
|
|
||||||
private fun restoreFileUrl(markedFakeHttpUrl: String): String {
|
|
||||||
return markedFakeHttpUrl.replaceFirst("http:", "file:/")
|
private fun restoreFilePath(url: String): String {
|
||||||
|
val path = URLDecoder.decode(url.replaceFirst("http://", ""), "UTF-8")
|
||||||
|
|
||||||
|
// Windows
|
||||||
|
if (System.getProperty("os.name").lowercase().startsWith("win")) {
|
||||||
|
// convert paths like "c/Users/..." to "c:/Users/..."
|
||||||
|
return StringBuilder(path).insert(1, ":").toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
return "/$path"
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
val request = chain.request()
|
val request = chain.request()
|
||||||
val url = request.url
|
val url = request.url
|
||||||
val fileUrl = restoreFileUrl(url.toString())
|
val filePath = restoreFilePath(url.toString())
|
||||||
return try {
|
return try {
|
||||||
Response.Builder()
|
Response.Builder()
|
||||||
.body(URL(fileUrl).readBytes().toResponseBody())
|
.body(File(filePath).source().buffer().asResponseBody())
|
||||||
.code(200)
|
.code(200)
|
||||||
.message("Some file")
|
.message("Some file")
|
||||||
.protocol(Protocol.HTTP_1_0)
|
.protocol(Protocol.HTTP_1_0)
|
||||||
@@ -457,7 +469,7 @@ private object FileSystemInterceptor : Interceptor {
|
|||||||
Response.Builder()
|
Response.Builder()
|
||||||
.body("".toResponseBody())
|
.body("".toResponseBody())
|
||||||
.code(404)
|
.code(404)
|
||||||
.message(e.message ?: "File not found ($fileUrl)")
|
.message(e.message ?: "File not found ($filePath)")
|
||||||
.protocol(Protocol.HTTP_1_0)
|
.protocol(Protocol.HTTP_1_0)
|
||||||
.request(request)
|
.request(request)
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
Reference in New Issue
Block a user