Skip to content

Commit

Permalink
Merge pull request #1193 from nextcloud/createFolderId
Browse files Browse the repository at this point in the history
Get remoteId directly from CreateFolderRemoteOperation
  • Loading branch information
tobiasKaminsky authored Sep 25, 2023
2 parents 8afe859 + 512bf34 commit 1d0a747
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ public static File extractAsset(String fileName, Context context) throws IOExcep

@After
public void after() {
// removeOnClient(client);
// removeOnClient(client2);
removeOnClient(client);
removeOnClient(client2);
}

private void removeOnClient(OwnCloudClient client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@
*/
package com.owncloud.android;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.nextcloud.test.RandomStringGenerator;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation;
import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
import com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation;
import com.owncloud.android.lib.resources.files.model.RemoteFile;

import org.junit.After;
import org.junit.Before;
Expand All @@ -39,18 +45,17 @@
import java.util.Iterator;
import java.util.List;

import static org.junit.Assert.assertTrue;

/**
* Class to test Create Folder Operation
*/
public class CreateFolderIT extends AbstractIT {
public class CreateFolderRemoteOperationIT extends AbstractIT {
private static final String FOLDER_PATH_BASE = "/testCreateFolder";
private static final int TAG_LENGTH = 10;

private List<String> mCreatedFolderPaths;
private final List<String> mCreatedFolderPaths;
private String mFullPath2FolderBase;

public CreateFolderIT() {
public CreateFolderRemoteOperationIT() {
super();
mCreatedFolderPaths = new ArrayList<>();
}
Expand All @@ -70,7 +75,7 @@ public void setUp() {
public void testCreateFolder() {
String remotePath = mFullPath2FolderBase;
mCreatedFolderPaths.add(remotePath);
RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
RemoteOperationResult<Long> result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
assertTrue(result.isSuccess());

// Create Subfolder
Expand All @@ -80,14 +85,28 @@ public void testCreateFolder() {
assertTrue(result.isSuccess());
}

@Test
public void testFileID() {
String remotePath = mFullPath2FolderBase + "/" + RandomStringGenerator.make(TAG_LENGTH);
mCreatedFolderPaths.add(remotePath);
RemoteOperationResult<Long> result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
assertTrue(result.isSuccess());

RemoteOperationResult readResult = new ReadFileRemoteOperation(remotePath).execute(client);
assertTrue(readResult.isSuccess());

Long remoteId = ((RemoteFile) readResult.getData().get(0)).getLocalId();
assertEquals(result.getResultData(), remoteId);
}

/**
* Test to create folder with special characters: / \ < > : " | ?
* > oc8.1 no characters are forbidden
*/
@Test
public void testCreateFolderSpecialCharactersOnNewVersion() {
String remotePath = mFullPath2FolderBase + "_<";
RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
RemoteOperationResult<Long> result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
assertTrue("Remote path: " + remotePath, result.isSuccess());

remotePath = mFullPath2FolderBase + "_>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation
import com.owncloud.android.lib.resources.files.model.GeoLocation
import com.owncloud.android.lib.resources.files.model.ImageDimension
import com.owncloud.android.lib.resources.files.model.RemoteFile
import com.owncloud.android.lib.resources.status.NextcloudVersion
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
Expand Down Expand Up @@ -77,6 +78,9 @@ class ReadFileRemoteOperationIT : AbstractIT() {

@Suppress("Detekt.MagicNumber")
assertEquals(ImageDimension(451f, 529f), remoteFile.imageDimension)

testOnlyOnServer(NextcloudVersion.nextcloud_27)

@Suppress("Detekt.MagicNumber")
assertEquals(GeoLocation(49.99679166666667, 8.67198611111111), remoteFile.geoLocation)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;

Expand All @@ -41,7 +42,7 @@
* @author David A. Velasco
* @author masensio
*/
public class CreateFolderRemoteOperation extends RemoteOperation {
public class CreateFolderRemoteOperation extends RemoteOperation<Long> {

private static final String TAG = CreateFolderRemoteOperation.class.getSimpleName();

Expand Down Expand Up @@ -76,8 +77,8 @@ public CreateFolderRemoteOperation(String remotePath, boolean createFullPath, St
* @param client Client object to communicate with the remote ownCloud server.
*/
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
protected RemoteOperationResult<Long> run(OwnCloudClient client) {
RemoteOperationResult<Long> result;

result = createFolder(client);
if (!result.isSuccess() && createFullPath &&
Expand All @@ -93,28 +94,40 @@ protected RemoteOperationResult run(OwnCloudClient client) {
}


private RemoteOperationResult createFolder(OwnCloudClient client) {
RemoteOperationResult result;
private RemoteOperationResult<Long> createFolder(OwnCloudClient client) {
RemoteOperationResult<Long> result;
MkColMethod mkCol = null;
try {
mkCol = new MkColMethod(client.getFilesDavUri(remotePath));

if (!TextUtils.isEmpty(token)) {
mkCol.addRequestHeader(E2E_TOKEN, token);
}

client.executeMethod(mkCol, READ_TIMEOUT, CONNECTION_TIMEOUT);

if (HttpStatus.SC_METHOD_NOT_ALLOWED == mkCol.getStatusCode()) {
result = new RemoteOperationResult(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS);
result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS);
} else {
result = new RemoteOperationResult(mkCol.succeeded(), mkCol);
result = new RemoteOperationResult<>(mkCol.succeeded(), mkCol);
Header fileIdHeader = mkCol.getResponseHeader("OC-FileId");
Header cookieHeader = mkCol.getResponseHeader("Set-Cookie");

if (fileIdHeader != null && cookieHeader != null) {
String instanceId = cookieHeader.getValue().split("=")[0];
String fileId = fileIdHeader.getValue();
String id = fileId.replace(instanceId, "");

result.setResultData(Long.valueOf(id));
} else {
result.setResultData(null);
}
}

Log_OC.d(TAG, "Create directory " + remotePath + ": " + result.getLogMessage());
client.exhaustResponse(mkCol.getResponseBodyAsStream());
} catch (Exception e) {
result = new RemoteOperationResult(e);
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Create directory " + remotePath + ": " + result.getLogMessage(), e);

} finally {
Expand All @@ -124,8 +137,8 @@ private RemoteOperationResult createFolder(OwnCloudClient client) {
return result;
}

private RemoteOperationResult createParentFolder(String parentPath, OwnCloudClient client) {
RemoteOperation operation = new CreateFolderRemoteOperation(parentPath, createFullPath);
private RemoteOperationResult<Long> createParentFolder(String parentPath, OwnCloudClient client) {
RemoteOperation<Long> operation = new CreateFolderRemoteOperation(parentPath, createFullPath);
return operation.execute(client);
}

Expand Down

0 comments on commit 1d0a747

Please sign in to comment.