Reputation: 391
I want to code a own Mod Installer for my Minecraft-Mod.
There is one problem: I'd like to use the GitHub releases to download the preferred version.
The links there (Something like: https://github.com/TheOnlySilverClaw/Reforged/releases/download/v0.6-alpha/reforged-0.6-alpha-1.8.jar ) aren't direct links, so that I can't download directly from them using OutputStreams.
The "direct" links are something like that: https://github-cloud.s3.amazonaws.com/releases/48553472/d19e9d64-d436-11e5-802b-9cc751a6de68.jar?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAISTNZFOVBIJMK3TQ%2F20160228%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20160228T223303Z&X-Amz-Expires=300&X-Amz-Signature=664ca72a54472e2ef7c3e4c5a0e9e552607b010bdf43c83c967319eb0110dd1a&X-Amz-SignedHeaders=host&actor_id=12763829&response-content-disposition=attachment%3B%20filename%3Dreforged-0.6-alpha-1.8.9.jar&response-content-type=application%2Foctet-stream
Is there a way to retrieve an easier Download URL (has to be DIRECT LINK!)?
I don't want to upload every file 3 times :/
Upvotes: 1
Views: 310
Reputation: 391
Thanks for all your help! I found a sample code and modified it (sorry, I can't say the source, as I don't find it anymore...):
public void sample() throws IOException {
String link =
"https://github.com/TheOnlySilverClaw/Reforged/releases/download/v0.6-alpha/reforged-0.6-alpha-1.8.jar";
String fileName = "reforged-0.6-alpha-1.8.jar";
URL url = new URL( link );
HttpURLConnection http = (HttpURLConnection)url.openConnection();
Map< String, List< String >> header = http.getHeaderFields();
while( isRedirected( header )) {
link = header.get( "Location" ).get( 0 );
url = new URL( link );
http = (HttpURLConnection)url.openConnection();
header = http.getHeaderFields();
}
InputStream input = http.getInputStream();
byte[] buffer = new byte[4096];
int n = -1;
OutputStream output = new FileOutputStream( new File("C:/Users/Nico/Desktop/" + fileName));
while ((n = input.read(buffer)) != -1) {
output.write( buffer, 0, n );
}
output.close();
}
private static boolean isRedirected( Map<String, List<String>> header ) {
for( String hv : header.get( null )) {
if( hv.contains( " 301 " )
|| hv.contains( " 302 " )) return true;
}
return false;
}
Thanks for your answers! ^^
Upvotes: 0
Reputation: 3354
Here is info how you can download single file via any HTTP client (curl, you java HTTP client etc.).
Another approach is using some cloud CI (e.g travis-ci), take sources from you GitHub repo into cloud building process and release artifact anywhere you need.
You also can use GitHub REST API for get a single release asset
Upvotes: 1
Reputation: 4695
One way to achieve this without much complexity is to leave the downloading part to a proven tool, leaving the release URL's intact. For instance, if you do something like: curl -L -O https://github.com/TheOnlySilverClaw/Reforged/releases/download/v0.6-alpha/reforged-0.6-alpha-1.8.jar
, then cUrl
being cUrl
will do the right thing and download the ultimate JAR file following all redirect links.
If you were to do it yourself, you can of course use excellent HTTP client libraries like Apache HTTP client. But I'd suggest you leave it to a preprocessing tool like cUrl. Once it is downloaded locally, you can always do what you need in your program or installer.
I'd strongly suggest against using the ultimate link (the long link from AWS you posted in your question) perhaps because it is subject to change without notice. The link to the release JAR file (https://github.com/TheOnlySilverClaw/Reforged/releases/download/v0.6-alpha/reforged-0.6-alpha-1.8.jar
) is official interface and is less likely to change.
Upvotes: 0