Browse Source

Move business-independent code to Util class

master
Gwendal 7 years ago
parent
commit
7e52a338ff
2 changed files with 69 additions and 64 deletions
  1. +0
    -64
      src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt
  2. +69
    -0
      src/main/kotlin/bandcampcollectiondownloader/Util.kt

+ 0
- 64
src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt View File

@ -8,9 +8,6 @@ import org.jsoup.HttpStatusException
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.zeroturnaround.zip.ZipUtil import org.zeroturnaround.zip.ZipUtil
import java.io.File import java.io.File
import java.io.FileOutputStream
import java.net.HttpURLConnection
import java.net.URL
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
import java.nio.file.Paths import java.nio.file.Paths
@ -19,7 +16,6 @@ import java.sql.DriverManager
import java.time.Instant import java.time.Instant
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
import javax.mail.internet.ContentDisposition
data class ParsedCookie( data class ParsedCookie(
@SerializedName("Name raw") @SerializedName("Name raw")
@ -63,66 +59,6 @@ fun parsedCookiesToMap(parsedCookies: Array<ParsedCookie>): Map<String, String>
const val BUFFER_SIZE = 4096 const val BUFFER_SIZE = 4096
/** /**
* From http://www.codejava.net/java-se/networking/use-httpurlconnection-to-download-file-from-an-http-url
*/
fun downloadFile(fileURL: String, saveDir: Path, optionalFileName: String = ""): Path {
val url = URL(fileURL)
val httpConn = url.openConnection() as HttpURLConnection
val responseCode = httpConn.responseCode
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
val disposition = httpConn.getHeaderField("Content-Disposition")
val fileName: String =
when {
optionalFileName != "" -> optionalFileName
disposition != null -> {
val parsedDisposition = ContentDisposition(disposition)
parsedDisposition.getParameter("filename")
}
else -> Paths.get(url.file).fileName.toString()
}
// opens input stream from the HTTP connection
val inputStream = httpConn.inputStream
val saveFilePath = saveDir.resolve(fileName)
val saveFilePathString = saveFilePath.toAbsolutePath().toString()
// opens an output stream to save into file
val outputStream = FileOutputStream(saveFilePathString)
val buffer = ByteArray(BUFFER_SIZE)
var bytesRead = inputStream.read(buffer)
while (bytesRead != -1) {
outputStream.write(buffer, 0, bytesRead)
bytesRead = inputStream.read(buffer)
}
outputStream.close()
inputStream.close()
httpConn.disconnect()
return saveFilePath
} else {
throw Exception("No file to download. Server replied HTTP code: $responseCode")
}
}
fun isUnix(): Boolean {
val os = System.getProperty("os.name").toLowerCase()
return os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 || os.indexOf("aix") > 0
}
fun isWindows(): Boolean {
val os = System.getProperty("os.name").toLowerCase()
return os.indexOf("win") >= 0
}
/**
* Core function called from the main * Core function called from the main
*/ */
fun downloadAll(cookiesFile: Path?, bandcampUser: String, downloadFormat: String, downloadFolder: Path) { fun downloadAll(cookiesFile: Path?, bandcampUser: String, downloadFormat: String, downloadFolder: Path) {


+ 69
- 0
src/main/kotlin/bandcampcollectiondownloader/Util.kt View File

@ -0,0 +1,69 @@
package bandcampcollectiondownloader
import java.io.FileOutputStream
import java.net.HttpURLConnection
import java.net.URL
import java.nio.file.Path
import java.nio.file.Paths
import javax.mail.internet.ContentDisposition
/**
* From http://www.codejava.net/java-se/networking/use-httpurlconnection-to-download-file-from-an-http-url
*/
fun downloadFile(fileURL: String, saveDir: Path, optionalFileName: String = ""): Path {
val url = URL(fileURL)
val httpConn = url.openConnection() as HttpURLConnection
val responseCode = httpConn.responseCode
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
val disposition = httpConn.getHeaderField("Content-Disposition")
val fileName: String =
when {
optionalFileName != "" -> optionalFileName
disposition != null -> {
val parsedDisposition = ContentDisposition(disposition)
parsedDisposition.getParameter("filename")
}
else -> Paths.get(url.file).fileName.toString()
}
// opens input stream from the HTTP connection
val inputStream = httpConn.inputStream
val saveFilePath = saveDir.resolve(fileName)
val saveFilePathString = saveFilePath.toAbsolutePath().toString()
// opens an output stream to save into file
val outputStream = FileOutputStream(saveFilePathString)
val buffer = ByteArray(BUFFER_SIZE)
var bytesRead = inputStream.read(buffer)
while (bytesRead != -1) {
outputStream.write(buffer, 0, bytesRead)
bytesRead = inputStream.read(buffer)
}
outputStream.close()
inputStream.close()
httpConn.disconnect()
return saveFilePath
} else {
throw Exception("No file to download. Server replied HTTP code: $responseCode")
}
}
fun isUnix(): Boolean {
val os = System.getProperty("os.name").toLowerCase()
return os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 || os.indexOf("aix") > 0
}
fun isWindows(): Boolean {
val os = System.getProperty("os.name").toLowerCase()
return os.indexOf("win") >= 0
}

Loading…
Cancel
Save