Skip to content

Commit

Permalink
fix some download issues with copying to the same file
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkyAI committed Mar 21, 2021
1 parent 0bb6170 commit f185fc6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 91 deletions.
96 changes: 6 additions & 90 deletions util/download/src/main/kotlin/voodoo.util/DownloadExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -66,6 +69,7 @@ suspend fun File.download(
}

targetFile.absoluteFile.parentFile.mkdirs()

// Response content is streamed
val channel = response.receive<ByteReadChannel>()
val contentLength = channel.copyAndClose(targetFile.writeChannel())
Expand Down Expand Up @@ -96,7 +100,6 @@ suspend fun File.download(
continue
}

val cacheFile = cacheDir?.resolve(targetFile.name)

logger.debug("running validator on $targetFile")
if (!validator(targetFile)) {
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 4 additions & 1 deletion util/maven/src/main/kotlin/voodoo.util/maven/MavenUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f185fc6

Please sign in to comment.