Code: Select all
zipFile.zip/MainFolder/MyFile.xml
Code: Select all
java.io.IOException: Attempted read on closed stream
Code: Select all
private fun downloadDataToDisk(
source: FileData,
name: String,
fileType: FileType
): Either {
val storingPath = prepareDirectory()
val fileCompleteEndpoint =
storingPath.flatMap { _ ->
val fileCompletedName =
when (fileType) {
FileType.DAY -> {
source.dataUrl +
"${generateDateForDownload(source.daysAgo.toLong())}${fileType.fileType}.zip"
}
FileType.FULL -> {
source.dataUrl +
"${generateDateForDownload(source.daysAgo.toLong())}${fileType.fileType}.zip"
}
}
fileCompletedName.right()
}
val downloadedFile =
fileCompleteEndpoint.flatMap { path ->
externalClient.downloadFile(path).mapLeft { mapClientError(it) }
}
val storedFileStatus =
downloadedFile.flatMap { inputStream ->
val storedFilePath = storingPath.flatMap { path -> storeFile(path, name, inputStream) }
storedFilePath
}
return storedFileStatus.flatMap { storedFilePath ->
storedFilePath.right()
}
}
Code: Select all
private fun prepareDirectory(): Either {
return Either.conditionally(
File(properties.fileStoragePath).exists(),
{ directoryMissingError() },
{ properties.fileStoragePath }
)
}
Code: Select all
private fun storeFile(
path: String,
fileName: String,
input: InputStream
): Either {
val filePath =
Either.catch {
val file = File(path, fileName)
if (!file.exists()) {
file.createNewFile()
input.use { stream -> file.outputStream().use { target -> stream.copyTo(target) } }
}
file.path
}
.mapLeft { fileStorageError(fileName, it) }
.tap { log.atInfo().log("Successfully extracted and stored dow jones file $fileName") }
return filePath
}