Fix loading chapter images in local feeds (#7016)

This commit is contained in:
ByteHamster 2024-03-22 22:12:36 +01:00 committed by GitHub
parent 69f0daa2e8
commit 376c83d200
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 12 deletions

View File

@ -49,6 +49,6 @@ public class ApGlideModule extends AppGlideModule {
registry.append(String.class, InputStream.class, new ApOkHttpUrlLoader.Factory());
registry.append(String.class, InputStream.class, new NoHttpStringLoader.StreamFactory());
registry.append(EmbeddedChapterImage.class, ByteBuffer.class, new ChapterImageModelLoader.Factory());
registry.append(EmbeddedChapterImage.class, ByteBuffer.class, new ChapterImageModelLoader.Factory(context));
}
}

View File

@ -1,5 +1,8 @@
package de.danoeh.antennapod.ui.glide;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import androidx.annotation.NonNull;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
@ -22,12 +25,17 @@ import okhttp3.Response;
import org.apache.commons.io.IOUtils;
public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapterImage, ByteBuffer> {
public static class Factory implements ModelLoaderFactory<EmbeddedChapterImage, ByteBuffer> {
private final Context context;
public Factory(Context context) {
this.context = context;
}
@NonNull
@Override
public ModelLoader<EmbeddedChapterImage, ByteBuffer> build(@NonNull MultiModelLoaderFactory unused) {
return new ChapterImageModelLoader();
return new ChapterImageModelLoader(context);
}
@Override
@ -36,12 +44,16 @@ public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapte
}
}
private final Context context;
public ChapterImageModelLoader(Context context) {
this.context = context;
}
@Override
public LoadData<ByteBuffer> buildLoadData(@NonNull EmbeddedChapterImage model,
int width,
int height,
@NonNull Options options) {
return new LoadData<>(new ObjectKey(model), new EmbeddedImageFetcher(model));
public LoadData<ByteBuffer> buildLoadData(@NonNull EmbeddedChapterImage model, int width,
int height, @NonNull Options options) {
return new LoadData<>(new ObjectKey(model), new EmbeddedImageFetcher(model, context));
}
@Override
@ -51,9 +63,11 @@ public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapte
static class EmbeddedImageFetcher implements DataFetcher<ByteBuffer> {
private final EmbeddedChapterImage image;
private final Context context;
public EmbeddedImageFetcher(EmbeddedChapterImage image) {
public EmbeddedImageFetcher(EmbeddedChapterImage image, Context context) {
this.image = image;
this.context = context;
}
@Override
@ -61,9 +75,15 @@ public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapte
BufferedInputStream stream = null;
try {
if (image.getMedia().localFileAvailable()) {
File localFile = new File(image.getMedia().getLocalMediaUrl());
stream = new BufferedInputStream(new FileInputStream(localFile));
boolean isLocalFeed = image.getMedia().getStreamUrl().startsWith(ContentResolver.SCHEME_CONTENT);
if (isLocalFeed || image.getMedia().localFileAvailable()) {
if (isLocalFeed) {
Uri uri = Uri.parse(image.getMedia().getStreamUrl());
stream = new BufferedInputStream(context.getContentResolver().openInputStream(uri));
} else {
File localFile = new File(image.getMedia().getLocalMediaUrl());
stream = new BufferedInputStream(new FileInputStream(localFile));
}
IOUtils.skip(stream, image.getPosition());
byte[] imageContent = new byte[image.getLength()];
IOUtils.read(stream, imageContent, 0, image.getLength());