Superloup10

Untitled

Nov 7th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.87 KB | None | 0 0
  1. package fr.wolfdev
  2.  
  3. import io.ktor.application.call
  4. import io.ktor.freemarker.FreeMarkerContent
  5. import io.ktor.http.ContentType
  6. import io.ktor.http.cio.websocket.send
  7. import io.ktor.http.content.PartData
  8. import io.ktor.http.content.forEachPart
  9. import io.ktor.http.content.streamProvider
  10. import io.ktor.http.formUrlEncode
  11. import io.ktor.locations.KtorExperimentalLocationsAPI
  12. import io.ktor.locations.Location
  13. import io.ktor.locations.get
  14. import io.ktor.locations.post
  15. import io.ktor.request.receiveMultipart
  16. import io.ktor.response.respond
  17. import io.ktor.routing.Route
  18. import io.ktor.websocket.webSocket
  19. import kotlinx.coroutines.CoroutineDispatcher
  20. import kotlinx.coroutines.Dispatchers
  21. import kotlinx.coroutines.withContext
  22. import kotlinx.coroutines.yield
  23. import java.io.File
  24. import java.io.InputStream
  25. import java.io.OutputStream
  26. import java.net.URI
  27. import java.net.http.HttpClient
  28. import java.net.http.HttpRequest
  29. import java.net.http.HttpResponse
  30.  
  31. @KtorExperimentalLocationsAPI
  32. @Location("/contact")
  33. class Contact
  34.  
  35. @KtorExperimentalLocationsAPI
  36. fun Route.contact(nonce: String) {
  37.     var result = "none"
  38.     post<Contact> {
  39.         val multiPart = call.receiveMultipart()
  40.         var email = ""
  41.         var name = ""
  42.         var subject = ""
  43.         var message = ""
  44.         var captcha = ""
  45.         val files = arrayListOf<File>()
  46.         multiPart.forEachPart {part ->
  47.             when(part) {
  48.                 is PartData.FormItem -> {
  49.                     when(part.name) {
  50.                         "email" -> email = part.value
  51.                         "name" -> name = part.value
  52.                         "subject" -> subject = part.value
  53.                         "message" -> message = part.value
  54.                         "g-recaptcha-response" -> captcha = part.value
  55.                         "otherSubject" -> {
  56.                             if(subject == "other")
  57.                                 subject = part.value
  58.                         }
  59.                     }
  60.                 }
  61.                 is PartData.FileItem -> {
  62.                     println(part.originalFileName)
  63.                     if(!part.originalFileName.isNullOrEmpty()) {
  64.                         val file = File(createTempDir(), part.originalFileName)
  65.                         part.streamProvider().use {input -> file.outputStream().buffered().use {output -> input.copyToSuspend(output)}}
  66.                         files.add(file)
  67.                     }
  68.                 }
  69.             }
  70.             part.dispose
  71.         }
  72.         val data = mapOf(
  73.                 "secret" to "6LdkTooUAAAAABbt-oRhRBQjyam1zHbZpmKuO87A",
  74.                 "response" to captcha
  75.         )
  76.         val response = sendParams("https://www.google.com/recaptcha/api/siteverify", data)
  77.         if(email != "" && name != "" && subject != "" && message != "" && response.contains(Regex("\"success\": true"))) {
  78.             result = Mailer.sendMail(email, name, subject, message, files, false)
  79.         }
  80.         call.respond(FreeMarkerContent("contact.ftl", mapOf("nonce" to nonce)))
  81.     }
  82.     webSocket("/ws") {
  83.         send(result)
  84.     }
  85.     get<Contact> {
  86.         call.respond(FreeMarkerContent("contact.ftl", mapOf("nonce" to nonce)))
  87.     }
  88. }
  89.  
  90. suspend fun InputStream.copyToSuspend(
  91.         out: OutputStream,
  92.         bufferSize: Int = DEFAULT_BUFFER_SIZE,
  93.         yieldSize: Int = 4 * 1024 * 1024,
  94.         dispatcher: CoroutineDispatcher = Dispatchers.IO
  95. ) = withContext(dispatcher) {
  96.     val buffer = ByteArray(bufferSize)
  97.     var bytesCopied = 0L
  98.     var bytesAfterYield = 0L
  99.     while(true) {
  100.         val bytes = read(buffer).takeIf {it >= 0} ?: break
  101.         out.write(buffer, 0, bytes)
  102.         if(bytesAfterYield >= yieldSize) {
  103.             yield()
  104.             bytesAfterYield %= yieldSize
  105.         }
  106.         bytesCopied += bytes
  107.         bytesAfterYield += bytes
  108.     }
  109.     return@withContext bytesCopied
  110. }
  111.  
Add Comment
Please, Sign In to add comment