diff --git a/src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt b/src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt index 1613d96..05f0860 100644 --- a/src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt +++ b/src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt @@ -8,9 +8,6 @@ import org.jsoup.HttpStatusException import org.jsoup.Jsoup import org.zeroturnaround.zip.ZipUtil 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.Path import java.nio.file.Paths @@ -19,7 +16,6 @@ import java.sql.DriverManager import java.time.Instant import java.util.* import java.util.regex.Pattern -import javax.mail.internet.ContentDisposition data class ParsedCookie( @SerializedName("Name raw") @@ -63,66 +59,6 @@ fun parsedCookiesToMap(parsedCookies: Array): Map 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 */ fun downloadAll(cookiesFile: Path?, bandcampUser: String, downloadFormat: String, downloadFolder: Path) { diff --git a/src/main/kotlin/bandcampcollectiondownloader/Util.kt b/src/main/kotlin/bandcampcollectiondownloader/Util.kt new file mode 100644 index 0000000..193ee45 --- /dev/null +++ b/src/main/kotlin/bandcampcollectiondownloader/Util.kt @@ -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 +} +