Clean + lint
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package starter.routes
|
||||
|
||||
import org.http4k.core.Method
|
||||
import org.http4k.core.Response
|
||||
import org.http4k.core.body.form
|
||||
import org.http4k.core.body.formAsMap
|
||||
import org.http4k.core.with
|
||||
import org.http4k.routing.bind
|
||||
import starter.Project
|
||||
import starter.ProjectZip
|
||||
import starter.StarterConfig
|
||||
import starter.extensions.attachment
|
||||
import starter.extensions.badRequest
|
||||
import starter.extensions.ok
|
||||
import java.io.ByteArrayInputStream
|
||||
|
||||
class ZipRouteSupplier(
|
||||
private val conf: StarterConfig,
|
||||
private val projectZip: ProjectZip,
|
||||
) : RouteSupplier {
|
||||
|
||||
override fun get() = "/" bind Method.POST to { req ->
|
||||
|
||||
val deps = conf.dependencies.filter {
|
||||
req.form(it.name) != null
|
||||
}
|
||||
|
||||
val inputKeys = conf.inputs.map { it.name }
|
||||
val inputs = req.formAsMap()
|
||||
.filter { it.key in inputKeys }
|
||||
.map { (name, value) ->
|
||||
conf.inputs.find { it.name == name }!!.copy(value = value.first())
|
||||
}
|
||||
|
||||
val projectName = inputs.find { it.name == "name" }!!.value!!
|
||||
val basePackage = inputs.find { it.name == "basePackage" }!!.value!!
|
||||
|
||||
if (basePackage.contains("/") || basePackage.contains("..")) {
|
||||
Response.badRequest()
|
||||
} else {
|
||||
val repositories = conf.repositories
|
||||
.filter { repo -> repo.name in deps.mapNotNull { it.repository } }
|
||||
|
||||
val project = Project(projectName, basePackage, inputs, deps, repositories)
|
||||
val outputStream = projectZip.createZip(project)
|
||||
|
||||
Response.ok().with(
|
||||
attachment(
|
||||
value = ByteArrayInputStream(outputStream.toByteArray()),
|
||||
name = "$projectName.zip",
|
||||
contentType = "application/zip"
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user