I know that every file which is uploaded as an attachment on Corda gets an unique hash generated and these files are to be converted to either zip/jar format because these are the types Corda supports for saving them as attachments. I have come across a scenario where I have uploaded a same file twice on the same node with a time gap and this resulted it two attachment hashes which is understandable because Im compressing this file as a zip first and then uploading. But when I try to upload the same file twice at the same instant then there is this DuplicateAttachmentException. I tried debugging and what I have found out is that, while we are zipping the files they are having the same time because of which it looks to Corda that it these zips are the same. If I delay the zipping process for multiple files by 1 second or so, this exception doesn't occur. Is there any solution for this?
private fun uploader(inputStream: InputStream, fileName: String, uploader: String): AttachmentId {
val zipName = "$fileName.zip"
FileOutputStream(zipName).use { fileOutputStream ->
ZipOutputStream(fileOutputStream).use { zipOutputStream ->
val zipEntry = ZipEntry(fileName)
zipOutputStream.putNextEntry(zipEntry)
inputStream.copyTo(zipOutputStream, 1024)
}
}
return FileInputStream(zipName).use { fileInputStream ->
val hash = proxy.uploadAttachmentWithMetadata(
jar = fileInputStream,
uploader = uploader,
filename = fileName
)
Files.deleteIfExists(Paths.get(zipName))
hash
}
}
This is the code using which I was able to produce the issue.