-
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.
Allow programatically uploading files to EtherealGambi
- Loading branch information
1 parent
7a94e36
commit 9401900
Showing
7 changed files
with
107 additions
and
7 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
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
8 changes: 7 additions & 1 deletion
8
backend/src/main/kotlin/net/perfectdreams/etherealgambi/backend/EtherealGambiLauncher.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
45 changes: 45 additions & 0 deletions
45
.../main/kotlin/net/perfectdreams/etherealgambi/backend/routes/api/v1/PostUploadFileRoute.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,45 @@ | ||
package net.perfectdreams.etherealgambi.backend.routes.api.v1 | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import net.perfectdreams.etherealgambi.backend.EtherealGambi | ||
import net.perfectdreams.etherealgambi.data.DefaultImageVariantPreset | ||
import net.perfectdreams.etherealgambi.data.ScaleDownToWidthImageVariantPreset | ||
import net.perfectdreams.etherealgambi.data.api.UploadFileRequest | ||
import net.perfectdreams.etherealgambi.data.api.UploadFileResponse | ||
import net.perfectdreams.etherealgambi.data.api.requests.ImageVariantsRequest | ||
import net.perfectdreams.etherealgambi.data.api.responses.ImageVariantsResponse | ||
import net.perfectdreams.sequins.ktor.BaseRoute | ||
import java.io.File | ||
import java.util.* | ||
|
||
class PostUploadFileRoute(val m: EtherealGambi) : BaseRoute("/api/v1/upload") { | ||
override suspend fun onRequest(call: ApplicationCall) { | ||
val authorization = call.request.header("Authorization") | ||
if (authorization == null) { | ||
call.respondText(Json.encodeToString(UploadFileResponse.Unauthorized)) | ||
return | ||
} | ||
|
||
val authorizationToken = m.config.authorizationTokens.firstOrNull { it.token == authorization } | ||
if (authorizationToken == null) { | ||
call.respondText(Json.encodeToString(UploadFileResponse.Unauthorized)) | ||
return | ||
} | ||
|
||
val request = Json.decodeFromString<UploadFileRequest>(call.receiveText()) | ||
|
||
val fileData = Base64.getDecoder().decode(request.dataBase64) | ||
|
||
val file = File(m.files, request.path) | ||
val folder = file.parentFile | ||
folder.mkdirs() | ||
file.writeBytes(fileData) | ||
|
||
call.respondText(Json.encodeToString(UploadFileResponse.Success)) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/main/kotlin/net/perfectdreams/etherealgambi/backend/utils/EtherealGambiConfig.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,14 @@ | ||
package net.perfectdreams.etherealgambi.backend.utils | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class EtherealGambiConfig( | ||
val authorizationTokens: List<AuthorizationToken> | ||
) { | ||
@Serializable | ||
data class AuthorizationToken( | ||
val name: String, | ||
val token: String | ||
) | ||
} |
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
18 changes: 18 additions & 0 deletions
18
common/src/main/kotlin/net/perfectdreams/etherealgambi/data/api/UploadFile.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,18 @@ | ||
package net.perfectdreams.etherealgambi.data.api | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class UploadFileRequest( | ||
val path: String, | ||
val dataBase64: String | ||
) | ||
|
||
@Serializable | ||
sealed class UploadFileResponse { | ||
@Serializable | ||
data object Success : UploadFileResponse() | ||
|
||
@Serializable | ||
data object Unauthorized : UploadFileResponse() | ||
} |