Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.wolfdev
- import io.ktor.application.call
- import io.ktor.freemarker.FreeMarkerContent
- import io.ktor.http.ContentType
- import io.ktor.http.cio.websocket.send
- import io.ktor.http.content.PartData
- import io.ktor.http.content.forEachPart
- import io.ktor.http.content.streamProvider
- import io.ktor.http.formUrlEncode
- import io.ktor.locations.KtorExperimentalLocationsAPI
- import io.ktor.locations.Location
- import io.ktor.locations.get
- import io.ktor.locations.post
- import io.ktor.request.receiveMultipart
- import io.ktor.response.respond
- import io.ktor.routing.Route
- import io.ktor.websocket.webSocket
- import kotlinx.coroutines.CoroutineDispatcher
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.withContext
- import kotlinx.coroutines.yield
- import java.io.File
- import java.io.InputStream
- import java.io.OutputStream
- import java.net.URI
- import java.net.http.HttpClient
- import java.net.http.HttpRequest
- import java.net.http.HttpResponse
- @KtorExperimentalLocationsAPI
- @Location("/contact")
- class Contact
- @KtorExperimentalLocationsAPI
- fun Route.contact(nonce: String) {
- var result = "none"
- post<Contact> {
- val multiPart = call.receiveMultipart()
- var email = ""
- var name = ""
- var subject = ""
- var message = ""
- var captcha = ""
- val files = arrayListOf<File>()
- multiPart.forEachPart {part ->
- when(part) {
- is PartData.FormItem -> {
- when(part.name) {
- "email" -> email = part.value
- "name" -> name = part.value
- "subject" -> subject = part.value
- "message" -> message = part.value
- "g-recaptcha-response" -> captcha = part.value
- "otherSubject" -> {
- if(subject == "other")
- subject = part.value
- }
- }
- }
- is PartData.FileItem -> {
- println(part.originalFileName)
- if(!part.originalFileName.isNullOrEmpty()) {
- val file = File(createTempDir(), part.originalFileName)
- part.streamProvider().use {input -> file.outputStream().buffered().use {output -> input.copyToSuspend(output)}}
- files.add(file)
- }
- }
- }
- part.dispose
- }
- val data = mapOf(
- "secret" to "6LdkTooUAAAAABbt-oRhRBQjyam1zHbZpmKuO87A",
- "response" to captcha
- )
- val response = sendParams("https://www.google.com/recaptcha/api/siteverify", data)
- if(email != "" && name != "" && subject != "" && message != "" && response.contains(Regex("\"success\": true"))) {
- result = Mailer.sendMail(email, name, subject, message, files, false)
- }
- call.respond(FreeMarkerContent("contact.ftl", mapOf("nonce" to nonce)))
- }
- webSocket("/ws") {
- send(result)
- }
- get<Contact> {
- call.respond(FreeMarkerContent("contact.ftl", mapOf("nonce" to nonce)))
- }
- }
- suspend fun InputStream.copyToSuspend(
- out: OutputStream,
- bufferSize: Int = DEFAULT_BUFFER_SIZE,
- yieldSize: Int = 4 * 1024 * 1024,
- dispatcher: CoroutineDispatcher = Dispatchers.IO
- ) = withContext(dispatcher) {
- val buffer = ByteArray(bufferSize)
- var bytesCopied = 0L
- var bytesAfterYield = 0L
- while(true) {
- val bytes = read(buffer).takeIf {it >= 0} ?: break
- out.write(buffer, 0, bytes)
- if(bytesAfterYield >= yieldSize) {
- yield()
- bytesAfterYield %= yieldSize
- }
- bytesCopied += bytes
- bytesAfterYield += bytes
- }
- return@withContext bytesCopied
- }
Add Comment
Please, Sign In to add comment