Skip to content

Commit

Permalink
Rename functions
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed May 2, 2024
1 parent bb36e32 commit 82af5b4
Showing 1 changed file with 43 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -98,7 +99,6 @@
import javax.crypto.NoSuchPaddingException;

import androidx.annotation.CheckResult;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import kotlin.Triple;
Expand Down Expand Up @@ -471,7 +471,7 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
}

long counter = getE2ECounter(parentFile);
token = getToken(client, parentFile, counter);
token = getFolderUnlockTokenOrLockFolder(client, parentFile, counter);

// Update metadata
EncryptionUtilsV2 encryptionUtilsV2 = new EncryptionUtilsV2();
Expand All @@ -480,7 +480,7 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
metadataExists = true;
}

if (CapabilityUtils.getCapability(mContext).getEndToEndEncryptionApiVersion().compareTo(E2EVersion.V2_0) >= 0) {
if (isEndToEndVersionAtLeastV2()) {
if (object == null) {
return new RemoteOperationResult(new IllegalStateException("Metadata does not exist"));
}
Expand All @@ -490,7 +490,7 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare

E2EClientData clientData = new E2EClientData(client, token, publicKey);

List<String> fileNames = checkNameCollision(object);
List<String> fileNames = getCollidedFileNames(object);

RemoteOperationResult collisionResult = checkNameCollision(client, fileNames, parentFile.isEncrypted());
if (collisionResult != null) {
Expand Down Expand Up @@ -524,11 +524,11 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
result = channelResult.getSecond();
FileChannel channel = channelResult.getThird();

size = initSize(channel);
size = getChannelSize(channel);
updateSize(size);
setUploadFileRemoteOperationForE2E(token, e2eFiles.getEncryptedTempFile(), e2eData.getEncryptedFileName(), lastModifiedTimestamp, creationTimestamp, size);
setUploadOperationForE2E(token, e2eFiles.getEncryptedTempFile(), e2eData.getEncryptedFileName(), lastModifiedTimestamp, creationTimestamp, size);

result = performE2EUpload(result, clientData);
result = performE2EUpload(clientData);

if (result.isSuccess()) {
updateMetadataForE2E(object, e2eData, clientData, e2eFiles, arbitraryDataProvider, encryptionUtilsV2, metadataExists);
Expand All @@ -550,28 +550,36 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
return result;
}

private String getToken(OwnCloudClient client, OCFile parentFile, long counter) throws UploadException {
String token;
if (mFolderUnlockToken != null && !mFolderUnlockToken.isEmpty()) {
token = mFolderUnlockToken;
} else {
token = EncryptionUtils.lockFolder(parentFile, client, counter);
mUpload.setFolderUnlockToken(token);
uploadsStorageManager.updateUpload(mUpload);
}
private boolean isEndToEndVersionAtLeastV2() {
return getE2EVersion().compareTo(E2EVersion.V2_0) >= 0;
}

return token;
private E2EVersion getE2EVersion() {
return CapabilityUtils.getCapability(mContext).getEndToEndEncryptionApiVersion();
}

private long getE2ECounter(OCFile parentFile) {
long counter = -1;
if (CapabilityUtils.getCapability(mContext).getEndToEndEncryptionApiVersion().compareTo(E2EVersion.V2_0) >= 0) {

if (isEndToEndVersionAtLeastV2()) {
counter = parentFile.getE2eCounter() + 1;
}

return counter;
}

private String getFolderUnlockTokenOrLockFolder(OwnCloudClient client, OCFile parentFile, long counter) throws UploadException {
if (mFolderUnlockToken != null && !mFolderUnlockToken.isEmpty()) {
return mFolderUnlockToken;
}

String token = EncryptionUtils.lockFolder(parentFile, client, counter);
mUpload.setFolderUnlockToken(token);
uploadsStorageManager.updateUpload(mUpload);

return token;
}

private DecryptedFolderMetadataFileV1 getDecryptedFolderMetadataV1(String publicKey, Object object)
throws NoSuchPaddingException, IllegalBlockSizeException, CertificateException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {

Expand All @@ -590,21 +598,21 @@ private DecryptedFolderMetadataFileV1 getDecryptedFolderMetadataV1(String public
return metadata;
}

private List<String> checkNameCollision(Object object) {
List<String> fileNames = new ArrayList<>();
private List<String> getCollidedFileNames(Object object) {
List<String> result = new ArrayList<>();

if (object instanceof DecryptedFolderMetadataFileV1 metadata) {
for (DecryptedFile file : metadata.getFiles().values()) {
fileNames.add(file.getEncrypted().getFilename());
result.add(file.getEncrypted().getFilename());
}
} else {
for (com.owncloud.android.datamodel.e2e.v2.decrypted.DecryptedFile file :
((DecryptedFolderMetadataFile) object).getMetadata().getFiles().values()) {
fileNames.add(file.getFilename());
} else if (object instanceof DecryptedFolderMetadataFile metadataFile) {
Map<String, com.owncloud.android.datamodel.e2e.v2.decrypted.DecryptedFile> files = metadataFile.getMetadata().getFiles();
for (com.owncloud.android.datamodel.e2e.v2.decrypted.DecryptedFile file : files.values()) {
result.add(file.getFilename());
}
}

return fileNames;
return result;
}

private String getEncryptedFileName(Object object) {
Expand All @@ -623,12 +631,12 @@ private String getEncryptedFileName(Object object) {
return encryptedFileName;
}

private void setUploadFileRemoteOperationForE2E(String token,
File encryptedTempFile,
String encryptedFileName,
long lastModifiedTimestamp,
long creationTimestamp,
long size) {
private void setUploadOperationForE2E(String token,
File encryptedTempFile,
String encryptedFileName,
long lastModifiedTimestamp,
long creationTimestamp,
long size) {

if (size > ChunkedFileUploadRemoteOperation.CHUNK_SIZE_MOBILE) {
boolean onWifiConnection = connectivityService.getConnectivity().isWifi();
Expand Down Expand Up @@ -696,15 +704,15 @@ private Triple<FileLock, RemoteOperationResult, FileChannel> initFileChannel(Rem
return new Triple<>(fileLock, result, channel);
}

private long initSize(FileChannel channel) {
private long getChannelSize(FileChannel channel) {
try {
return channel.size();
} catch (IOException e1) {
return new File(mFile.getStoragePath()).length();
}
}

private RemoteOperationResult performE2EUpload( RemoteOperationResult result, E2EClientData data) throws OperationCancelledException {
private RemoteOperationResult performE2EUpload(E2EClientData data) throws OperationCancelledException {
for (OnDatatransferProgressListener mDataTransferListener : mDataTransferListeners) {
mUploadOperation.addDataTransferProgressListener(mDataTransferListener);
}
Expand All @@ -713,7 +721,7 @@ private RemoteOperationResult performE2EUpload( RemoteOperationResult result, E2
throw new OperationCancelledException();
}

result = mUploadOperation.execute(data.getClient());
RemoteOperationResult result = mUploadOperation.execute(data.getClient());

/// move local temporal file or original file to its corresponding
// location in the Nextcloud local folder
Expand Down

0 comments on commit 82af5b4

Please sign in to comment.