4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-05-19 20:04:07 +02:00

FileDownloader can now resolve file names.

This commit is contained in:
LordMZTE 2020-08-09 15:40:56 +02:00
parent f03fb37cb4
commit 2992d71e04
2 changed files with 26 additions and 8 deletions

View file

@ -15,7 +15,6 @@ import net.sourceforge.argparse4j.inf.ArgumentParser
import net.sourceforge.argparse4j.inf.Namespace
import java.io.File
import java.net.URL
import java.nio.file.Paths
import java.util.stream.Collectors.toMap
@LoadCommand
@ -50,7 +49,7 @@ object DownloadMods : ICommand {
.filter {it.installer == "internal.dir:mods"}
.collect(toMap<FileOrLink, URL, File>(
{URL(it.link)},
{File(args.get<File>("dir"), Paths.get(URL(it.link).path).fileName.toString())},
{args.get<File>("dir")},
{_: File, f: File -> f}
)),
{r: DownloadFileTask.Return ->
@ -61,7 +60,8 @@ object DownloadMods : ICommand {
println(r.exception.message)
}
},
args.get<Boolean>("force") == null
!args.getBoolean("force"),
true
)
return success()
}

View file

@ -9,16 +9,27 @@ import org.apache.commons.io.FileUtils
import java.io.File
import java.io.IOException
import java.net.URL
import java.nio.file.Paths
import java.util.concurrent.CountDownLatch
import java.util.stream.Collectors
private var latch: CountDownLatch? = null
/**
* Downloads all supplied urls to the given files
*
* @param files the files to download and the file to save them to
* @param callback the callback which will be called once a download finishes
* @param skipExistingFiles if true, files that already exist will not be downloaded
* @param resolveFileName if true, the file name will be resolved using the URL (target file will now serve as directory)
*/
@JvmOverloads
fun downloadFiles(
files: Map<URL, File>,
callback: (DownloadFileTask.Return) -> Unit,
skipExistingFiles: Boolean
skipExistingFiles: Boolean = false,
resolveFileName: Boolean = false
) {
val tasks = files.entries.stream()
//remove if it should be skipped
@ -26,7 +37,7 @@ fun downloadFiles(
.collect(Collectors.toList())
latch = CountDownLatch(tasks.size)
tasks.forEach {
val req = DownloadFileTask(it.key, it.value, callback, latch!!)
val req = DownloadFileTask(it.key, it.value, callback, latch!!, resolveFileName)
HTTP_CLIENT.newCall(req.request).enqueue(req)
}
latch!!.await()
@ -36,7 +47,8 @@ open class DownloadFileTask(
protected open val url: URL,
protected open val file: File,
protected open val callback: (Return) -> Unit,
protected open val latch: CountDownLatch
protected open val latch: CountDownLatch,
protected open val resolveFileName: Boolean
) : Callback {
open val request = Request.Builder()
.get()
@ -56,13 +68,19 @@ open class DownloadFileTask(
override fun onResponse(call: Call, response: Response) {
callback(try {
val outFile =
if(resolveFileName)
file mergeTo Paths.get(response.request.url.toUri().path).fileName.toFile()
else
file
val stream = response.body?.byteStream()
FileUtils.copyInputStreamToFile(stream, file)
FileUtils.copyInputStreamToFile(stream, outFile)
stream!!.close()
Return(
url,
file,
outFile,
response.code,
response.message,
null