Created
June 28, 2019 06:35
-
-
Save Webmobsoft/786b002458cd84b80c910ced423d0fa5 to your computer and use it in GitHub Desktop.
Upload Attachement on Corda Ledger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PostMapping(value = ["uploadFile"], produces =[APPLICATION_JSON_VALUE]) | |
fun uploadFile(@RequestParam file: MultipartFile, @RequestParam uploader: String):ResponseEntity<String>{ | |
val filename = file.originalFilename | |
val uploader = uploader | |
require(filename != null) {"File Name must be set"} | |
val hash: SecureHash = if(!(file.contentType == "zip" || file.contentType == "jar")){ | |
uploadZip(file.inputStream, uploader,filename!!) | |
}else{ | |
proxy.uploadAttachmentWithMetadata( | |
jar = file.inputStream, | |
uploader = uploader, | |
filename = filename!! | |
) | |
} | |
return ResponseEntity.created(URI.create("attachments/$hash")).body("Attachment uploaded with hash - $hash") | |
} | |
private fun uploadZip(inputStream: InputStream, uploader: String,filename:String): AttachmentId{ | |
val zipName = "$filename-${UUID.randomUUID()}.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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment