Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit b043145

Browse files
committed
Add Utils for handling MultiPart
1 parent d585869 commit b043145

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ compileTestKotlin {
2828
}
2929

3030
group 'de.debuglevel.sparkmicroserviceutils'
31-
version '0.0.21'
31+
version '0.0.22'
3232

3333
sourceCompatibility = 1.8
3434

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package de.debuglevel.microservices.utils.multipart
2+
3+
import mu.KotlinLogging
4+
import spark.Request
5+
import java.nio.file.Files
6+
import java.nio.file.Path
7+
import java.nio.file.StandardCopyOption
8+
import javax.servlet.MultipartConfigElement
9+
import javax.servlet.http.Part
10+
11+
object MultipartUtils {
12+
private val logger = KotlinLogging.logger {}
13+
14+
/**
15+
* Get the original file name of a multipart part
16+
*/
17+
private fun getOriginalFilename(part: Part): String? {
18+
for (cd in part.getHeader("content-disposition").split(";")) {
19+
if (cd.trim { it <= ' ' }.startsWith("filename")) {
20+
return cd.substring(
21+
cd
22+
.indexOf('=') + 1)
23+
.trim { it <= ' ' }
24+
.replace("\"", "")
25+
}
26+
}
27+
28+
return null
29+
}
30+
31+
/**
32+
* Get text from a multipart request.
33+
*
34+
* @param fieldName name of the field in the HTML form
35+
*/
36+
fun getField(request: Request, fieldName: String): String {
37+
setup(request)
38+
39+
return request.raw()
40+
.getPart(fieldName)
41+
.inputStream
42+
.reader()
43+
.use { it.readText() }
44+
}
45+
46+
fun getCheckbox(request: Request, fieldName: String): Boolean {
47+
setup(request)
48+
49+
return request.raw()
50+
.parts
51+
.filter { it.name == fieldName }
52+
.filter { it.inputStream.reader().use { it -> it.readText() } == "on" }
53+
.any()
54+
}
55+
56+
/**
57+
* Get a file from a multipart request.
58+
*
59+
* Copies the content form a multipart request field to a file and returns the path.
60+
* Note: The file should be deleted after usage.
61+
*
62+
* @param fieldName name of the field in the HTML form
63+
*/
64+
fun getFile(request: Request, fieldName: String): Path {
65+
setup(request)
66+
67+
val temporarySurveyFile = createTempFile("sparkmicroserviceutils").toPath()
68+
request.raw()
69+
.getPart(fieldName) // getPart needs to use same "name" as input field in form
70+
.inputStream
71+
.use {
72+
Files.copy(it, temporarySurveyFile, StandardCopyOption.REPLACE_EXISTING)
73+
}
74+
logger.debug("Uploaded file '${getOriginalFilename(request.raw().getPart(fieldName))}' saved as '${temporarySurveyFile.toAbsolutePath()}'")
75+
return temporarySurveyFile
76+
}
77+
78+
private fun setup(request: Request) {
79+
if (!request.attributes().contains("org.eclipse.jetty.multipartConfig")) {
80+
request.attribute("org.eclipse.jetty.multipartConfig", MultipartConfigElement("/temp"))
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)