Skip to content

Commit

Permalink
Merge pull request #240 from code-freak/fix/file-upload-path
Browse files Browse the repository at this point in the history
Fix invalid names for file uploads
  • Loading branch information
erikhofer committed Oct 11, 2019
2 parents 76e05a8 + 77516c8 commit 02dd3e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/kotlin/de/code_freak/codefreak/util/TarUtil.kt
Expand Up @@ -174,11 +174,17 @@ object TarUtil {

private fun wrapUploadInTar(file: MultipartFile, out: OutputStream) {
val outputStream = TarArchiveOutputStream(out)
val entry = TarArchiveEntry(file.originalFilename)
val entry = TarArchiveEntry(basename(file.originalFilename ?: "file"))
entry.size = file.size
outputStream.putArchiveEntry(entry)
file.inputStream.use { StreamUtils.copy(it, outputStream) }
outputStream.closeArchiveEntry()
outputStream.finish()
}

private fun basename(path: String): String {
path.split("[\\\\/]".toRegex()).apply {
return if (isEmpty()) "" else last()
}
}
}
13 changes: 13 additions & 0 deletions src/test/kotlin/de/code_freak/codefreak/util/TarUtilTest.kt
Expand Up @@ -4,8 +4,10 @@ import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.containsInAnyOrder
import org.hamcrest.Matchers.notNullValue
import org.junit.Test
import org.springframework.core.io.ClassPathResource
import org.springframework.mock.web.MockMultipartFile
import java.io.ByteArrayOutputStream

internal class TarUtilTest {
Expand All @@ -30,4 +32,15 @@ internal class TarUtilTest {
assertThat(result.mode, `is`(33252))
}
}

@Test
fun `file uploads are wrapped in tar`() {
val file = MockMultipartFile("file", "C:\\Users\\jdoe\\main.c", "text/plain", "".toByteArray())
val out = ByteArrayOutputStream()
TarUtil.writeUploadAsTar(file, out)
TarArchiveInputStream(out.toByteArray().inputStream()).use {
val result = generateSequence { it.nextTarEntry }.filter { it.name == "main.c" }.firstOrNull()
assertThat(result, notNullValue())
}
}
}

0 comments on commit 02dd3e4

Please sign in to comment.