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; 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 { URLConnection c = new URL(getProperty(Property.SubDownloadLink)).openConnection();
ByteBufferOutputStream buffer = new ByteBufferOutputStream(getLength()); try (InputStream in = c.getInputStream()) {
// check download quota
// extract gzipped subtitle on-the-fly String quota = c.getHeaderField("Download-Quota");
try { if (quota != null) {
stream = new GZIPInputStream(stream); if ((DOWNLOAD_QUOTA = Integer.parseInt(quota)) <= 0) {
} catch (ZipException e) { throw new IOException("Download-Quota has been exceeded");
throw new IOException(String.format("%s: anti-leech limit has been reached", e.getMessage())); }
debug.finest(format("Download-Quota: %d", DOWNLOAD_QUOTA));
} }
// fully download // read and extract subtitle data
buffer.transferFully(stream); 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();
} }
} }