Load range only

This commit is contained in:
ByteHamster 2020-02-12 18:09:25 +01:00
parent 9a492fa406
commit ddfb4de9cf
1 changed files with 22 additions and 9 deletions

View File

@ -10,7 +10,9 @@ import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory; import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.MultiModelLoaderFactory; import com.bumptech.glide.load.model.MultiModelLoaderFactory;
import com.bumptech.glide.signature.ObjectKey; import com.bumptech.glide.signature.ObjectKey;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
import de.danoeh.antennapod.core.util.EmbeddedChapterImage; import de.danoeh.antennapod.core.util.EmbeddedChapterImage;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
@ -18,6 +20,9 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapterImage, ByteBuffer> { public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapterImage, ByteBuffer> {
@ -45,7 +50,7 @@ public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapte
return true; return true;
} }
class EmbeddedImageFetcher implements DataFetcher<ByteBuffer> { static class EmbeddedImageFetcher implements DataFetcher<ByteBuffer> {
private final EmbeddedChapterImage image; private final EmbeddedChapterImage image;
public EmbeddedImageFetcher(EmbeddedChapterImage image) { public EmbeddedImageFetcher(EmbeddedChapterImage image) {
@ -60,17 +65,25 @@ public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapte
if (image.getMedia().localFileAvailable()) { if (image.getMedia().localFileAvailable()) {
File localFile = new File(image.getMedia().getLocalMediaUrl()); File localFile = new File(image.getMedia().getLocalMediaUrl());
stream = new BufferedInputStream(new FileInputStream(localFile)); stream = new BufferedInputStream(new FileInputStream(localFile));
} else {
URL url = new URL(image.getMedia().getStreamUrl());
stream = new BufferedInputStream(url.openStream());
}
byte[] imageContent = new byte[image.getLength()];
stream.skip(image.getPosition()); stream.skip(image.getPosition());
byte[] imageContent = new byte[image.getLength()];
stream.read(imageContent, 0, image.getLength()); stream.read(imageContent, 0, image.getLength());
callback.onDataReady(ByteBuffer.wrap(imageContent)); callback.onDataReady(ByteBuffer.wrap(imageContent));
} else {
Request.Builder httpReq = new Request.Builder();
httpReq.header("User-Agent", ClientConfig.USER_AGENT);
// Skipping would download the whole file
httpReq.header("Range", "bytes=" + image.getPosition()
+ "-" + (image.getPosition() + image.getLength()));
httpReq.url(image.getMedia().getStreamUrl());
Response response = AntennapodHttpClient.getHttpClient().newCall(httpReq.build()).execute();
if (!response.isSuccessful() || response.body() == null) {
throw new IOException("Invalid response: " + response.code() + " " + response.message());
}
callback.onDataReady(ByteBuffer.wrap(response.body().bytes()));
}
} catch (IOException e) { } catch (IOException e) {
callback.onLoadFailed(new IOException("Loading embedded cover did not work")); callback.onLoadFailed(e);
e.printStackTrace();
} finally { } finally {
IOUtils.closeQuietly(stream); IOUtils.closeQuietly(stream);
} }