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

Unverified Commit 367ed98f authored by tobiasKaminsky's avatar tobiasKaminsky
Browse files

Add RichWorkspaces support

parent 00df7ada
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
144
145
+91 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android client application
 *
 * @author Tobias Kaminsky
 * Copyright (C) 2019 Tobias Kaminsky
 * Copyright (C) 2019 Nextcloud GmbH
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

package com.nextcloud.android.lib.richWorkspace;

import android.webkit.URLUtil;

import com.owncloud.android.AbstractIT;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation;
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

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

public class RichWorkspaceDirectEditingRemoteOperationTest extends AbstractIT {

    @Test
    public void getEditLinkForRoot() {
        RemoteOperationResult result = new RichWorkspaceDirectEditingRemoteOperation("/").execute(client);
        assertTrue(result.isSuccess());
        assertNotNull(result.getSingleData());

        String url = (String) result.getSingleData();

        assertTrue(URLUtil.isValidUrl(url));
    }

    @Test
    public void getEditLinkForFolder() {
        String path = "/workspace/sub1/";

        assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());

        RemoteOperationResult result = new RichWorkspaceDirectEditingRemoteOperation(path).execute(client);
        assertTrue(result.isSuccess());
        assertNotNull(result.getSingleData());

        String url = (String) result.getSingleData();

        assertTrue(URLUtil.isValidUrl(url));
    }

    @Test
    public void reuseExistingFile() throws IOException {
        String folder = "/Workspace/";
        String filePath = folder + "Readme.md";
        File txtFile = getFile(ASSETS__TEXT_FILE_NAME);

        assertTrue(new CreateFolderRemoteOperation(folder, true).execute(client).isSuccess());

        RemoteOperationResult uploadResult = new UploadFileRemoteOperation(txtFile.getAbsolutePath(),
                filePath,
                "txt/plain",
                String.valueOf(System.currentTimeMillis() / 1000))
                .execute(client);

        assertTrue("Error uploading file " + filePath + ": " + uploadResult, uploadResult.isSuccess());

        RemoteOperationResult result = new RichWorkspaceDirectEditingRemoteOperation(folder).execute(client);
        assertTrue(result.isSuccess());
        assertNotNull(result.getSingleData());

        String url = (String) result.getSingleData();

        assertTrue(URLUtil.isValidUrl(url));
    }
}
+104 −0
Original line number Diff line number Diff line
/* Nextcloud Android Library is available under MIT license
 *
 *   @author Tobias Kaminsky
 *   Copyright (C) 2019 Tobias Kaminsky
 *   Copyright (C) 2019 Nextcloud GmbH
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 *
 */

package com.nextcloud.android.lib.richWorkspace;

import com.google.gson.GsonBuilder;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Get direct editing url for rich workspace
 */

public class RichWorkspaceDirectEditingRemoteOperation extends RemoteOperation {
    private static final String TAG = RichWorkspaceDirectEditingRemoteOperation.class.getSimpleName();
    private static final int SYNC_READ_TIMEOUT = 40000;
    private static final int SYNC_CONNECTION_TIMEOUT = 5000;
    private static final String DIRECT_ENDPOINT = "/ocs/v2.php/apps/text/workspace/direct";
    private static final String JSON_FORMAT = "?format=json";
    private static final String PATH = "path";

    private String path;

    public RichWorkspaceDirectEditingRemoteOperation(String path) {
        this.path = path;
    }

    protected RemoteOperationResult run(OwnCloudClient client) {
        RemoteOperationResult result;
        PostMethod postMethod = null;

        try {
            postMethod = new PostMethod(client.getBaseUri() + DIRECT_ENDPOINT + JSON_FORMAT);
            postMethod.addRequestHeader(CONTENT_TYPE, JSON_ENCODED);
            postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

            Map<String, String> values = new HashMap<>();
            values.put(PATH, path);

            String json = new GsonBuilder().create().toJson(values, Map.class);

            postMethod.setRequestEntity(new StringRequestEntity(json));

            int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);

            if (status == HttpStatus.SC_OK) {
                String response = postMethod.getResponseBodyAsString();

                // Parse the response
                JSONObject respJSON = new JSONObject(response);
                String url = (String) respJSON.getJSONObject("ocs").getJSONObject("data").get("url");

                result = new RemoteOperationResult(true, postMethod);
                result.setSingleData(url);
            } else {
                result = new RemoteOperationResult(false, postMethod);
                client.exhaustResponse(postMethod.getResponseBodyAsStream());
            }
        } catch (Exception e) {
            result = new RemoteOperationResult(e);
            Log_OC.e(TAG, "Get edit url for rich workspace failed: " + result.getLogMessage(),
                    result.getException());
        } finally {
            if (postMethod != null) {
                postMethod.releaseConnection();
            }
        }
        return result;
    }
}
+9 −1
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ public class WebdavEntry {
    public static final String EXTENDED_PROPERTY_HAS_PREVIEW = "has-preview";
    public static final String EXTENDED_PROPERTY_NOTE = "note";
    public static final String EXTENDED_PROPERTY_SHAREES = "sharees";
    public static final String EXTENDED_PROPERTY_RICH_WORKSPACE = "rich-workspace";
    public static final String TRASHBIN_FILENAME = "trashbin-filename";
    public static final String TRASHBIN_ORIGINAL_LOCATION = "trashbin-original-location";
    public static final String TRASHBIN_DELETION_TIME = "trashbin-deletion-time";
@@ -105,6 +106,7 @@ public class WebdavEntry {
    @Getter @Setter private boolean hasPreview;
    @Getter private String note = "";
    @Getter private ShareeUser[] sharees = new ShareeUser[0];
    @Getter private String richWorkspace = "";

    public enum MountType {INTERNAL, EXTERNAL, GROUP}

@@ -320,12 +322,18 @@ public class WebdavEntry {
                trashbinDeletionTimestamp = Long.parseLong((String) prop.getValue());
            }

            // NC note property <nc-note>
            // NC note property <nc:note>
            prop = propSet.get(EXTENDED_PROPERTY_NOTE, ncNamespace);
            if (prop != null && prop.getValue() != null) {
                note = prop.getValue().toString();
            }

            // NC rich-workspace property <nc:rich-workspace>
            prop = propSet.get(EXTENDED_PROPERTY_RICH_WORKSPACE, ncNamespace);
            if (prop != null && prop.getValue() != null) {
                richWorkspace = prop.getValue().toString();
            }

            // NC sharees property <nc-sharees>
            prop = propSet.get(EXTENDED_PROPERTY_SHAREES, ncNamespace);
            if (prop != null && prop.getValue() != null) {
+1 −0
Original line number Diff line number Diff line
@@ -110,6 +110,7 @@ public class WebdavUtils {
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_HAS_PREVIEW, ncNamespace);
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_NOTE, ncNamespace);
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_SHAREES, ncNamespace);
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_RICH_WORKSPACE, ncNamespace);

        return propSet;
    }
Loading