Skip to content

Commit 762344b

Browse files
authored
Added support for fetching data of multiple files (#44)
Signed-off-by: Arnau Mora <[email protected]>
1 parent 4124f11 commit 762344b

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

src/main/kotlin/com/arnyminerz/escalaralcoiaicomtat/backend/server/endpoints/files/RequestFileEndpoint.kt

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,51 @@ import io.ktor.server.application.call
1313
import io.ktor.server.plugins.origin
1414
import io.ktor.server.util.getValue
1515
import io.ktor.util.pipeline.PipelineContext
16+
import java.io.FileNotFoundException
1617
import java.nio.file.Files
1718
import java.security.MessageDigest
1819
import kotlinx.coroutines.Dispatchers
1920
import kotlinx.coroutines.withContext
21+
import org.json.JSONObject
2022

2123
object RequestFileEndpoint : EndpointBase() {
2224
private const val DEFAULT_HTTP_PORT = 80
2325

2426
private val digest = MessageDigest.getInstance(MessageDigestAlgorithm.SHA_256)
2527

26-
override suspend fun PipelineContext<Unit, ApplicationCall>.endpoint() {
27-
val uuid: String by call.parameters
28-
29-
val file = Storage.find(uuid) ?: return respondFailure(Errors.FileNotFound)
28+
private suspend fun PipelineContext<Unit, ApplicationCall>.getDataFor(uuid: String): JSONObject {
29+
val file = Storage.find(uuid) ?: throw FileNotFoundException("Could not find file with uuid $uuid")
3030
val downloadAddress = call.request.origin.let { p ->
3131
val port = p.serverPort.takeIf { it != DEFAULT_HTTP_PORT }?.let { ":$it" } ?: ""
3232
"${p.scheme}://${p.serverHost}$port/download/$uuid"
3333
}
3434
val size = withContext(Dispatchers.IO) { Files.size(file.toPath()) }
3535

36-
respondSuccess(
37-
jsonOf(
38-
"hash" to HashUtils.getCheckSumFromFile(digest, file),
39-
"filename" to file.name,
40-
"download" to downloadAddress,
41-
"size" to size
42-
)
36+
return jsonOf(
37+
"hash" to HashUtils.getCheckSumFromFile(digest, file),
38+
"filename" to file.name,
39+
"download" to downloadAddress,
40+
"size" to size
4341
)
4442
}
43+
44+
override suspend fun PipelineContext<Unit, ApplicationCall>.endpoint() {
45+
val uuids: String by call.parameters
46+
val list = uuids.split(",")
47+
48+
// It's impossible that "list" has size 0
49+
try {
50+
respondSuccess(
51+
if (list.size <= 1) {
52+
getDataFor(uuids)
53+
} else {
54+
jsonOf(
55+
"files" to list.map { getDataFor(it) }
56+
)
57+
}
58+
)
59+
} catch (_: FileNotFoundException) {
60+
respondFailure(Errors.FileNotFound)
61+
}
62+
}
4563
}

src/main/kotlin/com/arnyminerz/escalaralcoiaicomtat/backend/server/plugins/Routing.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fun Application.configureEndpoints() {
8181
delete("/block/{blockId}") { DeleteBlockEndpoint.call(this) }
8282
patch("/block/{blockId}") { PatchBlockEndpoint.call(this) }
8383

84-
get("/file/{uuid}") { RequestFileEndpoint.call(this) }
84+
get("/file/{uuids}") { RequestFileEndpoint.call(this) }
8585
get("/download/{uuid}") { DownloadFileEndpoint.call(this) }
8686

8787
val enableImporter = EnvironmentVariables.Legacy.Importer.value

src/test/kotlin/com/arnyminerz/escalaralcoiaicomtat/backend/server/endpoints/files/TestFileFetching.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.arnyminerz.escalaralcoiaicomtat.backend.ServerDatabase
44
import com.arnyminerz.escalaralcoiaicomtat.backend.assertions.assertFailure
55
import com.arnyminerz.escalaralcoiaicomtat.backend.assertions.assertSuccess
66
import com.arnyminerz.escalaralcoiaicomtat.backend.database.entity.Area
7+
import com.arnyminerz.escalaralcoiaicomtat.backend.database.entity.Zone
78
import com.arnyminerz.escalaralcoiaicomtat.backend.server.DataProvider
89
import com.arnyminerz.escalaralcoiaicomtat.backend.server.base.ApplicationTestBase
910
import com.arnyminerz.escalaralcoiaicomtat.backend.server.error.Errors
@@ -36,4 +37,32 @@ class TestFileFetching : ApplicationTestBase() {
3637
assertFailure(Errors.FileNotFound)
3738
}
3839
}
40+
41+
@Test
42+
fun `test data multiple`() = test {
43+
val areaId = DataProvider.provideSampleArea()
44+
assertNotNull(areaId)
45+
val zoneId = DataProvider.provideSampleZone(areaId)
46+
assertNotNull(zoneId)
47+
48+
val area: Area = ServerDatabase.instance.query { Area[areaId] }
49+
val zone: Zone = ServerDatabase.instance.query { Zone[zoneId] }
50+
51+
get("/file/${area.image.name},${zone.image.name}").apply {
52+
assertSuccess { data ->
53+
assertNotNull(data)
54+
assertTrue(data.has("files"))
55+
56+
val files = data.getJSONArray("files")
57+
(0 until files.length())
58+
.map { files.getJSONObject(it) }
59+
.forEach { file ->
60+
assertTrue(file.has("hash"))
61+
assertTrue(file.has("filename"))
62+
assertTrue(file.has("download"))
63+
assertTrue(file.has("size"))
64+
}
65+
}
66+
}
67+
}
3968
}

0 commit comments

Comments
 (0)