Commit c6284040 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Refactor remote file use.

Kotlin way of handling closable resources.
parent 02f75b73
......@@ -34,26 +34,36 @@ class FileValidation(val sftp: SftpClient) {
fun validate(file: File): Report {
when (val format = validateExtension(file)) {
"CSV", "TSV" -> {
val remoteFile = sftp.open(file)
val stream = remoteFile.RemoteFileInputStream()
try {
csvReader {
charset = "UTF-8"
delimiter = if (format == "CSV") ',' else '\t'
quoteChar = '"'
escapeChar = '\\'
}.readAll(stream)
} catch (ex: MalformedCSVException) {
remoteFile.close()
return Report(id = file.name, status = "invalid", path = file.path, format = format, error = ex.localizedMessage)
sftp.open(file).use {
val stream = it.RemoteFileInputStream()
try {
csvReader {
charset = "UTF-8"
delimiter = if (format == "CSV") ',' else '\t'
quoteChar = '"'
escapeChar = '\\'
}.readAll(stream)
} catch (ex: MalformedCSVException) {
return Report(
id = file.name,
status = "invalid",
path = file.path,
format = format,
error = ex.localizedMessage
)
}
return Report(id = file.name, status = "valid", path = file.path, format = format, error = "")
}
remoteFile.close()
return Report(id = file.name, status = "valid", path = file.path, format = format, error = "")
}
else -> return Report(id = file.name, status = "invalid", path = file.path, format = format, error = "Not a valid file extension: ${file.name}.")
else -> return Report(
id = file.name,
status = "invalid",
path = file.path,
format = format,
error = "File Extension Error: Not a valid file extension: ${file.name}."
)
}
}
private fun validateExtension(file: File): String {
return supportedExtensions.getOrDefault(file.extension, "INVALID")
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment