Skip to content

Commit fd06ee2

Browse files
Add direct endpoint api
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
1 parent 6a130b2 commit fd06ee2

File tree

15 files changed

+772
-0
lines changed

15 files changed

+772
-0
lines changed

.drone.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ services:
9797
- su www-data -c "php /var/www/html/occ group:add users"
9898
- su www-data -c "php /var/www/html/occ group:adduser users user1"
9999
- su www-data -c "php /var/www/html/occ group:adduser users user2"
100+
- su www-data -c "git clone -b enh/noid/direct-editing https://github.com/nextcloud/text.git /var/www/html/apps/text/"
101+
- su www-data -c "php /var/www/html/occ app:enable text"
100102
- /run.sh
101103

102104
trigger:
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2019 Tobias Kaminsky
5+
* Copyright (C) 2019 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.nextcloud.android.lib.resources.directediting;
29+
30+
import com.owncloud.android.AbstractIT;
31+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
32+
33+
import org.junit.Test;
34+
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertTrue;
37+
38+
public class DirectEditingCreateFileRemoteOperationTest extends AbstractIT {
39+
@Test
40+
public void createEmptyFile() {
41+
RemoteOperationResult result = new DirectEditingCreateFileRemoteOperation("/test.md",
42+
"text",
43+
"textdocumenttemplate")
44+
.execute(client);
45+
assertTrue(result.isSuccess());
46+
47+
String url = (String) result.getSingleData();
48+
49+
assertFalse(url.isEmpty());
50+
}
51+
52+
@Test
53+
public void createFileFromTemplate() {
54+
RemoteOperationResult result = new DirectEditingCreateFileRemoteOperation("/test.md",
55+
"text",
56+
"textdocumenttemplate",
57+
"1")
58+
.execute(client);
59+
assertTrue(result.isSuccess());
60+
61+
String url = (String) result.getSingleData();
62+
63+
assertFalse(url.isEmpty());
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2019 Tobias Kaminsky
5+
* Copyright (C) 2019 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.nextcloud.android.lib.resources.directediting;
29+
30+
import com.owncloud.android.AbstractIT;
31+
import com.owncloud.android.lib.common.TemplateList;
32+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
33+
34+
import org.junit.Test;
35+
36+
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertTrue;
38+
39+
public class DirectEditingObtainListOfTemplatesRemoteOperationTest extends AbstractIT {
40+
41+
@Test
42+
public void testGetAll() {
43+
RemoteOperationResult result = new DirectEditingObtainListOfTemplatesRemoteOperation("text",
44+
"textdocumenttemplate")
45+
.execute(client);
46+
assertTrue(result.isSuccess());
47+
48+
TemplateList templateList = (TemplateList) result.getSingleData();
49+
50+
assertEquals("Weekly ToDo", templateList.templates.get("1").name);
51+
assertEquals("md", templateList.templates.get("1").extension);
52+
53+
assertEquals("Meeting notes", templateList.templates.get("2").name);
54+
assertEquals("md", templateList.templates.get("2").extension);
55+
}
56+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2019 Tobias Kaminsky
5+
* Copyright (C) 2019 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.nextcloud.android.lib.resources.directediting;
29+
30+
import com.owncloud.android.AbstractIT;
31+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
32+
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
33+
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation;
34+
import com.owncloud.android.lib.resources.files.model.RemoteFile;
35+
36+
import junit.framework.TestCase;
37+
38+
import org.junit.Test;
39+
40+
import java.io.IOException;
41+
42+
import static org.junit.Assert.assertFalse;
43+
import static org.junit.Assert.assertTrue;
44+
45+
public class DirectEditingOpenFileRemoteOperationTest extends AbstractIT {
46+
@Test
47+
public void createFileFromTemplate() throws IOException {
48+
// create file
49+
String filePath = createFile("text");
50+
String remotePath = "/text.md";
51+
TestCase.assertTrue(new UploadFileRemoteOperation(filePath, remotePath, "text/markdown", "123")
52+
.execute(client).isSuccess());
53+
54+
RemoteOperationResult readFile = new ReadFileRemoteOperation(remotePath).execute(client);
55+
TestCase.assertTrue(readFile.isSuccess());
56+
57+
RemoteFile remoteFile = (RemoteFile) readFile.getData().get(0);
58+
String remoteId = remoteFile.getRemoteId();
59+
String fileId = remoteId.substring(0, 8).replaceAll("^0*", "");
60+
61+
// open file
62+
RemoteOperationResult result = new DirectEditingOpenFileRemoteOperation(fileId, "text").execute(client);
63+
assertTrue(result.isSuccess());
64+
65+
String url = (String) result.getSingleData();
66+
67+
assertFalse(url.isEmpty());
68+
}
69+
}

src/androidTest/java/com/owncloud/android/GetCapabilitiesTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public void testGetRemoteCapabilitiesOperation() {
5353
OCCapability capability = (OCCapability) result.getData().get(0);
5454

5555
Assert.assertSame(capability.getRichDocuments(), CapabilityBooleanType.FALSE);
56+
57+
Assert.assertFalse(capability.getDirectEditing().creators.isEmpty());
58+
Assert.assertFalse(capability.getDirectEditing().editors.isEmpty());
59+
5660
// TODO assert basic capabilities
5761
}
5862
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* Nextcloud Android Library is available under MIT license
2+
*
3+
* @author Tobias Kaminsky
4+
* Copyright (C) 2019 Tobias Kaminsky
5+
* Copyright (C) 2019 Nextcloud GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
package com.nextcloud.android.lib.resources.directediting;
29+
30+
import android.net.Uri;
31+
32+
import com.owncloud.android.lib.common.OwnCloudClient;
33+
import com.owncloud.android.lib.common.operations.RemoteOperation;
34+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
35+
import com.owncloud.android.lib.common.utils.Log_OC;
36+
37+
import org.apache.commons.httpclient.HttpStatus;
38+
import org.apache.commons.httpclient.methods.PostMethod;
39+
import org.json.JSONObject;
40+
41+
/**
42+
* Create file with direct editing api
43+
*/
44+
45+
public class DirectEditingCreateFileRemoteOperation extends RemoteOperation {
46+
private static final String TAG = DirectEditingCreateFileRemoteOperation.class.getSimpleName();
47+
private static final int SYNC_READ_TIMEOUT = 40000;
48+
private static final int SYNC_CONNECTION_TIMEOUT = 5000;
49+
private static final String DIRECT_ENDPOINT = "/ocs/v2.php/apps/files/api/v1/directEditing/create";
50+
51+
private static final String JSON_FORMAT = "?format=json";
52+
53+
private String path;
54+
private String editor;
55+
private String creator;
56+
private String template;
57+
58+
public DirectEditingCreateFileRemoteOperation(String path, String editor, String creator) {
59+
this(path, editor, creator, "");
60+
}
61+
62+
public DirectEditingCreateFileRemoteOperation(String path, String editor, String creator, String template) {
63+
this.path = path;
64+
this.editor = editor;
65+
this.creator = creator;
66+
this.template = template;
67+
}
68+
69+
protected RemoteOperationResult run(OwnCloudClient client) {
70+
RemoteOperationResult result;
71+
PostMethod postMethod = null;
72+
73+
try {
74+
postMethod = new PostMethod(client.getBaseUri() + DIRECT_ENDPOINT + JSON_FORMAT);
75+
postMethod.addParameter("path", Uri.encode(path));
76+
postMethod.addParameter("editorId", editor);
77+
postMethod.addParameter("creatorId", creator);
78+
79+
if (!template.isEmpty()) {
80+
postMethod.addParameter("templateId", template);
81+
}
82+
83+
// remote request
84+
postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
85+
86+
int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
87+
88+
if (status == HttpStatus.SC_OK) {
89+
String response = postMethod.getResponseBodyAsString();
90+
91+
// Parse the response
92+
JSONObject respJSON = new JSONObject(response);
93+
String url = (String) respJSON.getJSONObject("ocs").getJSONObject("data").get("url");
94+
95+
result = new RemoteOperationResult(true, postMethod);
96+
result.setSingleData(url);
97+
} else {
98+
result = new RemoteOperationResult(false, postMethod);
99+
client.exhaustResponse(postMethod.getResponseBodyAsStream());
100+
}
101+
} catch (Exception e) {
102+
result = new RemoteOperationResult(e);
103+
Log_OC.e(TAG, "Get all direct editing informations failed: " + result.getLogMessage(),
104+
result.getException());
105+
} finally {
106+
if (postMethod != null) {
107+
postMethod.releaseConnection();
108+
}
109+
}
110+
return result;
111+
}
112+
}

0 commit comments

Comments
 (0)