Skip to content

Commit

Permalink
fix image preview bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhwanng committed Nov 1, 2024
1 parent a8329ec commit 64a2328
Show file tree
Hide file tree
Showing 19 changed files with 249 additions and 153 deletions.
54 changes: 7 additions & 47 deletions app/src/main/java/com/seafile/seadroid2/context/NavContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,23 @@ public DirentModel getTopDirentModel() {
/**
* Get the parents model of the current Dirent, maybe RepoModel
*/
public boolean hasParentWritePermission() {
public boolean isParentHasWritePermission() {
if (!inRepo()) {
//repo list page should not have permission verification
throw new IllegalArgumentException("Please check your code");
}

if (inRepoRoot()) {
return hasWritePermissionWithRepo();
return getRepoModel().hasWritePermission();
}

BaseModel bd = navStack.elementAt(navStack.size() - 1);
DirentModel d = (DirentModel) bd;
if (d != null) {
return d.hasWritePermission();
if (d == null) {
return false;
}
return false;

return d.hasWritePermission();
}

public RepoModel getRepoModel() {
Expand All @@ -135,7 +137,6 @@ public BaseModel getTopModel() {
return navStack.peek();
}


/**
* @return /a/b/c/d/e/
*/
Expand All @@ -160,27 +161,6 @@ public String getNavPath() {

return fullPath;
}
//
// /**
// * /a/b/c/d/e.txt -> e.txt
// */
// public String getLastNameOfPath() {
// String fullPath = getNavPath();
// if (TextUtils.isEmpty(fullPath)) {
// return null;
// }
//
// if (!fullPath.contains("/")) {
// return fullPath;
// }
//
// String[] slash = fullPath.split("/");
// if (slash.length == 0) {
// return null;
// }
//
// return slash[slash.length - 1];
// }

/**
* /a/b/c -> c
Expand All @@ -201,24 +181,4 @@ public String getLastPathName() {

return null;
}

//
public boolean hasWritePermissionWithRepo() {

// BaseModel baseModel = getTopModel();
// if (baseModel == null) {
// return false;
// }
//
// if (baseModel instanceof RepoModel) {
// return ((RepoModel) baseModel).hasWritePermission();
// }
//
// if (baseModel instanceof DirentModel) {
// return ((DirentModel) baseModel).hasWritePermission();
// }

return getRepoModel().hasWritePermission();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public String getParent_path() {
public String repo_id;
public String repo_name;

//who
public String related_account;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public SafeOkHttpClient(Account account) {
super(account);
}

private TrustManager[] getTrustManagers() {
public static TrustManager[] getTrustManagers() {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

package com.seafile.seadroid2.framework.http;

import com.blankj.utilcode.util.CollectionUtils;
import com.seafile.seadroid2.account.Account;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.ConnectionSpec;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;

public class UnsafeOkHttpClient extends BaseOkHttpClient {
public UnsafeOkHttpClient() {
super(null);
}

public UnsafeOkHttpClient(Account account) {
super(account);
}

private final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
};

public OkHttpClient.Builder getBuilder() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();


try {
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
} catch (Exception e) {
e.printStackTrace();
}

builder.connectionSpecs(Arrays.asList(
ConnectionSpec.MODERN_TLS,
ConnectionSpec.COMPATIBLE_TLS,
ConnectionSpec.CLEARTEXT));
builder.cache(cache);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
//cache control
builder.interceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);
builder.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);

//add interceptors
List<Interceptor> interceptors = getInterceptors();
if (!CollectionUtils.isEmpty(interceptors)) {
for (Interceptor i : interceptors) {
builder.interceptors().add(i);
}
}

//timeout
builder.writeTimeout(DEFAULT_TIME_OUT, TimeUnit.MILLISECONDS);
builder.readTimeout(DEFAULT_TIME_OUT, TimeUnit.MILLISECONDS);
builder.connectTimeout(DEFAULT_TIME_OUT, TimeUnit.MILLISECONDS);

return builder;
}

@Override
public OkHttpClient getOkClient() {
OkHttpClient.Builder builder = getBuilder();
return builder.build();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.text.TextUtils;

import com.seafile.seadroid2.framework.util.SLogs;
import com.seafile.seadroid2.framework.util.TokenManager;

import java.io.IOException;
Expand All @@ -26,7 +27,6 @@ private Request.Builder initBuilder(Request.Builder builder) {
if (!TextUtils.isEmpty(authToken)) {
builder.addHeader("Authorization", "Token " + authToken);
}

return builder;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.seafile.seadroid2.framework.util;

import android.content.Context;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;

import androidx.annotation.NonNull;

Expand All @@ -14,13 +17,18 @@
import com.bumptech.glide.module.AppGlideModule;
import com.seafile.seadroid2.SeadroidApplication;
import com.seafile.seadroid2.framework.http.HttpIO;
import com.seafile.seadroid2.framework.http.UnsafeOkHttpClient;
import com.seafile.seadroid2.framework.http.interceptor.CurrentTokenInterceptor;
import com.seafile.seadroid2.framework.http.interceptor.HeaderInterceptor;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -57,6 +65,26 @@ public void registerComponents(@NonNull Context context, @NonNull Glide glide, @
}

private OkHttpClient getClient() {
return new OkHttpClient.Builder().addInterceptor(new CurrentTokenInterceptor()).build();
UnsafeOkHttpClient unsafeOkHttpClient = new UnsafeOkHttpClient();
OkHttpClient.Builder builder = unsafeOkHttpClient.getBuilder();
builder.followRedirects(true);
builder.addInterceptor(new CurrentTokenInterceptor());
// builder.addInterceptor(new Interceptor() {
// @Override
// public Response intercept(Chain chain) throws IOException {
// Request request = chain.request();
// String url = request.url().toString();
//
// String kie = CookieManager.getInstance().getCookie(URLs.getHost(url));
// Request.Builder requestBuilder = request.newBuilder();
// if (kie != null) {
// requestBuilder.addHeader("Cookie", kie);
// }
//
// Request newRequest = requestBuilder.build();
// return chain.proceed(newRequest);
// }
// });
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private static synchronized HashMap<String, Integer> getSuffixIconMap() {
suffixIconMap.put("markdown", R.drawable.icon_extended_md);
suffixIconMap.put("txt", R.drawable.icon_extended_txt);
suffixIconMap.put("png", R.drawable.icon_extended_png);
suffixIconMap.put("gif", R.drawable.icon_extended_png);
suffixIconMap.put("psd", R.drawable.icon_extended_psd);
suffixIconMap.put("ppt", R.drawable.icon_extended_ppt);
suffixIconMap.put("sdoc", R.drawable.icon_extended_sdoc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,6 @@ public void subscribe(SingleEmitter<List<DirentModel>> emitter) throws Exception
}).flatMap(new Function<List<DirentModel>, SingleSource<List<DirentModel>>>() {
@Override
public SingleSource<List<DirentModel>> apply(List<DirentModel> netModels) throws Exception {
if (CollectionUtils.isEmpty(netModels)) {
return Single.just(netModels);
}

Completable deleted = AppDatabase.getInstance().direntDao().deleteAllByParentPath(repoId, parentDir);
Single<Long> deleteAllByPathSingle = deleted.toSingleDefault(0L);
return deleteAllByPathSingle.flatMap(new Function<Long, SingleSource<List<DirentModel>>>() {
Expand All @@ -437,8 +433,7 @@ public SingleSource<List<DirentModel>> apply(List<DirentModel> direntModels) thr
return insertAllSingle.flatMap(new Function<Long, SingleSource<List<DirentModel>>>() {
@Override
public SingleSource<List<DirentModel>> apply(Long aLong) throws Exception {

SLogs.d("Dirents本地数据库已更新");
SLogs.d("The list has been inserted into the local database");
return Single.just(direntModels);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ public static String getFileNameFromPath(String path) {
return path.substring(path.lastIndexOf("/") + 1);
}

public static final String[] _units = new String[]{"B", "KB", "MB", "GB", "TB"};
public static final DecimalFormat _decimalFormat = new DecimalFormat("#,##0.#");

public static String readableFileSize(long size) {
if (size <= 0) return "0 KB";
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
int digitGroups = (int) (Math.log10(size) / Math.log10(1000));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1000, digitGroups)) + " " + units[digitGroups];
return _decimalFormat.format(size / Math.pow(1000, digitGroups)) + " " + _units[digitGroups];
}

public static boolean isViewableImage(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.seafile.seadroid2.account.Authenticator;
import com.seafile.seadroid2.account.SupportAccountManager;
import com.seafile.seadroid2.config.Constants;
import com.seafile.seadroid2.framework.http.HttpIO;
import com.seafile.seadroid2.framework.util.SLogs;
import com.seafile.seadroid2.ui.camera_upload.CameraUploadManager;

Expand Down Expand Up @@ -225,12 +226,6 @@ public void onActivityResult(ActivityResult o) {
private void finishLogin(Intent intent) {
SLogs.d(DEBUG_TAG, "finishLogin");

//firebase - event -login
Bundle eventBundle = new Bundle();
eventBundle.putString(FirebaseAnalytics.Param.METHOD, "finishLogin");
FirebaseAnalytics.getInstance(this).logEvent(FirebaseAnalytics.Event.LOGIN, eventBundle);


String newAccountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
String accountType = intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
String authToken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
Expand Down Expand Up @@ -299,7 +294,10 @@ private void finishLogin(Intent intent) {
SupportAccountManager.getInstance().setUserData(newAccount, Authenticator.KEY_SHIB, "shib");
}

//save current account
SupportAccountManager.getInstance().saveCurrentAccount(newAccountName);
//reset httpio instance
HttpIO.resetLoggedInInstance();

// set sync settings
ContentResolver.setIsSyncable(newAccount, CameraUploadManager.AUTHORITY, cameraIsSyncable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public SeafException getExceptionByThrowable(Throwable throwable) throws IOExcep
return SeafException.sslException;
}

return new SeafException(SeafException.CODE_ERROR, throwable.getMessage());
return new SeafException(SeafException.CODE_ERROR, throwable.getLocalizedMessage());
}

private SeafException checkResErrorBody(Response<?> resp) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private void onFileDownloadFailed(SeafException seafException) {
} else if (seafException == SeafException.invalidPassword) {
handlePassword();
} else {
ToastUtils.showLong(String.format("Failed to download file \"%s\"", direntModel.name));
ToastUtils.showLong(String.format("Failed to download file \"%s\"", seafException.getMessage()));

finishWithCancel();
}
Expand Down
Loading

0 comments on commit 64a2328

Please sign in to comment.