android: remoteResource support headers setting

This commit is contained in:
pengfei.zhou 2023-09-22 14:25:05 +08:00 committed by jingpeng
parent fd1e626bf2
commit 67882af2f6
3 changed files with 29 additions and 4 deletions

File diff suppressed because one or more lines are too long

View File

@ -19,16 +19,18 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.Key; import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.signature.ObjectKey;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -42,6 +44,7 @@ import pub.doric.async.AsyncResult;
*/ */
public class DoricRemoteResource extends DoricResource { public class DoricRemoteResource extends DoricResource {
private final boolean needCache; private final boolean needCache;
private final Map<String, String> headers = new HashMap<>();
public DoricRemoteResource(DoricContext doricContext, String identifier, boolean needCache) { public DoricRemoteResource(DoricContext doricContext, String identifier, boolean needCache) {
super(doricContext, identifier); super(doricContext, identifier);
@ -52,11 +55,19 @@ public class DoricRemoteResource extends DoricResource {
this(doricContext, identifier, true); this(doricContext, identifier, true);
} }
public void setHeader(String k, String v) {
this.headers.put(k, v);
}
@Override @Override
public AsyncResult<byte[]> fetchRaw() { public AsyncResult<byte[]> fetchRaw() {
final AsyncResult<byte[]> result = new AsyncResult<>(); final AsyncResult<byte[]> result = new AsyncResult<>();
LazyHeaders.Builder builder = new LazyHeaders.Builder();
for (Map.Entry<String, String> entry : headers.entrySet()) {
builder.addHeader(entry.getKey(), entry.getValue());
}
RequestBuilder<File> requestBuilder = Glide.with(doricContext.getContext()) RequestBuilder<File> requestBuilder = Glide.with(doricContext.getContext())
.download(identifier); .download(new GlideUrl(identifier, builder.build()));
if (!this.needCache) { if (!this.needCache) {
requestBuilder = requestBuilder.skipMemoryCache(true).signature(new Key() { requestBuilder = requestBuilder.skipMemoryCache(true).signature(new Key() {
@Override @Override

View File

@ -17,6 +17,7 @@ package pub.doric.resource;
import com.github.pengfeizhou.jscore.JSArrayBuffer; import com.github.pengfeizhou.jscore.JSArrayBuffer;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -57,6 +58,15 @@ public class DoricResourceManager {
if (doricResource instanceof DoricArrayBufferResource) { if (doricResource instanceof DoricArrayBufferResource) {
JSArrayBuffer buffer = resource.getProperty("data").asArrayBuffer(); JSArrayBuffer buffer = resource.getProperty("data").asArrayBuffer();
((DoricArrayBufferResource) doricResource).setValue(buffer); ((DoricArrayBufferResource) doricResource).setValue(buffer);
} else if (doricResource instanceof DoricRemoteResource) {
JSValue headers = resource.getProperty("headers");
if (headers.isObject()) {
JSObject headersObject = headers.asObject();
for (Map.Entry<String, JSValue> entry : headersObject.value().entrySet()) {
((DoricRemoteResource) doricResource).setHeader(entry.getKey(),
entry.getValue().asString().value());
}
}
} }
doricContext.cacheResource(resId, doricResource); doricContext.cacheResource(resId, doricResource);
} }