Skip to content

Commit

Permalink
Merge branch 'vget-1.0.39'
Browse files Browse the repository at this point in the history
  • Loading branch information
axet committed Dec 20, 2012
2 parents 15e74af + 14841df commit ec75d74
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.axet</groupId>
<artifactId>vget</artifactId>
<packaging>jar</packaging>
<version>1.0.38</version>
<version>1.0.39</version>
<name>vget</name>
<description>vget java video download library</description>
<url>https://github.com/axet/vget</url>
Expand Down
80 changes: 70 additions & 10 deletions src/main/java/com/github/axet/vget/VGet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.axet.vget;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -17,6 +18,7 @@
import com.github.axet.wget.RetryWrap;
import com.github.axet.wget.info.DownloadInfo;
import com.github.axet.wget.info.DownloadInfo.Part;
import com.github.axet.wget.info.ex.DownloadError;
import com.github.axet.wget.info.ex.DownloadIOCodeError;
import com.github.axet.wget.info.ex.DownloadIOError;
import com.github.axet.wget.info.ex.DownloadInterruptedError;
Expand Down Expand Up @@ -102,6 +104,13 @@ static String replaceBadChars(String f) {
return f;
}

static String maxFileLength(String str) {
int max = 50;
if (str.length() > max)
str = str.substring(0, max);
return str;
}

boolean done(AtomicBoolean stop) {
if (stop.get())
throw new DownloadInterruptedError("stop");
Expand Down Expand Up @@ -151,6 +160,13 @@ void retry(AtomicBoolean stop, Runnable notify, Throwable e) {
}

retracted = true;
} catch (DownloadIOCodeError ee) {
if (retry(ee)) {
info.setState(States.RETRYING, ee);
notify.run();
} else {
throw ee;
}
} catch (DownloadRetry ee) {
info.setState(States.RETRYING, ee);
notify.run();
Expand Down Expand Up @@ -181,6 +197,8 @@ void target(DownloadInfo dinfo) {

String sfilename = replaceBadChars(info.getTitle());

sfilename = maxFileLength(sfilename);

String ct = dinfo.getContentType();
if (ct == null)
throw new DownloadRetry("null content type");
Expand Down Expand Up @@ -247,11 +265,8 @@ public void extract(AtomicBoolean stop, Runnable notify) {
} catch (DownloadRetry e) {
retry(stop, notify, e);
} catch (DownloadMultipartError e) {
for (Part ee : e.getInfo().getParts()) {
if (!retry(ee.getException())) {
throw e;
}
}
checkFileNotFound(e);
checkRetry(e);
retry(stop, notify, e);
} catch (DownloadIOCodeError e) {
if (retry(e))
Expand All @@ -264,6 +279,54 @@ public void extract(AtomicBoolean stop, Runnable notify) {
}
}

void checkRetry(DownloadMultipartError e) {
for (Part ee : e.getInfo().getParts()) {
if (!retry(ee.getException())) {
throw e;
}
}
}

/**
* check if all parts has the same filenotfound exception. if so throw
* DownloadError.FilenotFoundexcepiton
*
* @param e
*/
void checkFileNotFound(DownloadMultipartError e) {
FileNotFoundException f = null;
for (Part ee : e.getInfo().getParts()) {
// no error for this part? skip it
if (ee.getException() == null)
continue;
// this exception has no cause? then it is not FileNotFound
// excpetion. then do noting. this is checking function. do not
// rethrow
if (ee.getException().getCause() == null)
return;
if (ee.getException().getCause() instanceof FileNotFoundException) {
// our first filenotfoundexception?
if (f == null) {
// save it for later checks
f = (FileNotFoundException) ee.getException().getCause();
} else {
// check filenotfound error message is it the same?
FileNotFoundException ff = (FileNotFoundException) ee.getException().getCause();
if (!ff.getMessage().equals(f.getMessage())) {
// if the filenotfound exception message is not the
// same. then we cannot retrhow filenotfound exception.
// return and continue checks
return;
}
}
} else {
break;
}
}
if (f != null)
throw new DownloadError(f);
}

public void download(final AtomicBoolean stop, final Runnable notify) {
try {
if (empty()) {
Expand Down Expand Up @@ -325,11 +388,8 @@ public void run() {
} catch (DownloadRetry e) {
retry(stop, notify, e);
} catch (DownloadMultipartError e) {
for (Part ee : e.getInfo().getParts()) {
if (!retry(ee.getException())) {
throw e;
}
}
checkFileNotFound(e);
checkRetry(e);
retry(stop, notify, e);
} catch (DownloadIOCodeError e) {
if (retry(e))
Expand Down

0 comments on commit ec75d74

Please sign in to comment.