Skip to content

Instantly share code, notes, and snippets.

@Webmobsoft
Created June 28, 2019 07:27
Show Gist options
  • Save Webmobsoft/5572fc4c719349d741b3803d5b83218a to your computer and use it in GitHub Desktop.
Save Webmobsoft/5572fc4c719349d741b3803d5b83218a to your computer and use it in GitHub Desktop.
@PostMapping(value = ["uploadFile"], produces =[APPLICATION_JSON_VALUE])
fun uploadFile(@RequestParam file: MultipartFile, @RequestParam uploadBy: String):ResponseEntity<String>{
val filename = file.originalFilename
val uploadBy = uploadBy
require(filename != null) {"File Must be Selected"}
val hash: SecureHash = if(!(file.contentType == "zip")){
upload(file.inputStream, uploadBy,filename!!)
}else{
proxy.uploadAttachmentWithMetadata(
jar = file.inputStream,
uploadBy = uploadBy,
filename = filename!!
)
}
return ResponseEntity.created(URI.create("attachments/$hash")).body("Attachment uploaded - $hash")
}
private fun upload(inputStream: InputStream, uploadeBy: String,filename:String): AttachmentId{
val zip = "$filename-${UUID.randomUUID()}.zip"
FileOutputStream(zip).use { fileOutputStream ->
ZipOutputStream(fileOutputStream).use { zipOutputStream ->
val zipEntry = ZipEntry(filename)
zipOutputStream.putNextEntry(zipEntry)
inputStream.copyTo(zipOutputStream, 1024)
}
}
return FileInputStream(zip).use { fileInputStream ->
val hash = proxy.uploadAttachmentWithMetadata(
jar = fileInputStream,
uploadeBy = uploadeBy,
filename = filename
)
Files.deleteIfExists(Paths.get(zip))
hash
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment