feat:Add Base64Resource

This commit is contained in:
pengfei.zhou
2021-10-22 17:00:00 +08:00
committed by osborn
parent 5501dd38d9
commit 6402522946
16 changed files with 364 additions and 51 deletions

View File

@@ -43,6 +43,7 @@ import pub.doric.plugin.StoragePlugin;
import pub.doric.refresh.RefreshableNode;
import pub.doric.resource.DoricAssetsLoader;
import pub.doric.resource.DoricAndroidLoader;
import pub.doric.resource.DoricBase64Loader;
import pub.doric.resource.DoricLocalLoader;
import pub.doric.resource.DoricRemoteLoader;
import pub.doric.resource.DoricResourceManager;
@@ -134,6 +135,7 @@ public class DoricRegistry {
this.getResourceManager().registerLoader(new DoricAssetsLoader());
this.getResourceManager().registerLoader(new DoricLocalLoader());
this.getResourceManager().registerLoader(new DoricRemoteLoader());
this.getResourceManager().registerLoader(new DoricBase64Loader());
initRegistry(this);
doricJSEngine.setEnvironmentValue(DoricSingleton.getInstance().envMap);
DoricSingleton.getInstance().registries.add(new WeakReference<>(this));

View File

@@ -0,0 +1,35 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import pub.doric.DoricContext;
/**
* @Description: This loads base64 resource
* @Author: pengfei.zhou
* @CreateDate: 2021/10/22
*/
public class DoricBase64Loader implements DoricResourceLoader {
@Override
public String resourceType() {
return "base64";
}
@Override
public DoricResource load(DoricContext doricContext, String identifier) {
return new DoricBase64Resource(doricContext, identifier);
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.resource;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Pair;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
import pub.doric.utils.DoricUtils;
/**
* @Description: This represents a base64 resource
* @Author: pengfei.zhou
* @CreateDate: 2021/10/22
*/
class DoricBase64Resource extends DoricResource {
private final String identifier;
public DoricBase64Resource(DoricContext doricContext, String identifier) {
super(doricContext);
this.identifier = identifier;
}
@Override
public AsyncResult<InputStream> asInputStream() {
AsyncResult<InputStream> ret = new AsyncResult<>();
Pair<String, String> result = DoricUtils.translateBase64(identifier);
if (result != null) {
String imageType = result.first;
String base64 = result.second;
if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) {
try {
byte[] data = Base64.decode(base64, Base64.DEFAULT);
ret.setResult(new ByteArrayInputStream(data));
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
ret.setError(new Error("Base64 format error"));
}
return ret;
}
}

View File

@@ -374,7 +374,19 @@ public class ImageNode extends ViewNode<ImageView> {
doricResource.asInputStream().setCallback(new AsyncResult.Callback<InputStream>() {
@Override
public void onResult(InputStream result) {
loadIntoTarget(Glide.with(getContext()).load(result));
try {
byte[] imageData = new byte[result.available()];
result.read(imageData, 0, result.available());
loadIntoTarget(Glide.with(getContext()).load(imageData));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
result.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override