From 512bf3465cf2eaa68323398c6e2311f6405b98e8 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Fri, 8 Sep 2023 08:37:11 +0200 Subject: [PATCH] Get localId directly from CreateFolderRemoteOperation Signed-off-by: tobiasKaminsky --- .../java/com/owncloud/android/AbstractIT.java | 4 +-- ...ava => CreateFolderRemoteOperationIT.java} | 33 +++++++++++++---- .../files/ReadFileRemoteOperationIT.kt | 4 +++ .../files/CreateFolderRemoteOperation.java | 35 +++++++++++++------ 4 files changed, 56 insertions(+), 20 deletions(-) rename library/src/androidTest/java/com/owncloud/android/{CreateFolderIT.java => CreateFolderRemoteOperationIT.java} (79%) diff --git a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java index f8c2d6141..83a4869d9 100644 --- a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -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) { diff --git a/library/src/androidTest/java/com/owncloud/android/CreateFolderIT.java b/library/src/androidTest/java/com/owncloud/android/CreateFolderRemoteOperationIT.java similarity index 79% rename from library/src/androidTest/java/com/owncloud/android/CreateFolderIT.java rename to library/src/androidTest/java/com/owncloud/android/CreateFolderRemoteOperationIT.java index 958136ad6..6f6c42446 100644 --- a/library/src/androidTest/java/com/owncloud/android/CreateFolderIT.java +++ b/library/src/androidTest/java/com/owncloud/android/CreateFolderRemoteOperationIT.java @@ -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; @@ -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 mCreatedFolderPaths; + private final List mCreatedFolderPaths; private String mFullPath2FolderBase; - public CreateFolderIT() { + public CreateFolderRemoteOperationIT() { super(); mCreatedFolderPaths = new ArrayList<>(); } @@ -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 result = new CreateFolderRemoteOperation(remotePath, true).execute(client); assertTrue(result.isSuccess()); // Create Subfolder @@ -80,6 +85,20 @@ public void testCreateFolder() { assertTrue(result.isSuccess()); } + @Test + public void testFileID() { + String remotePath = mFullPath2FolderBase + "/" + RandomStringGenerator.make(TAG_LENGTH); + mCreatedFolderPaths.add(remotePath); + RemoteOperationResult 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 @@ -87,7 +106,7 @@ public void testCreateFolder() { @Test public void testCreateFolderSpecialCharactersOnNewVersion() { String remotePath = mFullPath2FolderBase + "_<"; - RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client); + RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client); assertTrue("Remote path: " + remotePath, result.isSuccess()); remotePath = mFullPath2FolderBase + "_>"; diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/ReadFileRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/ReadFileRemoteOperationIT.kt index e95232497..72219a56a 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/ReadFileRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/ReadFileRemoteOperationIT.kt @@ -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 @@ -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) } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/CreateFolderRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/CreateFolderRemoteOperation.java index 48218fbd8..975c8a59c 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/CreateFolderRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/CreateFolderRemoteOperation.java @@ -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; @@ -41,7 +42,7 @@ * @author David A. Velasco * @author masensio */ -public class CreateFolderRemoteOperation extends RemoteOperation { +public class CreateFolderRemoteOperation extends RemoteOperation { private static final String TAG = CreateFolderRemoteOperation.class.getSimpleName(); @@ -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 run(OwnCloudClient client) { + RemoteOperationResult result; result = createFolder(client); if (!result.isSuccess() && createFullPath && @@ -93,8 +94,8 @@ protected RemoteOperationResult run(OwnCloudClient client) { } - private RemoteOperationResult createFolder(OwnCloudClient client) { - RemoteOperationResult result; + private RemoteOperationResult createFolder(OwnCloudClient client) { + RemoteOperationResult result; MkColMethod mkCol = null; try { mkCol = new MkColMethod(client.getFilesDavUri(remotePath)); @@ -102,19 +103,31 @@ private RemoteOperationResult createFolder(OwnCloudClient client) { 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 { @@ -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 createParentFolder(String parentPath, OwnCloudClient client) { + RemoteOperation operation = new CreateFolderRemoteOperation(parentPath, createFullPath); return operation.execute(client); }