diff --git a/drpy/build.gradle b/drpy/build.gradle index 1c0c557fe..8617b370b 100644 --- a/drpy/build.gradle +++ b/drpy/build.gradle @@ -13,6 +13,7 @@ android { dependencies { implementation project(':catvod') + implementation project(':media-lib-common') implementation 'androidx.annotation:annotation:1.5.0' implementation 'wang.harlon.quickjs:wrapper-android:0.19.3' api 'com.google.code.gson:gson:' + gsonVersion diff --git a/drpy/src/main/java/com/hiker/drpy/Loader.java b/drpy/src/main/java/com/hiker/drpy/Loader.java index 20457affb..15d2a3f1b 100644 --- a/drpy/src/main/java/com/hiker/drpy/Loader.java +++ b/drpy/src/main/java/com/hiker/drpy/Loader.java @@ -2,6 +2,8 @@ package com.hiker.drpy; import android.content.Context; +import androidx.media3.common.util.UriUtil; + import com.whl.quickjs.android.QuickJSLoader; import com.whl.quickjs.wrapper.JSModule; @@ -18,7 +20,7 @@ public class Loader { JSModule.setModuleLoader(new JSModule.ModuleLoader() { @Override public String convertModuleName(String moduleBaseName, String moduleName) { - return Module.convertModuleName(moduleBaseName, moduleName); + return UriUtil.resolve(moduleBaseName, moduleName); } @Override diff --git a/drpy/src/main/java/com/hiker/drpy/Module.java b/drpy/src/main/java/com/hiker/drpy/Module.java index 7c2ba1ff5..de3dd5753 100644 --- a/drpy/src/main/java/com/hiker/drpy/Module.java +++ b/drpy/src/main/java/com/hiker/drpy/Module.java @@ -1,18 +1,22 @@ package com.hiker.drpy; import android.content.Context; +import android.net.Uri; import com.github.catvod.net.OkHttp; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; import java.util.concurrent.ConcurrentHashMap; import okhttp3.Headers; +import okhttp3.Response; public class Module { @@ -32,7 +36,7 @@ public class Module { public String load(Context context, String name) { if (cache.contains(name)) return cache.get(name); - if (name.startsWith("http")) cache.put(name, getModule(name)); + if (name.startsWith("http")) cache.put(name, getModule(context, name)); if (name.startsWith("assets")) cache.put(name, getAssets(context, name)); return cache.get(name); } @@ -50,74 +54,56 @@ public class Module { } } - private String getModule(String url) { + private String getModule(Context context, String url) { try { - return OkHttp.newCall(url, Headers.of("User-Agent", "Mozilla/5.0")).execute().body().string(); + File file = getFile(context, url); + if (file.exists()) return read(file); + Response response = OkHttp.newCall(url, Headers.of("User-Agent", "Mozilla/5.0")).execute(); + if (response.code() != 200) return ""; + byte[] data = response.body().bytes(); + new Thread(() -> write(file, data)).start(); + return new String(data, "UTF-8"); } catch (Exception e) { e.printStackTrace(); return ""; } } - private static boolean isRemote(String name) { - return name.startsWith("http://") || name.startsWith("https://") || name.startsWith("assets://"); + private File getFile(Context context, String url) { + return new File(context.getCacheDir(), Uri.parse(url).getLastPathSegment()); } - public static String convertModuleName(String moduleBaseName, String moduleName) { - if (moduleName == null || moduleName.length() == 0 || isRemote(moduleName)) { - return moduleName; - } - moduleName = moduleName.replace("//", "/"); - if (moduleName.startsWith("./")) { - moduleName = moduleName.substring(2); - } - if (moduleName.charAt(0) == '/') { - return moduleName; - } - if (moduleBaseName == null || moduleBaseName.length() == 0) { - return moduleName; - } - if (!isRemote(moduleBaseName)) { - moduleBaseName = moduleBaseName.replace("//", "/"); - } - if (moduleBaseName.startsWith("./")) { - moduleBaseName = moduleBaseName.substring(2); - } - if (moduleBaseName.equals("/")) { - return "/" + moduleName; - } - if (moduleBaseName.endsWith("/")) { - return moduleBaseName + moduleName; - } - String[] parentSplit = moduleBaseName.split("/"); - String[] pathSplit = moduleName.split("/"); - List parentStack = new ArrayList<>(); - List pathStack = new ArrayList<>(); - Collections.addAll(parentStack, parentSplit); - Collections.addAll(pathStack, pathSplit); - while (!pathStack.isEmpty()) { - String tmp = pathStack.get(0); - if (tmp.equals("..")) { - pathStack.remove(0); - parentStack.remove(parentStack.size() - 1); - } else { - break; - } - } - if (!parentStack.isEmpty()) { - parentStack.remove(parentStack.size() - 1); - } - StringBuilder builder = new StringBuilder(); - if (moduleBaseName.startsWith("/")) { - builder.append("/"); + private void write(File file, byte[] data) { + try { + FileOutputStream fos = new FileOutputStream(file); + fos.write(data); + fos.flush(); + fos.close(); + chmod(file); + } catch (Exception e) { + file.delete(); } - for (String it : parentStack) { - builder.append(it).append("/"); + } + + private String read(File file) { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); + StringBuilder sb = new StringBuilder(); + String text; + while ((text = br.readLine()) != null) sb.append(text).append("\n"); + br.close(); + return sb.toString(); + } catch (Exception e) { + return ""; } - for (String it : pathStack) { - builder.append(it).append("/"); + } + + private void chmod(File file) { + try { + Process process = Runtime.getRuntime().exec("chmod 777 " + file); + process.waitFor(); + } catch (Exception e) { + e.printStackTrace(); } - builder.deleteCharAt(builder.length() - 1); - return builder.toString(); } }