[Core] [File] Support reader

This commit is contained in:
qianmoQ 2024-06-10 09:01:54 +08:00
parent bd560d2f46
commit fffd8a5db3
2 changed files with 57 additions and 1 deletions

View File

@ -1,5 +1,6 @@
package io.edurt.datacap.file.none
import com.google.common.base.Preconditions.checkState
import io.edurt.datacap.common.utils.DateUtils
import io.edurt.datacap.file.File
import io.edurt.datacap.file.FileConvert
@ -73,6 +74,40 @@ class NoneFile : File
override fun reader(request: FileRequest): FileResponse
{
TODO("Not yet implemented")
val response = FileResponse()
try
{
log.info("${name()} reader origin path [ ${request.path} ]")
log.info("${name()} reader start time [ ${DateUtils.now()} ]")
val file = FileConvert.formatFile(request, name())
log.info("${name()} reader file absolute path [ ${file.absolutePath} ]")
val lines = FileUtils.readLines(file, Charsets.UTF_8)
checkState(lines.isNotEmpty(), "The file is empty")
log.info("${name()} reader file line count [ ${lines.size} ]")
response.headers = parseLine(lines.first())
val columns = lines.drop(1)
.map { parseLine(it) }
response.columns = columns
log.info("${name()} reader end time [ ${DateUtils.now()} ]")
response.successful = true
}
catch (e: Exception)
{
response.successful = false
response.message = e.message
}
return response
}
private fun parseLine(line: String): List<String>
{
return line.trim()
.removeSurrounding("[", "]")
.split(", ")
.map { it.trim() }
}
}

View File

@ -66,4 +66,25 @@ class NoneFileTest
}
}
}
@Test
fun testReader()
{
injector?.let { injector ->
FileFilter.findNotify(injector, name)
.ifPresent { file ->
val response = file.reader(request)
log.info("headers: [ ${response.headers} ]")
response.columns
.let { columns ->
columns.forEachIndexed { index, line ->
log.info("index: [ $index ], line: [ $line ]")
}
}
assertTrue {
response.successful == true
}
}
}
}
}