Skip to content
This repository has been archived by the owner on Jul 21, 2024. It is now read-only.

Commit

Permalink
🐛Fixed static frontend work dir, cause 404 issues on some device
Browse files Browse the repository at this point in the history
  • Loading branch information
eritpchy committed Jan 8, 2024
1 parent aaa81ba commit e113e45
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 15 deletions.
1 change: 1 addition & 0 deletions aliyundrive-android-core/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
}
-keep class net.sf.webdav.WebdavServlet
-keep class net.xdow.aliyundrive.servlet.WebdavServletInit
-keep class net.xdow.aliyundrive.servlet.FrontendServlet
-keep class com.github.zxbu.webdavteambition.store.AliYunDriverFileSystemStore
-keep class org.eclipse.jetty.servlets.CrossOriginFilter
-keep class net.xdow.aliyundrive.filter.ErrorFilter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.xdow.aliyundrive.servlet;

import com.github.zxbu.webdavteambition.config.AliyunDriveProperties;
import net.xdow.aliyundrive.util.AliyunDriveClientServiceHolder;
import org.eclipse.jetty.servlet.DefaultServlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.util.*;

public class FrontendServlet extends DefaultServlet {

Map<String, String> mInitParameterMap = new HashMap<>();

@Override
public void init(ServletConfig config) throws ServletException {
AliyunDriveProperties properties = AliyunDriveClientServiceHolder.getInstance().getProperties();
mInitParameterMap.put("resourceBase", properties.getFrontendDir().getAbsolutePath());
mInitParameterMap.put("dirAllowed", "true");
mInitParameterMap.put("pathInfoOnly", "true");
super.init(config);
}

@Override
public String getInitParameter(String name) {
String val = mInitParameterMap.get(name);
if (val != null) {
return val;
}
return super.getInitParameter(name);
}

@Override
public Enumeration<String> getInitParameterNames() {
Collection<String> names = new ArrayList<>();
names.addAll(mInitParameterMap.keySet());
Enumeration<String> origNames = super.getInitParameterNames();
while (origNames.hasMoreElements()) {
names.add(origNames.nextElement());
}
return Collections.enumeration(names);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import net.xdow.aliyundrive.event.AliyunDriveAccessTokenInvalidEvent;
import net.xdow.aliyundrive.impl.AliyunDriveOpenApiImplV1;
import net.xdow.aliyundrive.webapi.impl.AliyunDriveWebApiImplV1;
import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.webapp.WebAppContext;
import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

public class AliyunDriveClientServiceHolder {
private static AliyunDriveClientService sAliyunDriveClientService;
Expand All @@ -28,7 +32,8 @@ public static AliyunDriveClientService getInstance() {
String deviceId = sharedPreferences.getString(context.getString(R.string.config_device_id),"");
String shareToken = sharedPreferences.getString(context.getString(R.string.config_share_token),"");
String downloadProxyMode = sharedPreferences.getString(context.getString(R.string.config_download_proxy_mode),"Auto");
final AliyunDriveProperties properties = AliyunDriveProperties.load(context.getFilesDir().getAbsolutePath() + File.separator);
String workDir = ensureWorkDir(context).getAbsolutePath() + File.separator;
final AliyunDriveProperties properties = AliyunDriveProperties.load(workDir);
properties.setAuthorization(null);
if (!refreshToken.equals(properties.getRefreshTokenNext())) {
properties.setRefreshToken(refreshToken);
Expand All @@ -37,7 +42,7 @@ public static AliyunDriveClientService getInstance() {
properties.setDeviceId(deviceId);
properties.setShareToken(shareToken);
properties.setDownloadProxyMode(AliyunDriveProperties.DownloadProxyMode.valueOf(downloadProxyMode));
properties.setWorkDir(context.getFilesDir().getAbsolutePath() + File.separator);
properties.setWorkDir(workDir);
properties.save();
boolean useAliyunDriveOpenApi = Boolean.parseBoolean(String.valueOf(webContext.getAttribute(context.getString(R.string.config_use_aliyun_drive_openapi))));
if (useAliyunDriveOpenApi) {
Expand All @@ -58,6 +63,35 @@ public static AliyunDriveClientService getInstance() {
return sAliyunDriveClientService;
}

private static File ensureWorkDir(Context context) {
List<Callable<File>> workDirCallList = new ArrayList<>();
workDirCallList.add(context::getFilesDir);
workDirCallList.add(() -> context.getExternalFilesDir(""));
workDirCallList.add(() -> new File("/data/user_de/0/" + context.getPackageName() + "/files"));
for (Callable<File> caller: workDirCallList) {
try {
File dir = caller.call();
if (mkdirs(dir)) {
return dir;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return context.getFilesDir();
}

private static boolean mkdirs(File dir) {
if (!dir.exists()) {
try {
FileUtils.forceMkdir(dir);
} catch (Exception e) {
e.printStackTrace();
}
}
return dir.exists() && dir.isDirectory();
}

public static void onShutdown() {
synchronized (AliyunDriveClientServiceHolder.class) {
sAliyunDriveClientService = null;
Expand Down
14 changes: 1 addition & 13 deletions aliyundrive-android-core/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,7 @@
</login-config>
<servlet>
<servlet-name>DefaultServlet</servlet-name>
<servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
<init-param>
<param-name>resourceBase</param-name>
<param-value>/data/data/net.xdow.webdavaliyundriver/files/web</param-value>
</init-param>
<init-param>
<param-name>dirAllowed</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>pathInfoOnly</param-name>
<param-value>true</param-value>
</init-param>
<servlet-class>net.xdow.aliyundrive.servlet.FrontendServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DefaultServlet</servlet-name>
Expand Down

0 comments on commit e113e45

Please sign in to comment.