Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.90 KB | None | 0 0
  1. //CONTROLLER
  2. @RequestMapping(method = [(RequestMethod.GET)])
  3.     fun download(@PathVariable("bucketname") bucketName: String,
  4.                  @PathVariable("objectname") objectName: String,
  5.                  @RequestHeader("Range") range: String,
  6.                  request: HttpServletRequest,
  7.                  response: HttpServletResponse): ResponseEntity<Any>{
  8.         return try{
  9.  
  10.             var getAllParts = false
  11.             if(range == ""){
  12.                 getAllParts = true
  13.             }
  14.             val pattern = Pattern.compile("(-?[0-9]*)-(-?[0-9]*)")
  15.             val matcher = pattern.matcher(range)
  16.             matcher.find()
  17.  
  18.             val from = matcher.group(1).toInt()
  19.  
  20.             var to = 0
  21.             to = if(matcher.group(2) == ""){
  22.                 -1
  23.             }else{
  24.                 matcher.group(2).toInt()
  25.             }
  26.  
  27.             objectStorageService.download(bucketName,objectName,from,to,response,getAllParts)
  28.             ResponseEntity.ok().build()
  29.         }
  30.         catch (e: Exception){
  31.             ResponseEntity.badRequest().build()
  32.         }
  33.  
  34. // SERVICE
  35. @Transactional
  36.     fun download(bucketName: String,objectName: String,from: Int,to: Int,response: HttpServletResponse,getAllParts: Boolean){
  37.  
  38.         val bucket = bucketRepository.findByName(bucketName) ?: throw Exception("Bucket Doesn't Exist")
  39.         val obj = objectStorageRepository.findOneByBucketAndName(bucket,objectName) ?: throw Exception("Object Doesn't Exist")
  40.  
  41.  
  42.         if(!obj.complete){
  43.             throw Exception("Object is not completed.")
  44.         }
  45.  
  46.         val allParts = partObjectRepository.findAllByObjectStorageOrderByPartNumberAsc(obj)
  47.         val target = response.outputStream
  48.         val baOutputStream = ByteArrayOutputStream()
  49.         var totalSizeRead = 0
  50.         for (p in allParts){
  51.             val fileInputStream = File(p.location).inputStream()
  52.             totalSizeRead += baOutputStream.write(fileInputStream)
  53.             fileInputStream.close()
  54.         }
  55.  
  56. //        println(obj.totalSize)
  57. //        println(totalSizeRead)
  58.  
  59.         val inputStream = baOutputStream.toInputStream()
  60.  
  61.         if(getAllParts){
  62. //            Write all parts to response
  63.             target.write(inputStream.readBytes())
  64.         }else{
  65.  
  66.             var newTo = to
  67.             if (newTo < 0){
  68.                 newTo = totalSizeRead
  69.             }
  70.  
  71.             val sizeNeeded = newTo - from
  72.             inputStream.skip(from.toLong())
  73.  
  74. //            var ba = ByteArray(sizeNeeded)
  75. //            inputStream.read(ba)
  76. //            target.write(ba)
  77.  
  78. //            val bounded = BoundedInputStream(inputStream,from.toLong()+sizeNeeded.toLong())
  79.  
  80.             for(i in 1..sizeNeeded){
  81.                 target.write(inputStream.read())
  82.             }
  83. //            target.write(bounded.readBytes())
  84.         }
  85.  
  86.  
  87.         response.setHeader("eTag", obj.objectETag)
  88.  
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement