From 66126e5a5527e7d500ce3523106eebf506092b15 Mon Sep 17 00:00:00 2001 From: Franckyi Date: Thu, 5 Apr 2018 21:41:44 +0200 Subject: [PATCH] Fixed #3 --- pom.xml | 2 +- .../java/com/github/franckyi/cmpdl/CMPDL.java | 2 +- .../franckyi/cmpdl/task/CustomTask.java | 22 ++++++++++++++----- .../franckyi/cmpdl/task/DownloadModsTask.java | 5 +++-- .../cmpdl/task/VerifyProjectTask.java | 12 +++++----- src/main/resources/interface.fxml | 2 +- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 277ef93..4ef00e2 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.franckyi cmpdl - 2.0.0-b3 + 2.0.0 1.8 1.8 diff --git a/src/main/java/com/github/franckyi/cmpdl/CMPDL.java b/src/main/java/com/github/franckyi/cmpdl/CMPDL.java index bd5d3ad..cfb9359 100644 --- a/src/main/java/com/github/franckyi/cmpdl/CMPDL.java +++ b/src/main/java/com/github/franckyi/cmpdl/CMPDL.java @@ -14,7 +14,7 @@ public class CMPDL extends Application { private static final String NAME = "Curse Modpack Downloader"; - private static final String VERSION = "2.0.0-b3"; + private static final String VERSION = "2.0.0"; private static final String AUTHOR = "Franckyi (original version by Vazkii)"; public static String title() { diff --git a/src/main/java/com/github/franckyi/cmpdl/task/CustomTask.java b/src/main/java/com/github/franckyi/cmpdl/task/CustomTask.java index 63260a4..aeee944 100644 --- a/src/main/java/com/github/franckyi/cmpdl/task/CustomTask.java +++ b/src/main/java/com/github/franckyi/cmpdl/task/CustomTask.java @@ -7,6 +7,8 @@ import java.io.IOException; import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; abstract class CustomTask extends Task { @@ -37,18 +39,28 @@ InterfaceController getController() { return CMPDL.controller; } - String crawl(String url) throws IOException, IllegalArgumentException { + String crawl(String url) throws IOException, URISyntaxException { + URL url0 = new URL(crawl0(url)); + return new URI(url0.getProtocol(), url0.getHost(), url0.getFile(), null).toASCIIString(); + } + + String crawlAddHost(String url) throws IOException, URISyntaxException { + URL url0 = new URL(crawlAddHost0(url)); + return new URI(url0.getProtocol(), url0.getHost(), url0.getFile(), null).toASCIIString(); + } + + private String crawl0(String url) throws IOException, IllegalArgumentException, URISyntaxException { String location = getLocation(url); - return location != null ? crawl(location) : url; + return location != null ? crawl0(location) : url; } - String crawlAddHost(String url) throws IOException, IllegalArgumentException { + private String crawlAddHost0(String url) throws IOException, IllegalArgumentException, URISyntaxException { URL url0 = new URL(url); String location = getLocation(url); - return location != null ? location.contains("://") ? crawlAddHost(location) : crawl(url0.getProtocol() + "://" + url0.getHost() + location) : url; + return location != null ? location.contains("://") ? crawlAddHost0(location) : crawl0(url0.getProtocol() + "://" + url0.getHost() + location) : url; } - private String getLocation(String url) throws IOException { + private String getLocation(String url) throws IOException, URISyntaxException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setInstanceFollowRedirects(false); if (connection.getResponseCode() < 200 || connection.getResponseCode() >= 400) diff --git a/src/main/java/com/github/franckyi/cmpdl/task/DownloadModsTask.java b/src/main/java/com/github/franckyi/cmpdl/task/DownloadModsTask.java index 3dbdb7f..b638b26 100644 --- a/src/main/java/com/github/franckyi/cmpdl/task/DownloadModsTask.java +++ b/src/main/java/com/github/franckyi/cmpdl/task/DownloadModsTask.java @@ -11,6 +11,7 @@ import java.io.FileReader; import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLDecoder; public class DownloadModsTask extends CustomTask { @@ -65,8 +66,8 @@ protected Void call0() throws Exception { updateProgress(0, 1); String url0 = crawl(crawlAddHost("http://minecraft.curseforge.com/projects/" + fileJson.projectID) + "/files/" + fileJson.fileID + "/download"); if (DownloadModsTask.this.isCancelled()) return null; - URL url = new URL(url0); - String fileName = new File(url.getFile()).getName().replaceAll("%20", " "); + URL url = new URL(url0.replaceAll("%25", "%")); + String fileName = new File(URLDecoder.decode(url.getFile(), "UTF-8")).getName(); log("> Downloading " + fileName); Platform.runLater(() -> getController().setSecondaryProgress(this, fileName)); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); diff --git a/src/main/java/com/github/franckyi/cmpdl/task/VerifyProjectTask.java b/src/main/java/com/github/franckyi/cmpdl/task/VerifyProjectTask.java index f864e55..b3619ab 100644 --- a/src/main/java/com/github/franckyi/cmpdl/task/VerifyProjectTask.java +++ b/src/main/java/com/github/franckyi/cmpdl/task/VerifyProjectTask.java @@ -31,10 +31,10 @@ protected Optional call0() throws Exception { protocol = url.getProtocol(); host = url.getHost(); folder = url.getFile(); - isCurse = host.equals("mods.curse.com"); + isCurse = host.equals("www.curseforge.com"); protocolValid = Arrays.asList("http", "https").contains(protocol); - hostValid = Arrays.asList("mods.curse.com", "minecraft.curseforge.com", "www.feed-the-beast.com").contains(host); - folderValid = isCurse ? folder.startsWith("/modpacks/minecraft/") && folder.split("/").length == 4 : folder.startsWith("/projects/") && folder.split("/").length == 3; + hostValid = Arrays.asList("www.curseforge.com", "minecraft.curseforge.com", "www.feed-the-beast.com").contains(host); + folderValid = isCurse ? folder.startsWith("/minecraft/modpacks/") && folder.split("/").length == 4 : folder.startsWith("/projects/") && folder.split("/").length == 3; if (!protocolValid || !hostValid || !folderValid) { log("The URL is incorrect !" + (!protocolValid ? "\n- Protocol invalid" : "") + (!hostValid ? "\n- Host invalid" : "") + (!folderValid ? "\n- Folder invalid" : "")); return Optional.empty(); @@ -42,12 +42,12 @@ protected Optional call0() throws Exception { if (fileID.isEmpty()) { fileID = "latest"; } - if (host.equals("www.feed-the-beast.com") && fileID.equals("latest")) { - log("You must specify a file ID other than\n'latest' for host www.feed-the-beast.com."); + if ((host.equals("www.feed-the-beast.com") || host.equals("www.curseforge.com")) && fileID.equals("latest")) { + log("You must specify a file ID other than\n'latest' for host www.feed-the-beast.com or www.curseforge.com."); return Optional.empty(); } try { - return Optional.of(crawl(isCurse ? modpackURL + "/" + (fileID.equals("latest") ? "download" : fileID) : modpackURL + "/files/" + fileID + (fileID.equals("latest") ? "" : "/download"))); + return Optional.of(crawl(isCurse ? modpackURL + "/download/" + fileID + "/file" : modpackURL + "/files/" + fileID + (fileID.equals("latest") ? "" : "/download"))); } catch (IOException e) { log("The file doesn't exist or website is unreachable !"); return Optional.empty(); diff --git a/src/main/resources/interface.fxml b/src/main/resources/interface.fxml index 0dcd4ad..8966462 100644 --- a/src/main/resources/interface.fxml +++ b/src/main/resources/interface.fxml @@ -25,7 +25,7 @@ -