-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
/files/{uuids}
endpoint (#188)
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
1 parent
e81504f
commit addd554
Showing
8 changed files
with
189 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/kotlin/server/endpoints/files/RequestFilesEndpoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package server.endpoints.files | ||
|
||
import io.ktor.server.plugins.origin | ||
import io.ktor.server.routing.RoutingContext | ||
import io.ktor.server.util.getValue | ||
import java.io.FileNotFoundException | ||
import java.nio.file.Files | ||
import java.security.MessageDigest | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import server.endpoints.EndpointBase | ||
import server.error.Errors | ||
import server.response.files.RequestFilesResponseData | ||
import server.response.respondFailure | ||
import server.response.respondSuccess | ||
import storage.HashUtils | ||
import storage.MessageDigestAlgorithm | ||
import storage.Storage | ||
|
||
object RequestFilesEndpoint : EndpointBase("/files/{uuids}") { | ||
private const val DEFAULT_HTTP_PORT = 80 | ||
|
||
private val digest = MessageDigest.getInstance(MessageDigestAlgorithm.SHA_256) | ||
|
||
private suspend fun RoutingContext.getDataFor(uuid: String): RequestFilesResponseData.Data { | ||
val file = Storage.find(uuid) ?: throw FileNotFoundException("Could not find file with uuid $uuid") | ||
val downloadAddress = call.request.origin.let { p -> | ||
val port = p.serverPort.takeIf { it != DEFAULT_HTTP_PORT }?.let { ":$it" } ?: "" | ||
"${p.scheme}://${p.serverHost}$port/download/$uuid" | ||
} | ||
val size = withContext(Dispatchers.IO) { Files.size(file.toPath()) } | ||
|
||
return RequestFilesResponseData.Data( | ||
uuid = uuid, | ||
hash = HashUtils.getCheckSumFromFile(digest, file), | ||
filename = file.name, | ||
download = downloadAddress, | ||
size = size | ||
) | ||
} | ||
|
||
override suspend fun RoutingContext.endpoint() { | ||
val uuids: String by call.parameters | ||
val list = uuids.split(",") | ||
|
||
// It's impossible that "list" has size 0 | ||
try { | ||
respondSuccess( | ||
data = if (list.size <= 1) { | ||
RequestFilesResponseData( | ||
listOf(getDataFor(uuids)) | ||
) | ||
} else { | ||
RequestFilesResponseData( | ||
list.map { getDataFor(it) } | ||
) | ||
} | ||
) | ||
} catch (_: FileNotFoundException) { | ||
respondFailure(Errors.FileNotFound) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/server/response/files/RequestFilesResponseData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package server.response.files | ||
|
||
import KoverIgnore | ||
import kotlinx.serialization.Serializable | ||
import server.response.ResponseData | ||
|
||
@KoverIgnore | ||
@Serializable | ||
data class RequestFilesResponseData( | ||
val files: List<Data> | ||
): ResponseData { | ||
@KoverIgnore | ||
@Serializable | ||
data class Data( | ||
val uuid: String, | ||
val hash: String, | ||
val filename: String, | ||
val download: String, | ||
val size: Long | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/test/kotlin/server/endpoints/files/TestFilesFetching.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package server.endpoints.files | ||
|
||
import ServerDatabase | ||
import assertions.assertFailure | ||
import assertions.assertSuccess | ||
import database.entity.Area | ||
import database.entity.Zone | ||
import server.DataProvider | ||
import server.base.ApplicationTestBase | ||
import server.error.Errors | ||
import server.response.files.RequestFilesResponseData | ||
import kotlin.test.Test | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertTrue | ||
|
||
class TestFilesFetching : ApplicationTestBase() { | ||
@Test | ||
fun `test data`() = test { | ||
val areaId = DataProvider.provideSampleArea(this) | ||
assertNotNull(areaId) | ||
|
||
val area: Area = ServerDatabase.instance.query { Area[areaId] } | ||
|
||
get("/files/${area.image.name}").apply { | ||
assertSuccess<RequestFilesResponseData> { data -> | ||
assertNotNull(data) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test data no extension`() = test { | ||
val areaId = DataProvider.provideSampleArea(this) | ||
assertNotNull(areaId) | ||
|
||
val area: Area = ServerDatabase.instance.query { Area[areaId] } | ||
|
||
get("/files/${area.image.nameWithoutExtension}").apply { | ||
assertSuccess<RequestFilesResponseData> { data -> | ||
assertNotNull(data) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test doesn't exist`() = test { | ||
get("/files/unknown").apply { | ||
assertFailure(Errors.FileNotFound) | ||
} | ||
} | ||
|
||
@Test | ||
fun `test data multiple`() = test { | ||
val areaId = DataProvider.provideSampleArea(this) | ||
assertNotNull(areaId) | ||
val zoneId = DataProvider.provideSampleZone(this, areaId) | ||
assertNotNull(zoneId) | ||
|
||
val area: Area = ServerDatabase.instance.query { Area[areaId] } | ||
val zone: Zone = ServerDatabase.instance.query { Zone[zoneId] } | ||
|
||
get("/files/${area.image.name},${zone.image.name}").apply { | ||
assertSuccess<RequestFilesResponseData> { data -> | ||
assertNotNull(data) | ||
|
||
val files = data.files | ||
assertTrue(files.isNotEmpty()) | ||
} | ||
} | ||
} | ||
} |