aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/jshipit/BlobDownloader.java
diff options
context:
space:
mode:
authoraxtloss <axtlos@getcryst.al>2023-05-15 22:36:20 +0200
committeraxtloss <axtlos@getcryst.al>2023-05-15 22:36:20 +0200
commit9736e92073502adcfd466a629f10464f518e2c15 (patch)
tree8d9b0d2b935fe95f5edbcd635b1dbd29313bbee1 /src/main/java/io/github/jshipit/BlobDownloader.java
parent01c8a59f8aa41296b791c8a64aa3b42da8e7458e (diff)
downloadjshipit-9736e92073502adcfd466a629f10464f518e2c15.tar.gz
jshipit-9736e92073502adcfd466a629f10464f518e2c15.tar.bz2
multithreaded downloads and non docker registries
Runs each layer download on a different thread. Allows using registries other than registry.docker.io
Diffstat (limited to '')
-rw-r--r--src/main/java/io/github/jshipit/BlobDownloader.java77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/main/java/io/github/jshipit/BlobDownloader.java b/src/main/java/io/github/jshipit/BlobDownloader.java
new file mode 100644
index 0000000..5fa3959
--- /dev/null
+++ b/src/main/java/io/github/jshipit/BlobDownloader.java
@@ -0,0 +1,77 @@
+package io.github.jshipit;
+
+import me.tongfei.progressbar.ProgressBar;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.*;
+
+public class BlobDownloader extends Thread {
+
+ private final String url;
+ private final String digest;
+ private final String[][] headers;
+ private final String tmpdir;
+
+ public BlobDownloader(String url, String digest, String[][] headers, String tmpdir) {
+ this.url = url;
+ this.digest = digest;
+ this.headers = headers;
+ this.tmpdir = tmpdir;
+ }
+
+ public void run() throws RuntimeException {
+ URL url_obj = null;
+ try {
+ url_obj = new URI(this.url).toURL();
+ System.out.println(url_obj.toString());
+ } catch(URISyntaxException | MalformedURLException e) {
+ System.out.println("URISyntaxException | MalformedURLException");
+ e.printStackTrace();
+ }
+
+ assert url_obj != null;
+ HttpURLConnection con;
+
+ try {
+ con = (HttpURLConnection) url_obj.openConnection();
+ con.setRequestMethod("GET");
+ for (String[] header : this.headers) {
+ con.setRequestProperty(header[0], header[1]);
+ }
+ con.connect();
+
+ if (con.getResponseCode() != 200) {
+ System.out.println("Error: " + con.getResponseCode());
+ throw new RuntimeException("Failed : HTTP error code : "
+ + con.getResponseCode());
+ } else {
+ System.out.println("Success: " + con.getResponseCode());
+ }
+ } catch (IOException e) {
+ System.out.println("Failed to connect");
+ e.printStackTrace();
+ return;
+ }
+
+
+ int fileSize = con.getContentLength();
+
+ try (InputStream in = con.getInputStream();
+ OutputStream out = new FileOutputStream(tmpdir + "/" + digest.replace("sha256:", "") + "layer.tar")) {
+ byte[] buffer = new byte[4096];
+ int bytesRead;
+ try (ProgressBar pb = new ProgressBar("Download blob "+digest.replace("sha256:", ""), fileSize)) {
+ while ((bytesRead = in.read(buffer)) != -1) {
+ out.write(buffer, 0, bytesRead);
+ pb.stepBy(bytesRead);
+ }
+ }
+ } catch (IOException e) {
+ System.out.println("Failed to download blob "+digest);
+ e.printStackTrace();
+ }
+ }
+}