aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/fabricmc/loom/util/DownloadUtil.java
blob: fcc79aa7525e1ba5a2c62c35e703d988138239f9 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * This file is part of fabric-loom, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2019 Chocohead
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.fabricmc.loom.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;

import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;

import net.fabricmc.loom.LoomGradlePlugin;

public class DownloadUtil {
	/**
	 * Download from the given {@link URL} to the given {@link File} so long as there are differences between them.
	 *
	 * @param from The URL of the file to be downloaded
	 * @param to The destination to be saved to, and compared against if it exists
	 * @param logger The logger to print everything to, typically from {@link Project#getLogger()}
	 * @throws IOException If an exception occurs during the process
	 */
	public static void downloadIfChanged(URL from, File to, Logger logger) throws IOException {
		downloadIfChanged(from, to, logger, false);
	}

	/**
	 * Download from the given {@link URL} to the given {@link File} so long as there are differences between them.
	 *
	 * @param from The URL of the file to be downloaded
	 * @param to The destination to be saved to, and compared against if it exists
	 * @param logger The logger to print information to, typically from {@link Project#getLogger()}
	 * @param quiet Whether to only print warnings (when <code>true</code>) or everything
	 * @throws IOException If an exception occurs during the process
	 */
	public static void downloadIfChanged(URL from, File to, Logger logger, boolean quiet) throws IOException {
		HttpURLConnection connection = (HttpURLConnection) from.openConnection();

		if (LoomGradlePlugin.refreshDeps) {
			getETagFile(to).delete();
			to.delete();
		}

		// If the output already exists we'll use it's last modified time
		if (to.exists()) {
			connection.setIfModifiedSince(to.lastModified());
		}

		//Try use the ETag if there's one for the file we're downloading
		String etag = loadETag(to, logger);

		if (etag != null) {
			connection.setRequestProperty("If-None-Match", etag);
		}

		// We want to download gzip compressed stuff
		connection.setRequestProperty("Accept-Encoding", "gzip");

		// Try make the connection, it will hang here if the connection is bad
		connection.connect();

		int code = connection.getResponseCode();

		if ((code < 200 || code > 299) && code != HttpURLConnection.HTTP_NOT_MODIFIED) {
			//Didn't get what we expected
			delete(to);
			throw new IOException(connection.getResponseMessage() + " for " + from);
		}

		long modifyTime = connection.getHeaderFieldDate("Last-Modified", -1);

		if (to.exists() && (code == HttpURLConnection.HTTP_NOT_MODIFIED || modifyTime > 0 && to.lastModified() >= modifyTime)) {
			if (!quiet) {
				logger.info("'{}' Not Modified, skipping.", to);
			}

			return; //What we've got is already fine
		}

		long contentLength = connection.getContentLengthLong();

		if (!quiet && contentLength >= 0) {
			logger.info("'{}' Changed, downloading {}", to, toNiceSize(contentLength));
		}

		try { // Try download to the output
			InputStream inputStream = connection.getInputStream();

			if ("gzip".equals(connection.getContentEncoding())) {
				inputStream = new GZIPInputStream(inputStream);
			}

			FileUtils.copyInputStreamToFile(inputStream, to);
		} catch (IOException e) {
			delete(to); // Probably isn't good if it fails to copy/save
			throw e;
		}

		//Set the modify time to match the server's (if we know it)
		if (modifyTime > 0) {
			to.setLastModified(modifyTime);
		}

		//Save the ETag (if we know it)
		String eTag = connection.getHeaderField("ETag");

		if (eTag != null) {
			//Log if we get a weak ETag and we're not on quiet
			if (!quiet && eTag.startsWith("W/")) {
				logger.warn("Weak ETag found.");
			}

			saveETag(to, eTag, logger);
		}
	}

	/**
	 * Creates a new file in the same directory as the given file with <code>.etag</code> on the end of the name.
	 *
	 * @param file The file to produce the ETag for
	 * @return The (uncreated) ETag file for the given file
	 */
	private static File getETagFile(File file) {
		return new File(file.getAbsoluteFile().getParentFile(), file.getName() + ".etag");
	}

	/**
	 * Attempt to load an ETag for the given file, if it exists.
	 *
	 * @param to The file to load an ETag for
	 * @param logger The logger to print errors to if it goes wrong
	 * @return The ETag for the given file, or <code>null</code> if it doesn't exist
	 */
	private static String loadETag(File to, Logger logger) {
		File eTagFile = getETagFile(to);

		if (!eTagFile.exists()) {
			return null;
		}

		try {
			return Files.asCharSource(eTagFile, StandardCharsets.UTF_8).read();
		} catch (IOException e) {
			logger.warn("Error reading ETag file '{}'.", eTagFile);
			return null;
		}
	}

	/**
	 * Saves the given ETag for the given file, replacing it if it already exists.
	 *
	 * @param to The file to save the ETag for
	 * @param eTag The ETag to be saved
	 * @param logger The logger to print errors to if it goes wrong
	 */
	private static void saveETag(File to, String eTag, Logger logger) {
		File eTagFile = getETagFile(to);

		try {
			if (!eTagFile.exists()) {
				eTagFile.createNewFile();
			}

			Files.asCharSink(eTagFile, StandardCharsets.UTF_8).write(eTag);
		} catch (IOException e) {
			logger.warn("Error saving ETag file '{}'.", eTagFile, e);
		}
	}

	/**
	 * Format the given number of bytes as a more human readable string.
	 *
	 * @param bytes The number of bytes
	 * @return The given number of bytes formatted to kilobytes, megabytes or gigabytes if appropriate
	 */
	public static String toNiceSize(long bytes) {
		if (bytes < 1024) {
			return bytes + " B";
		} else if (bytes < 1024 * 1024) {
			return bytes / 1024 + " KB";
		} else if (bytes < 1024 * 1024 * 1024) {
			return String.format("%.2f MB", bytes / (1024.0 * 1024.0));
		} else {
			return String.format("%.2f GB", bytes / (1024.0 * 1024.0 * 1024.0));
		}
	}

	/**
	 * Delete the file along with the corresponding ETag, if it exists.
	 *
	 * @param file The file to delete.
	 */
	public static void delete(File file) {
		if (file.exists()) {
			file.delete();
		}

		File etagFile = getETagFile(file);

		if (etagFile.exists()) {
			etagFile.delete();
		}
	}
}