Skip to content

Commit

Permalink
Make basic XML file accepting endpoint that responds back with the co…
Browse files Browse the repository at this point in the history
…ntents of the file #1
  • Loading branch information
raghavkhanna18 committed Jul 21, 2020
1 parent 9ec9901 commit 5c0db14
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/main/kotlin/sbolParserApi/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ import io.ktor.features.DefaultHeaders
import io.ktor.gson.gson
import io.ktor.http.ContentType
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.PartData
import io.ktor.http.content.forEachPart
import io.ktor.http.content.streamProvider
import io.ktor.request.receiveMultipart
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.post
import io.ktor.routing.routing
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import org.apache.log4j.BasicConfigurator
import io.ktor.routing.*


// Main server
Expand All @@ -38,6 +45,40 @@ fun Application.module() {
get("/") {
call.respondText("Hello World", ContentType.Text.Html)
}

post("/upload") {
// Receive data
val multiPartData = call.receiveMultipart()

var fileContentType: ContentType
var fileBytes = byteArrayOf()
val propMaxByteSize = 1024 * 1024;
multiPartData.forEachPart { part ->
when (part) {
is PartData.FileItem -> {
if (part.headers["Content-Length"]?.toInt() ?: 0 > propMaxByteSize) { // Check data size
call.respond(HttpStatusCode.PayloadTooLarge, "File is too large")
} else {
// Receive data
part.streamProvider().use { input -> fileBytes = input.readBytes() }

if (fileBytes.size > propMaxByteSize) { // Check data size again
call.respond(HttpStatusCode.BadRequest, "Content-Length header incorrect and file is too large")
} else {
fileContentType = part.contentType ?: ContentType.Application.Any
println(fileContentType.toString())
println(fileBytes.size)
println(String(fileBytes))
call.respond(HttpStatusCode.OK, String(fileBytes))
}
}
}
else -> {
call.respond(HttpStatusCode.BadRequest, "Not a file upload")
}
}
}
}
}
}

Expand Down

0 comments on commit 5c0db14

Please sign in to comment.