All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.baulsupp.oksocial.output.DownloadHandler.kt Maven / Gradle / Ivy

There is a newer version: 4.19.0
Show newest version
package com.baulsupp.oksocial.output

import okio.Okio
import okio.Sink
import java.io.File
import java.io.IOException

class DownloadHandler(
  private val responseExtractor: ResponseExtractor,
  private val outputFile: File
) : OutputHandler {

  override suspend fun showOutput(response: R) {
    val source = responseExtractor.source(response)

    val outputSink = getOutputSink(response)
    try {
      source.writeToSink(outputSink)
    } finally {
      if (!isStdout) {
        outputSink.close()
      }
    }
  }

  fun getOutputSink(response: R): Sink {
    return when {
      isStdout -> systemOut
      outputFile.isDirectory -> {
        val responseOutputFile = File(outputFile, responseExtractor.filename(response))
        System.err.println("Saving $responseOutputFile")
        Okio.sink(responseOutputFile)
      }
      else -> {
        if (outputFile.parentFile != null && !outputFile.parentFile.exists()) {
          if (!outputFile.parentFile.mkdirs()) {
            throw IOException("unable to create directory $outputFile")
          }
        }
        Okio.sink(outputFile)
      }
    }
  }

  val isStdout by lazy {
    outputFile.path == "-"
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy