Small refactoring

This commit is contained in:
Alexandre Archambault 2016-04-05 16:24:39 +02:00
parent f73dac68fd
commit 96275df945
1 changed files with 28 additions and 48 deletions

View File

@ -43,8 +43,8 @@ object Cache {
} }
} }
private def withLocal(artifact: Artifact, cache: File): Artifact = { private def localFile(url: String, cache: File): File = {
def local(url: String) = val path =
if (url.startsWith("file:///")) if (url.startsWith("file:///"))
url.stripPrefix("file://") url.stripPrefix("file://")
else if (url.startsWith("file:/")) else if (url.startsWith("file:/"))
@ -68,19 +68,7 @@ object Cache {
throw new Exception(s"No protocol found in URL $url") throw new Exception(s"No protocol found in URL $url")
} }
if (artifact.extra.contains("local")) new File(path)
artifact
else
artifact.copy(extra = artifact.extra + ("local" ->
artifact.copy(
url = local(artifact.url),
checksumUrls = artifact.checksumUrls
.mapValues(local)
.toVector
.toMap,
extra = Map.empty
)
))
} }
private def readFullyTo( private def readFullyTo(
@ -296,30 +284,15 @@ object Cache {
implicit val pool0 = pool implicit val pool0 = pool
val artifact0 = withLocal(artifact, cache)
.extra
.getOrElse("local", artifact)
// Reference file - if it exists, and we get not found errors on some URLs, we assume // Reference file - if it exists, and we get not found errors on some URLs, we assume
// we can keep track of these missing, and not try to get them again later. // we can keep track of these missing, and not try to get them again later.
val referenceFileOpt = { val referenceFileOpt = artifact
val referenceOpt = artifact.extra.get("metadata").map(withLocal(_, cache)) .extra
val referenceOpt0 = referenceOpt.map(a => a.extra.getOrElse("local", a)) .get("metadata")
.map(a => localFile(a.url, cache))
referenceOpt0.map(a => new File(a.url))
}
def referenceFileExists: Boolean = referenceFileOpt.exists(_.exists()) def referenceFileExists: Boolean = referenceFileOpt.exists(_.exists())
val pairs =
Seq(artifact0.url -> artifact.url) ++ {
checksums
.intersect(artifact0.checksumUrls.keySet)
.intersect(artifact.checksumUrls.keySet)
.toSeq
.map(sumType => artifact0.checksumUrls(sumType) -> artifact.checksumUrls(sumType))
}
def urlConn(url0: String) = { def urlConn(url0: String) = {
val conn = url(url0).openConnection() // FIXME Should this be closed? val conn = url(url0).openConnection() // FIXME Should this be closed?
// Dummy user-agent instead of the default "Java/...", // Dummy user-agent instead of the default "Java/...",
@ -551,9 +524,17 @@ object Cache {
} }
} }
val urls =
artifact.url +: {
checksums
.intersect(artifact.checksumUrls.keySet)
.toSeq
.map(artifact.checksumUrls)
}
val tasks = val tasks =
for ((f, url) <- pairs) yield { for (url <- urls) yield {
val file = new File(f) val file = localFile(url, cache)
val res = val res =
if (url.startsWith("file:/")) { if (url.startsWith("file:/")) {
@ -637,27 +618,26 @@ object Cache {
implicit val pool0 = pool implicit val pool0 = pool
val artifact0 = withLocal(artifact, cache) val localFile0 = localFile(artifact.url, cache)
.extra
.getOrElse("local", artifact)
EitherT { EitherT {
artifact0.checksumUrls.get(sumType) match { artifact.checksumUrls.get(sumType) match {
case Some(sumFile) => case Some(sumUrl) =>
val sumFile = localFile(sumUrl, cache)
Task { Task {
val sumOpt = parseChecksum( val sumOpt = parseChecksum(
new String(NioFiles.readAllBytes(new File(sumFile).toPath), "UTF-8") new String(NioFiles.readAllBytes(sumFile.toPath), "UTF-8")
) )
sumOpt match { sumOpt match {
case None => case None =>
FileError.ChecksumFormatError(sumType, sumFile).left FileError.ChecksumFormatError(sumType, sumFile.getPath).left
case Some(sum) => case Some(sum) =>
val md = MessageDigest.getInstance(sumType) val md = MessageDigest.getInstance(sumType)
val f = new File(artifact0.url) val is = new FileInputStream(localFile0)
val is = new FileInputStream(f)
try withContent(is, md.update(_, 0, _)) try withContent(is, md.update(_, 0, _))
finally is.close() finally is.close()
@ -671,14 +651,14 @@ object Cache {
sumType, sumType,
calculatedSum.toString(16), calculatedSum.toString(16),
sum.toString(16), sum.toString(16),
artifact0.url, localFile0.getPath,
sumFile sumFile.getPath
).left ).left
} }
} }
case None => case None =>
Task.now(FileError.ChecksumNotFound(sumType, artifact0.url).left) Task.now(FileError.ChecksumNotFound(sumType, localFile0.getPath).left)
} }
} }
} }