Remove non-blocking calls for simplicity
This commit is contained in:
parent
e9439a8b9c
commit
5cd96ae106
@ -9,8 +9,7 @@ import io.ktor.client.statement.HttpResponse
|
|||||||
import io.ktor.client.statement.readBytes
|
import io.ktor.client.statement.readBytes
|
||||||
import io.ktor.http.HttpHeaders
|
import io.ktor.http.HttpHeaders
|
||||||
import io.ktor.http.HttpMethod
|
import io.ktor.http.HttpMethod
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.jetbrains.api.runtime.SpaceHttpClientWithCallContext
|
import space.jetbrains.api.runtime.SpaceHttpClientWithCallContext
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
@ -21,13 +20,13 @@ import kotlin.io.path.writeBytes
|
|||||||
import kotlin.io.path.writeText
|
import kotlin.io.path.writeText
|
||||||
import kotlin.streams.toList
|
import kotlin.streams.toList
|
||||||
|
|
||||||
suspend fun SpaceHttpClientWithCallContext.extractImage(
|
fun SpaceHttpClientWithCallContext.extractImage(
|
||||||
client: HttpClient,
|
client: HttpClient,
|
||||||
spaceUrl: String,
|
spaceUrl: String,
|
||||||
parent: Path,
|
parent: Path,
|
||||||
imageId: String,
|
imageId: String,
|
||||||
imageFileName: String,
|
imageFileName: String,
|
||||||
) {
|
) = runBlocking {
|
||||||
val request = HttpRequestBuilder().apply {
|
val request = HttpRequestBuilder().apply {
|
||||||
val token = callContext.tokenSource.token()
|
val token = callContext.tokenSource.token()
|
||||||
url("$spaceUrl/d/$imageId")
|
url("$spaceUrl/d/$imageId")
|
||||||
@ -39,27 +38,24 @@ suspend fun SpaceHttpClientWithCallContext.extractImage(
|
|||||||
file.writeBytes(response.readBytes())
|
file.writeBytes(response.readBytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
private val regex = """!\[(?<fileName>.*)]\(/d/(?<id>.*)\?f=0""".toRegex()
|
private val regex = """!\[(?<alt>.*)]\(/d/(?<id>.*)\?f=0""".toRegex()
|
||||||
|
|
||||||
suspend fun SpaceHttpClientWithCallContext.processDocument(client: HttpClient, spaceUrl: String, path: Path) {
|
fun SpaceHttpClientWithCallContext.processDocument(client: HttpClient, spaceUrl: String, path: Path) {
|
||||||
val documentBody = path.readText()
|
val documentBody = path.readText()
|
||||||
val logger = LoggerFactory.getLogger("space-document-extractor")
|
val logger = LoggerFactory.getLogger("space-document-extractor")
|
||||||
logger.info("Processing file $path...")
|
logger.info("Processing file $path...")
|
||||||
coroutineScope {
|
|
||||||
val newText = documentBody.replace(regex) {
|
val newText = documentBody.replace(regex) {
|
||||||
val id = it.groups["id"]?.value ?: error("Unexpected reference format: ${it.value}")
|
val id = it.groups["id"]?.value ?: error("Unexpected reference format: ${it.value}")
|
||||||
val fileName = it.groups["fileName"]?.value ?: id
|
val alt = it.groups["alt"]?.value
|
||||||
launch {
|
logger.info("Downloading image $id as images/$id")
|
||||||
logger.info("Downloading image $id")
|
extractImage(client, spaceUrl, path.parent, id, id)
|
||||||
extractImage(client, spaceUrl, path.parent, id, fileName)
|
"![$alt](images/$id"
|
||||||
}
|
|
||||||
"![](images/$fileName"
|
|
||||||
}
|
}
|
||||||
path.writeText(newText)
|
path.writeText(newText)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun SpaceHttpClientWithCallContext.processDirectory(
|
fun SpaceHttpClientWithCallContext.processDirectory(
|
||||||
client: HttpClient,
|
client: HttpClient,
|
||||||
spaceUrl: String,
|
spaceUrl: String,
|
||||||
path: Path,
|
path: Path,
|
||||||
|
Loading…
Reference in New Issue
Block a user