Check Download-Quota HTTP header when downloading subtitles and abort if quota has been reached.

This commit is contained in:
Reinhard Pointner 2016-04-06 12:01:40 +00:00
parent 8e5ec6b2a8
commit c79896f827
1 changed files with 24 additions and 15 deletions

View File

@ -1,10 +1,13 @@
package net.filebot.web;
import static net.filebot.Logging.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.util.EnumMap;
import java.util.Map;
@ -112,27 +115,33 @@ public class OpenSubtitlesSubtitleDescriptor implements SubtitleDescriptor, Seri
return Integer.parseInt(getProperty(Property.SubSumCD));
}
private static int DOWNLOAD_QUOTA = 1000;
@Override
public ByteBuffer fetch() throws Exception {
URL resource = new URL(getProperty(Property.SubDownloadLink));
InputStream stream = resource.openStream();
try {
ByteBufferOutputStream buffer = new ByteBufferOutputStream(getLength());
// extract gzipped subtitle on-the-fly
try {
stream = new GZIPInputStream(stream);
} catch (ZipException e) {
throw new IOException(String.format("%s: anti-leech limit has been reached", e.getMessage()));
if (DOWNLOAD_QUOTA <= 0) {
throw new IOException("Download-Quota has been exceeded");
}
// fully download
buffer.transferFully(stream);
URLConnection c = new URL(getProperty(Property.SubDownloadLink)).openConnection();
try (InputStream in = c.getInputStream()) {
// check download quota
String quota = c.getHeaderField("Download-Quota");
if (quota != null) {
if ((DOWNLOAD_QUOTA = Integer.parseInt(quota)) <= 0) {
throw new IOException("Download-Quota has been exceeded");
}
debug.finest(format("Download-Quota: %d", DOWNLOAD_QUOTA));
}
// read and extract subtitle data
ByteBufferOutputStream buffer = new ByteBufferOutputStream(getLength());
try {
buffer.transferFully(new GZIPInputStream(in));
} catch (ZipException e) {
throw new IOException("Download-Quota has been exceeded: " + e.getMessage());
}
return buffer.getByteBuffer();
} finally {
stream.close();
}
}