diff --git a/src/org/moparscape/res/CompleteRunnable.java b/src/org/moparscape/res/CompleteRunnable.java index 286211f..4be8e82 100644 --- a/src/org/moparscape/res/CompleteRunnable.java +++ b/src/org/moparscape/res/CompleteRunnable.java @@ -33,12 +33,13 @@ public abstract class CompleteRunnable implements Runnable { protected int uid = -1; protected String url = null; protected String savePath = null; - protected boolean extract, uniqueFolder; + protected boolean extract, uniqueFolder, success; protected ChecksumInfo ci = null; protected Exception ex = null; - public void set(ResourceGrabber rg, int uid, String url, String savePath, boolean extract, ChecksumInfo ci, boolean uniqueFolder) { + public void set(ResourceGrabber rg, boolean success, int uid, String url, String savePath, boolean extract, ChecksumInfo ci, boolean uniqueFolder) { this.rg = rg; + this.success = success; this.uid = uid; this.url = url; this.savePath = savePath; diff --git a/src/org/moparscape/res/MakeTorrent.java b/src/org/moparscape/res/MakeTorrent.java index 76382fe..fe3f0f1 100644 --- a/src/org/moparscape/res/MakeTorrent.java +++ b/src/org/moparscape/res/MakeTorrent.java @@ -38,7 +38,9 @@ public class MakeTorrent { private File torrentFile = null; private File sharedFile = null; - private String[][] announceList = new String[][]{new String[]{"udp://tracker.moparisthebest.com:2710/announce"}, new String[]{"http://tracker.moparisthebest.com/announce"}, new String[]{"udp://exodus.desync.com:6969", "http://exodus.desync.com:6969/announce"}}; + //private String[][] announceList = new String[][]{new String[]{"udp://tracker.moparisthebest.com:2710/announce"}, new String[]{"http://tracker.moparisthebest.com/announce"}, new String[]{"udp://exodus.desync.com:6969", "http://exodus.desync.com:6969/announce"}}; + //private String[][] announceList = new String[][]{new String[]{"udp://tracker.moparisthebest.com:2710/announce"}}; + private String[][] announceList = new String[][]{new String[]{"http://tracker.moparisthebest.com:2710/announce"}, new String[]{"http://tracker.moparisthebest.com/announce"}, new String[]{"udp://exodus.desync.com:6969", "http://exodus.desync.com:6969/announce"}}; private String[] urlList = null; private String sha1InfoHash = null; @@ -152,8 +154,8 @@ public class MakeTorrent { //System.out.println("info hash of torrent: " + new Base32().toSha1(base32InfoHash)); System.out.println("CRC of torrent: " + crc); String magTrackers = ""; - if (announceList != null) - for (String[] trackerList : announceList) + if (this.announceList != null) + for (String[] trackerList : this.announceList) for (String tracker : trackerList) magTrackers += "&tr=" + urlEncode(tracker); String[][] magnetTypes = new String[][]{new String[]{"btih", base32InfoHash}, new String[]{"sha1", sha1InfoHash}}; @@ -296,6 +298,8 @@ public class MakeTorrent { } public static void main(String[] args) throws Exception { + //new MakeTorrent("/home/mopar/IdeaProjects/MoparScape4/cachedump/minimal317.9.zip", "http://cache.hybridscape.com/minimal317.9.zip", "http://bob.com/tom"); + //System.exit(0); Debug.debug = true; if (args.length < 1) { System.out.println("Usage: MakeTorrent file [webseed...]"); diff --git a/src/org/moparscape/res/ResourceGrabber.java b/src/org/moparscape/res/ResourceGrabber.java index d93a98d..b64f4bd 100644 --- a/src/org/moparscape/res/ResourceGrabber.java +++ b/src/org/moparscape/res/ResourceGrabber.java @@ -396,31 +396,40 @@ public class ResourceGrabber { } - public void download(final String url, final String savePath, final boolean extract, final ChecksumInfo ci, final boolean uniqueFolder, final Runnable run) { + public int download(final String url, final String savePath, final boolean extract, final ChecksumInfo ci, final boolean uniqueFolder, final Runnable run) { final ResourceGrabber rg = this; final CompleteRunnable crun; if (run instanceof CompleteRunnable) crun = (CompleteRunnable) run; else crun = null; + + int tryUid = -1; + try { + tryUid = rg.download(url, savePath, extract, ci, uniqueFolder); + } catch (Exception e) { + if (crun != null) + crun.setEx(e); + } + final int uid = tryUid; new Thread() { public void run() { - int uid = -1; + boolean success = false; try { - uid = rg.download(url, savePath, extract, ci, uniqueFolder); - rg.wait(uid, false); + success = rg.wait(uid, false); } catch (Exception e) { if (crun != null) crun.setEx(e); } if (crun != null) - crun.set(rg, uid, url, savePath, extract, ci, uniqueFolder); + crun.set(rg, success, uid, url, savePath, extract, ci, uniqueFolder); run.run(); rg.freeResources(uid); } }.start(); + return uid; } public boolean downloadWait(String url, String savePath) throws Exception { @@ -508,12 +517,10 @@ public class ResourceGrabber { private Downloader getSupportedDownloader(String url) throws MalformedURLException { for (Downloader dl : this.downloaders) - if (dl.supportsURL(url)) + if (dl.supportsURL(url)) { + //System.out.println("url: " + url + " returning: " + dl); return dl; - // if it's a file, put a "file://" in front of it - // or allow files - if (new File(url).exists()) - return this.getSupportedDownloader("file://"+url); + } throw new MalformedURLException("Unsupported URL: " + url); } @@ -611,7 +618,7 @@ public class ResourceGrabber { synchronized (files) { for (String file : files) for (String suffix : suffixes) - if (((ignoreCase && suffix != null && file.toLowerCase().endsWith(suffix.toLowerCase())) || file.endsWith(suffix)) && !file.endsWith(fileListFile)) + if (((ignoreCase && suffix != null && file.toLowerCase().endsWith(suffix.toLowerCase())) || file.endsWith(suffix)) && !file.endsWith(fileListFile)) return file; // if the last value in suffixes is null, that is a special meaning to return the first file if no // others can be found that matches the previous ones @@ -627,12 +634,12 @@ public class ResourceGrabber { public void actionPerformed(ActionEvent e) { synchronized (downloadItems) { for (final DlListener dll : downloadItems) { - /* + /**/ System.out.println("-------------------------------------------------------------"); System.out.println("uid: " + dll.uid); System.out.println("dll: " + dll); System.out.println("-------------------------------------------------------------"); -*/ + DownloadListener.Status status = dll.pollStatus(); if (status == null) status = dll.getStatus(); @@ -768,6 +775,8 @@ public class ResourceGrabber { @Override public synchronized void finished(String savePath, String... filesDownloaded) { + if (savePath != null && !savePath.endsWith("/")) + savePath += "/"; /* if (extract) for (String file : filesDownloaded) @@ -776,11 +785,13 @@ public class ResourceGrabber { if (files != null) files.clear(); // if we are supposed to extract it, do so, add the names to files if they extract - for (String file : filesDownloaded) + for (String file : filesDownloaded) { + file = savePath + file; if (!(extract && Downloader.supportsExtraction(file) && Downloader.extractFile(file, savePath, this, files)) && files != null) files.add(file); + } // write files to a file in the savePath, to be used on later runs to see what to CRC if (files != null) { @@ -837,7 +848,7 @@ public class ResourceGrabber { } public boolean download(String url, String savePath, boolean extract, ChecksumInfo ci) throws Exception { - return rg.wait(rg.download(url, savePath, extract, ci)); + return rg.wait(rg.download(url, savePath, extract, ci), true); } /** @@ -929,8 +940,8 @@ public class ResourceGrabber { public String toString() { return "DownloadItemPanel{" + "infoLabel=" + infoLabel.getText() + - ", titleLabel=" + titleLabel.getText() + - ", origInfo='" + origInfo + '\'' + + ", titleLabel=" + titleLabel.getText() + + ", origInfo='" + origInfo + '\'' + ", progressBar=" + progressBar.getPercentComplete() + '}'; } diff --git a/src/org/moparscape/res/impl/Downloader.java b/src/org/moparscape/res/impl/Downloader.java index 4106460..53c55b3 100644 --- a/src/org/moparscape/res/impl/Downloader.java +++ b/src/org/moparscape/res/impl/Downloader.java @@ -20,6 +20,7 @@ package org.moparscape.res.impl; +import org.moparscape.Debug; import org.moparscape.res.ChecksumInfo; import org.moparscape.res.DownloadListener; @@ -168,6 +169,7 @@ public abstract class Downloader { public static boolean extractFile(String fileName, String savePath, DownloadListener callback, Checksum cs, java.util.List files) { if(savePath != null && !savePath.endsWith("/")) savePath += "/"; + Debug.debug("extractFile: fileName: '%s', savePath: '%s'", fileName, savePath); File file = new File(fileName); try { long length = file.length(); diff --git a/src/org/moparscape/res/impl/URLDownloader.java b/src/org/moparscape/res/impl/URLDownloader.java index bdada54..5af6559 100644 --- a/src/org/moparscape/res/impl/URLDownloader.java +++ b/src/org/moparscape/res/impl/URLDownloader.java @@ -53,7 +53,16 @@ public class URLDownloader extends Downloader { public void run() { try { String saveTo = saveTo(url, savePath); - URLConnection uc = new URL(url).openConnection(); + URL toDownload = null; + try{ + toDownload = new URL(url); + }catch(MalformedURLException e){ + if(new File(url).exists()) + toDownload = new URL("file://"+url); + else + throw e; + } + URLConnection uc = toDownload.openConnection(); if (uc instanceof HttpURLConnection) { String userAgent = System.getProperty("http.agent"); if (userAgent == null) @@ -71,7 +80,7 @@ public class URLDownloader extends Downloader { writeStream(in, new FileOutputStream(saveTo)); if (callback != null) { - callback.finished(savePath, saveTo); + callback.finished(savePath, new File(saveTo).getName()); callback.stopped(); } } catch (IOException e) { @@ -103,7 +112,9 @@ public class URLDownloader extends Downloader { new URL(url); return true; } catch (MalformedURLException e) { - return false; + // if it's a file, put a "file://" in front of it + // or allow files + return new File(url).exists(); } } @@ -117,7 +128,7 @@ public class URLDownloader extends Downloader { url = url.toLowerCase().replaceFirst(".*://", "").replaceAll("/+", ".").replaceAll("(^\\.|\\.$)", ""); - return url+"/"; + return url + "/"; } @Override