From ddf85585782c1990c52a755663de96d45473ca8d Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Thu, 10 Sep 2020 19:03:24 +0200 Subject: [PATCH] Return zip --- src/main/kotlin/starter/ProjectZip.kt | 9 +++++++-- src/main/kotlin/starter/Server.kt | 10 +++++++--- src/main/kotlin/starter/utils/PathUtils.kt | 3 +++ src/main/kotlin/starter/utils/ZipOutput.kt | 2 ++ 4 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/starter/utils/PathUtils.kt diff --git a/src/main/kotlin/starter/ProjectZip.kt b/src/main/kotlin/starter/ProjectZip.kt index f892584..4e23396 100644 --- a/src/main/kotlin/starter/ProjectZip.kt +++ b/src/main/kotlin/starter/ProjectZip.kt @@ -2,17 +2,22 @@ package starter import starter.utils.ZipOutput import starter.utils.prettyPrintXml +import starter.utils.sanitizeFilename class ProjectZip(private val templates: Templates) { - fun createZip(project: Project, inputs: List, dependencies: List) { + fun createZip(project: Project, inputs: List, dependencies: List): String { val pom = templates.pom(dependencies, inputs) val prettyPom = prettyPrintXml(pom) - ZipOutput(project.name).use { + val name: String + + ZipOutput(sanitizeFilename(project.name)).use { + name = it.name it.write("pom.xml", prettyPom) } + return name } } \ No newline at end of file diff --git a/src/main/kotlin/starter/Server.kt b/src/main/kotlin/starter/Server.kt index b5832b5..6221fa1 100644 --- a/src/main/kotlin/starter/Server.kt +++ b/src/main/kotlin/starter/Server.kt @@ -1,6 +1,8 @@ package starter import io.javalin.Javalin +import starter.utils.sanitizeFilename +import java.io.File class Server( private val views: Views, @@ -33,10 +35,12 @@ class Server( val basePackage = inputs.find { it.name == "basePackage" }!!.value!! val project = Project(projectName, basePackage) - projectZip.createZip(project, inputs, deps) - TODO() - ctx.result("prettyPrintXml(generatedPom)") + ctx.contentType("text/xml") + ctx.header("Content-Disposition", "attachment; filename=\"${sanitizeFilename(projectName)}.zip\"") + val zipFile = projectZip.createZip(project, inputs, deps) + ctx.result(File(zipFile).readBytes()) } } + } \ No newline at end of file diff --git a/src/main/kotlin/starter/utils/PathUtils.kt b/src/main/kotlin/starter/utils/PathUtils.kt new file mode 100644 index 0000000..81eb342 --- /dev/null +++ b/src/main/kotlin/starter/utils/PathUtils.kt @@ -0,0 +1,3 @@ +package starter.utils + +fun sanitizeFilename(inputName: String): String = inputName.replace("[^a-zA-Z0-9-_.]".toRegex(), "_") diff --git a/src/main/kotlin/starter/utils/ZipOutput.kt b/src/main/kotlin/starter/utils/ZipOutput.kt index 23b3646..282bbd9 100644 --- a/src/main/kotlin/starter/utils/ZipOutput.kt +++ b/src/main/kotlin/starter/utils/ZipOutput.kt @@ -12,6 +12,8 @@ class ZipOutput(name: String) : AutoCloseable { createNewFile() } private val outputStream = ZipArchiveOutputStream(zipFile.outputStream()) + val name: String + get() = zipFile.name fun write(path: String, content: String) { val entry = ZipArchiveEntry(path)