diff --git a/util/download/src/main/kotlin/voodoo.util/DownloadExtension.kt b/util/download/src/main/kotlin/voodoo.util/DownloadExtension.kt index ee3cdbb3..5778fb7f 100644 --- a/util/download/src/main/kotlin/voodoo.util/DownloadExtension.kt +++ b/util/download/src/main/kotlin/voodoo.util/DownloadExtension.kt @@ -24,10 +24,13 @@ suspend fun File.download( useragent: String = voodoo.util.useragent ) = withContext(Dispatchers.IO) { val targetFile = this@download + val cacheFile = cacheDir?.resolve(targetFile.name) for (retry in (0..retries)) { val retryDelay = (retry * 1000L) - if (cacheDir != null) { - val cacheFile = cacheDir.resolve(targetFile.name) + if (cacheDir != null && cacheFile != null) { + require(targetFile.absolutePath != cacheFile.absolutePath) { + "cache file cannot be the same as target file" + } logger.info("downloading $url -> ${targetFile}") logger.debug("cacheFile $cacheFile") if (cacheFile.exists() && !cacheFile.isFile) cacheFile.deleteRecursively() @@ -66,6 +69,7 @@ suspend fun File.download( } targetFile.absoluteFile.parentFile.mkdirs() + // Response content is streamed val channel = response.receive() val contentLength = channel.copyAndClose(targetFile.writeChannel()) @@ -96,7 +100,6 @@ suspend fun File.download( continue } - val cacheFile = cacheDir?.resolve(targetFile.name) logger.debug("running validator on $targetFile") if (!validator(targetFile)) { @@ -126,93 +129,6 @@ suspend fun File.download( error("failed to download $url after $retries attempts") } - -/* -suspend fun File.download( - url: String, - cacheDir: File, - canSkipDownload: (file: File) -> Boolean = { true }, - logger: KLogger = Downloader.logger, - retries: Int = 3 -) { - val cacheFile = cacheDir.resolve(this.name) - logger.info("downloading $url -> ${this@download}") - logger.debug("cacheFile $cacheFile") - if (cacheFile.exists() && !cacheFile.isFile) cacheFile.deleteRecursively() - - logger.debug("validating $cacheFile existence and hash") - if (cacheFile.exists() && cacheFile.isFile && canSkipDownload(cacheFile)) { - logger.info("file: $cacheFile exists and can skip download") - } else { - val (request, response, result) = try { - manager.download(url) - .fileDestination { response, request -> - cacheDir.mkdirs() - cacheFile.parentFile.mkdirs() - cacheFile - } - .header("User-Agent" to Downloader.useragent) - .awaitByteArrayResponseResult() - } catch (e: ClassCastException) { - e.printStackTrace() - logger.error(e) { "failed for url: $url" } - val ex = IllegalStateException("failed for url: $url") - ex.addSuppressed(e) - throw ex - exitProcess(-2) - } - - when (result) { - is Result.Success -> { -// cacheFile.writeBytes(result.value) - } - is Result.Failure -> { - logger.error("invalid statusCode {} from {}", response.statusCode, url.encoded) - logger.error("connection url: ${request.url}") - logger.error("cUrl: ${request.cUrlString()}") - logger.error("response: $response") -// logger.error("content: {}", result.component1()) - logger.error("error: {}", result.error.toString()) - if (retries > 0) { - logger.error("attempting to download again in 500 ms") - delay(500) - download( - url = url, - cacheDir = cacheDir, - canSkipDownload = canSkipDownload, - logger = logger, - retries = retries - 1 - ) - } - - logger.error(result.error.exception) { "Download Failed" } - exitProcess(-1) - } - } - } - - logger.debug("saving $url -> $this") - try { - this.parentFile.mkdirs() - cacheFile.copyTo(this, overwrite = true) - } catch (e: FileAlreadyExistsException) { - val fileIsLocked = !this.renameTo(this) - logger.error("failed to copy file $cacheFile to $this .. file is locked ? $fileIsLocked") - if (!fileIsLocked) - cacheFile.copyTo(this, overwrite = true) - } - - if (!canSkipDownload(cacheFile)) { - cacheFile.delete() - } - if (!canSkipDownload(this)) { - logger.error("$this did not pass validation") - } - - logger.debug("done downloading $url -> $this") -} -*/ - val String.encoded: String get() = this .replace(" ", "%20") diff --git a/util/maven/src/main/kotlin/voodoo.util/maven/MavenUtil.kt b/util/maven/src/main/kotlin/voodoo.util/maven/MavenUtil.kt index 4aad119f..8bd2da1f 100644 --- a/util/maven/src/main/kotlin/voodoo.util/maven/MavenUtil.kt +++ b/util/maven/src/main/kotlin/voodoo.util/maven/MavenUtil.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.runBlocking import mu.KotlinLogging import org.w3c.dom.NodeList import org.xml.sax.InputSource +import voodoo.util.Directories import voodoo.util.download import voodoo.util.toHexString import voodoo.util.useClient @@ -30,6 +31,8 @@ import javax.xml.xpath.XPathFactory object MavenUtil { private val logger = KotlinLogging.logger {} + private val directories = Directories.get(moduleName = "MAVEN") + private val cacheHome = directories.cacheHome val mavenLocalFolder = File(System.getProperty("user.home")).resolve(".m2").resolve("repository") fun localMavenFile( group: String, @@ -93,7 +96,7 @@ object MavenUtil { targetFile.absoluteFile.parentFile.mkdirs() targetFile.download( url = artifactUrl, - cacheDir = outputDir, + cacheDir = cacheHome.resolve(group).resolve(artifactId).resolve(version), validator = { file -> if(verifyChecksum) { // TODO: use whatever checksum thing it finds