aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/jshipit/BlobDownloader.java
blob: 3aafec49dc4560f54b2418d36733add7924a998f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package io.github.jshipit;

import me.tongfei.progressbar.ProgressBar;
import org.apache.commons.compress.archivers.tar.*;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;

import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;

public class BlobDownloader extends Thread {

    private final String url;
    private final String digest;
    private final String[][] headers;
    private final String tmpdir;
    private final boolean extract;
    private final String renameTo;

    public BlobDownloader(String url, String digest, String[][] headers, String tmpdir, boolean extract, String renameTo) {
        this.url = url;
        this.digest = digest;
        this.headers = headers;
        this.tmpdir = tmpdir;
        this.extract = extract;
        this.renameTo = renameTo;
    }

    public void run() {
        URL url_obj = null;
        try {
            url_obj = new URI(this.url).toURL();
            System.out.println(url_obj);
        } 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:", "") + ".tar.gz")) {
            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();
        }

        if (this.renameTo == null) {
            return;
        }

        Path rename = new File(renameTo).toPath();
        Path current = new File(tmpdir + "/" + digest.replace("sha256:", "")+".tar.gz").toPath();
        try {
            Files.move(current, rename);
        } catch (IOException e) {
            System.out.println("Failed to rename file "+digest);
            e.printStackTrace();
        }

        if (!extract) {
            return;
        }

        File extracted = new File(tmpdir + "/" + digest.replace("sha256:", ""));
        if (extracted.exists()) {
            extracted.delete();
        }
        TarManager tarManager = new TarManager();
        try {
            tarManager.untar(tmpdir + "/" + digest.replace("sha256:", "") + ".tar.gz", extracted);
        } catch (IOException e) {
            System.out.println("Failed to extract blob "+digest);
            e.printStackTrace();
        }
        File tar = new File(tmpdir + "/" + digest.replace("sha256:", "") + ".tar.gz");
        if (tar.exists()) {
            tar.delete();
        }
    }
}