[File] [Csv] Add formatStream

This commit is contained in:
qianmoQ 2024-06-14 21:46:14 +08:00
parent 1564ff91d2
commit 511c3fead0
2 changed files with 54 additions and 2 deletions

View File

@ -8,7 +8,10 @@ import io.edurt.datacap.file.model.FileResponse
import org.apache.commons.csv.CSVFormat
import org.apache.commons.io.FileUtils
import org.slf4j.LoggerFactory.getLogger
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.util.Objects.requireNonNull
class CsvFile : File
{
@ -40,7 +43,35 @@ class CsvFile : File
override fun formatStream(request: FileRequest): FileResponse
{
TODO("Not yet implemented")
val response = FileResponse()
val delimiter = ","
try
{
requireNonNull("Stream must not be null")
log.info("${name()} format stream start time [ ${DateUtils.now()} ]")
request.stream
?.let {
BufferedReader(InputStreamReader(it, Charsets.UTF_8)).use { reader ->
response.headers = reader.readLine()
.split(delimiter)
val columns = mutableListOf<Any>()
reader.readLines()
.forEach { line -> columns.add(line.split(delimiter)) }
response.columns = columns
it.close()
}
}
log.info("${name()} format stream end time [ ${DateUtils.now()} ]")
response.successful = true
}
catch (e: IOException)
{
response.successful = false
response.message = e.message
}
return response
}
override fun writer(request: FileRequest): FileResponse
@ -48,7 +79,6 @@ class CsvFile : File
val response = FileResponse()
try
{
val csvFormat = CSVFormat.newFormat(',')
log.info("${name()} writer start time [ ${DateUtils.now()} ]")
response.headers = request.headers
val file = FileConvert.formatFile(request, name())

View File

@ -9,6 +9,8 @@ import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.slf4j.LoggerFactory.getLogger
import java.io.File
import java.io.FileInputStream
class CsvFileTest
{
@ -64,4 +66,24 @@ class CsvFileTest
}
}
}
@Test
fun testFormatStream()
{
injector?.let { injector ->
FileFilter.filter(injector, name)
.ifPresent { file ->
request.stream = FileInputStream(File("${System.getProperty("user.dir")}/${request.name}.csv"))
val response = file.formatStream(request)
log.info("headers: [ ${response.headers} ]")
response.columns
.let { columns ->
columns.forEachIndexed { index, line ->
log.info("index: [ $index ], line: [ $line ]")
}
}
assertTrue(response.successful == true)
}
}
}
}