1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-16 14:25:02 -05:00

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

View File

@ -1,10 +1,13 @@
package net.filebot.web; package net.filebot.web;
import static net.filebot.Logging.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Serializable; import java.io.Serializable;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
@ -112,27 +115,33 @@ public class OpenSubtitlesSubtitleDescriptor implements SubtitleDescriptor, Seri
return Integer.parseInt(getProperty(Property.SubSumCD)); return Integer.parseInt(getProperty(Property.SubSumCD));
} }
private static int DOWNLOAD_QUOTA = 1000;
@Override @Override
public ByteBuffer fetch() throws Exception { public ByteBuffer fetch() throws Exception {
URL resource = new URL(getProperty(Property.SubDownloadLink)); if (DOWNLOAD_QUOTA <= 0) {
InputStream stream = resource.openStream(); throw new IOException("Download-Quota has been exceeded");
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()));
} }
// fully download URLConnection c = new URL(getProperty(Property.SubDownloadLink)).openConnection();
buffer.transferFully(stream); 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(); return buffer.getByteBuffer();
} finally {
stream.close();
} }
} }