Skip to content

Commit

Permalink
Retry asset download automatically in case of checksum mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenraven committed Apr 26, 2023
1 parent 2ede846 commit 4f36c8e
Showing 1 changed file with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.inject.Inject;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.RegularFileProperty;
Expand Down Expand Up @@ -110,36 +111,42 @@ public void execute() {
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
IOUtils.copy(bis, bos);
bos.flush();

final String realSha1;
try {
realSha1 = new DigestUtils(DigestUtils.getSha1Digest())
.digestAsHex(params.getTargetFile().get());
} catch (IOException e) {
throw new RuntimeException(e);
}
if (!realSha1.equals(params.getSha1().get())) {
bos.close();
fos.close();
FileUtils.deleteQuietly(params.getTargetFile().get());
throw new RuntimeException(
String.format(
"Asset %s sha1sum doesn't match! Downloaded: %s Expected: %s",
params.getTargetFile().get().getAbsolutePath(),
realSha1,
params.getSha1().get()));
}

System.out.printf(
"Downloaded asset (%3d/%3d) %s\n",
downloaded.incrementAndGet(),
toDownload.get(),
params.getSha1().get());
break;
} catch (Exception e) {
System.err.println(e);
System.err.println(e.getMessage());
e.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// no-op
}
}

final String realSha1;
try {
realSha1 = new DigestUtils(DigestUtils.getSha1Digest()).digestAsHex(params.getTargetFile().get());
} catch (IOException e) {
throw new RuntimeException(e);
}
if (!realSha1.equals(params.getSha1().get())) {
throw new RuntimeException(
String.format(
"Asset %s sha1sum doesn't match! Downloaded: %s Expected: %s",
params.getTargetFile().get().getAbsolutePath(),
realSha1,
params.getSha1().get()));
}
}
}
}

0 comments on commit 4f36c8e

Please sign in to comment.