Skip to content

Commit fc8865a

Browse files
committed
Maintenance. Specify User-Agent
1 parent c44ce25 commit fc8865a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/main/java/org/mcphackers/mcp/tools/FileUtil.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.net.URL;
8+
import java.net.URLConnection;
89
import java.nio.channels.Channels;
910
import java.nio.channels.FileChannel;
1011
import java.nio.channels.ReadableByteChannel;
@@ -28,6 +29,8 @@
2829
import java.util.zip.ZipInputStream;
2930
import java.util.zip.ZipOutputStream;
3031

32+
import org.mcphackers.mcp.MCP;
33+
3134
public abstract class FileUtil {
3235

3336
public static void delete(Path path) throws IOException {
@@ -128,13 +131,19 @@ public static void downloadFile(String url, Path output) throws IOException {
128131
}
129132

130133
public static void downloadFile(URL url, Path output) throws IOException {
131-
ReadableByteChannel channel = Channels.newChannel(url.openStream());
134+
ReadableByteChannel channel = Channels.newChannel(openURLStream(url));
132135
try (FileOutputStream stream = new FileOutputStream(output.toAbsolutePath().toString())) {
133136
FileChannel fileChannel = stream.getChannel();
134137
fileChannel.transferFrom(channel, 0, Long.MAX_VALUE);
135138
}
136139
}
137140

141+
public static InputStream openURLStream(URL url) throws IOException {
142+
URLConnection connection = url.openConnection();
143+
connection.setRequestProperty("User-Agent", "RetroMCP/" + MCP.VERSION);
144+
return connection.getInputStream();
145+
}
146+
138147
public static void deleteDirectoryIfExists(Path path) throws IOException {
139148
if (Files.isDirectory(path)) {
140149
deleteDirectory(path);

0 commit comments

Comments
 (0)