1
0

Return zip

This commit is contained in:
Hubert Van De Walle 2020-09-10 19:03:24 +02:00
parent 883610878a
commit ddf8558578
4 changed files with 19 additions and 5 deletions

View File

@ -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<Input>, dependencies: List<Dependency>) {
fun createZip(project: Project, inputs: List<Input>, dependencies: List<Dependency>): 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
}
}

View File

@ -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())
}
}
}

View File

@ -0,0 +1,3 @@
package starter.utils
fun sanitizeFilename(inputName: String): String = inputName.replace("[^a-zA-Z0-9-_.]".toRegex(), "_")

View File

@ -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)