Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 74fd7d92 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

fix: Fix flood of user info requests at file upload

parent fb415141
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -71,6 +71,9 @@ public class EdriveApplication extends Application {

        FailedSyncPrefsManager.getInstance(getApplicationContext()).clearPreferences();

        prefs.edit().remove(AppConstants.ACCOUNT_QUOTA_LAST_CHECK).apply();
        prefs.edit().remove(AppConstants.ACCOUNT_QUOTA_REACHED).apply();

        setupEdriveRecovery();
    }

+40 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import static foundation.e.drive.utils.FileUtils.getMimeType;

import android.accounts.Account;
import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -40,6 +41,7 @@ import java.util.Set;

import foundation.e.drive.database.DbHelper;
import foundation.e.drive.models.SyncedFileState;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.DavClientProvider;
import timber.log.Timber;

@@ -51,6 +53,7 @@ import timber.log.Timber;
public class UploadFileOperation extends RemoteOperation {
    public final static int FILE_SIZE_FLOOR_FOR_CHUNKED = 3072000; //3MB
    private static final Set<ResultCode> handledFailureCodes = getHandledFailureCodes();
    private static final long ONE_MINUTE_IN_MS = 60 * 1000L;

    private final Context context;
    private final SyncedFileState syncedState;
@@ -233,12 +236,21 @@ public class UploadFileOperation extends RemoteOperation {
    @VisibleForTesting()
    @NonNull
    public RemoteOperationResult checkAvailableSpace(@NonNull NextcloudClient client, long fileSize) {
        if (!canReadUserInfo()) {
            return new RemoteOperationResult(ResultCode.QUOTA_EXCEEDED);
        }

        final RemoteOperationResult<UserInfo> ocsResult = readUserInfo(client);
        if (!ocsResult.isSuccess()) {
            return ocsResult;
        }

        final Quota quotas = ocsResult.getResultData().getQuota();
        boolean isQuotaReached = quotas != null && quotas.getRelative() >= 99.0;
        getSharedPreferences()
                .edit()
                .putBoolean(AppConstants.ACCOUNT_QUOTA_REACHED, isQuotaReached)
                .apply();

        if (quotas != null && quotas.getFree() < fileSize) {
            return new RemoteOperationResult(ResultCode.QUOTA_EXCEEDED);
@@ -257,6 +269,11 @@ public class UploadFileOperation extends RemoteOperation {
    @VisibleForTesting()
    @NonNull
    public RemoteOperationResult<UserInfo> readUserInfo(@NonNull NextcloudClient client) {
        getSharedPreferences()
                .edit()
                .putLong(AppConstants.ACCOUNT_QUOTA_LAST_CHECK, System.currentTimeMillis())
                .apply();

        final GetUserInfoRemoteOperation GetUserInfoRemoteOperation = new GetUserInfoRemoteOperation();
        return GetUserInfoRemoteOperation.execute(client);
    }
@@ -383,4 +400,27 @@ public class UploadFileOperation extends RemoteOperation {
    public long formatTimestampToMatchCloud(long timestamp) {
        return timestamp / 1000;
    }

    private boolean canReadUserInfo() {
        if (!getSharedPreferences().getBoolean(AppConstants.ACCOUNT_QUOTA_REACHED, false)) {
            return true;
        }

        final long currentTime = System.currentTimeMillis();
        final long previousTime = getSharedPreferences()
                .getLong(AppConstants.ACCOUNT_QUOTA_LAST_CHECK, 0);

        if (previousTime == 0) {
            return true;
        }

        return currentTime - previousTime > ONE_MINUTE_IN_MS;
    }

    private SharedPreferences getSharedPreferences() {
        return context.getSharedPreferences(
                AppConstants.SHARED_PREFERENCE_NAME,
                Context.MODE_PRIVATE
        );
    }
}
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ object AppConstants {
    const val ACCOUNT_DATA_USED_QUOTA_KEY = "used_quota"
    const val ACCOUNT_DATA_TOTAL_QUOTA_KEY = "total_quota"
    const val ACCOUNT_DATA_RELATIVE_QUOTA_KEY = "relative_quota"
    const val ACCOUNT_QUOTA_REACHED = "quota_reached"
    const val ACCOUNT_QUOTA_LAST_CHECK = "quota_last_check"
    const val ACCOUNT_DATA_GROUPS = "group"
    const val ACCOUNT_DATA_ALIAS_KEY = "alias"
    const val ACCOUNT_DATA_EMAIL = "email"