|
|
@ -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<ParsedCookie>): Map<String, String> |
|
|
|
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) { |
|
|
|