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

Unverified Commit 4c04d440 authored by Andy Scherzinger's avatar Andy Scherzinger Committed by GitHub
Browse files

Merge pull request #145 from nextcloud/trashbin

Add Trashbin support
parents cad115a4 64a4117b
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -49,6 +49,9 @@ public class WebdavEntry {
    public static final String EXTENDED_PROPERTY_FAVORITE = "favorite";
    public static final String EXTENDED_PROPERTY_IS_ENCRYPTED = "is-encrypted";
    public static final String EXTENDED_PROPERTY_MOUNT_TYPE = "mount-type";
    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";

    public static final String PROPERTY_QUOTA_USED_BYTES = "quota-used-bytes";
    public static final String PROPERTY_QUOTA_AVAILABLE_BYTES = "quota-available-bytes";
@@ -57,6 +60,7 @@ public class WebdavEntry {

    private static final int CODE_PROP_NOT_FOUND = 404;
    

    private String mName;
    private String mPath;
    private String mUri;
@@ -64,6 +68,9 @@ public class WebdavEntry {
    private String mEtag;
    private String mPermissions;
    private String mRemoteId;
    private String mTrashbinOriginalLocation;
    private String mTrashbinFilename;
    private long mTrashbinDeletionTimestamp;
    private boolean mIsFavorite;
    private boolean mIsEncrypted;
    private MountType mMountType;
@@ -236,6 +243,23 @@ public class WebdavEntry {
                mMountType = MountType.INTERNAL;
            }
            
            // NC trashbin-original-location <nc:trashbin-original-location>
            prop = propSet.get(TRASHBIN_ORIGINAL_LOCATION, Namespace.getNamespace(NAMESPACE_NC));
            if (prop != null) {
                mTrashbinOriginalLocation = prop.getValue().toString();
            }

            // NC trashbin-filename <nc:trashbin-filename>
            prop = propSet.get(TRASHBIN_FILENAME, Namespace.getNamespace(NAMESPACE_NC));
            if (prop != null) {
                mTrashbinFilename = prop.getValue().toString();
            }

            // NC trashbin-deletion-time <nc:trashbin-deletion-time>
            prop = propSet.get(TRASHBIN_DELETION_TIME, Namespace.getNamespace(NAMESPACE_NC));
            if (prop != null) {
                mTrashbinDeletionTimestamp = Long.parseLong((String) prop.getValue());
            }
        } else {
            Log_OC.e("WebdavEntry", "General fuckup, no status for webdav response");
        }
@@ -317,6 +341,18 @@ public class WebdavEntry {
        return mMountType;
    }
    
    public String getTrashbinOriginalLocation() {
        return mTrashbinOriginalLocation;
    }
    
    public String getTrashbinFilename() {
        return mTrashbinFilename;
    }
    
    public long getTrashbinDeletionTimestamp() {
        return mTrashbinDeletionTimestamp;
    }

    private void resetData() {
        mName = mUri = mContentType = mPermissions = null; mRemoteId = null;
        mContentLength = mCreateTimestamp = mModifiedTimestamp = 0;
+23 −9
Original line number Diff line number Diff line
@@ -122,14 +122,28 @@ public class WebdavUtils {
        propSet.add(DavPropertyName.GETLASTMODIFIED);
        propSet.add(DavPropertyName.CREATIONDATE);
        propSet.add(DavPropertyName.GETETAG);
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_NAME_PERMISSIONS,
                Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_NAME_REMOTE_ID,
                Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_NAME_SIZE,
                Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_FAVORITE,
                Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_NAME_PERMISSIONS, Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_NAME_REMOTE_ID, Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_NAME_SIZE, Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_FAVORITE, Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));

        return propSet;
    }

    /**
     * Builds a DavPropertyNameSet with properties for trashbin
     * @return
     */
    public static DavPropertyNameSet getTrashbinPropSet() {
        DavPropertyNameSet propSet = new DavPropertyNameSet();
        propSet.add(DavPropertyName.RESOURCETYPE);
        propSet.add(DavPropertyName.GETCONTENTTYPE);
        propSet.add(DavPropertyName.GETCONTENTLENGTH);
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_NAME_SIZE, Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
        propSet.add(WebdavEntry.EXTENDED_PROPERTY_NAME_REMOTE_ID, Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
        propSet.add(WebdavEntry.TRASHBIN_FILENAME, Namespace.getNamespace(WebdavEntry.NAMESPACE_NC));
        propSet.add(WebdavEntry.TRASHBIN_ORIGINAL_LOCATION, Namespace.getNamespace(WebdavEntry.NAMESPACE_NC));
        propSet.add(WebdavEntry.TRASHBIN_DELETION_TIME, Namespace.getNamespace(WebdavEntry.NAMESPACE_NC));

        return propSet;
    }
+153 −0
Original line number Diff line number Diff line
/* ownCloud Android Library is available under MIT license
 *   Copyright (C) 2018 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.owncloud.android.lib.resources.files;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.network.WebdavEntry;
import com.owncloud.android.lib.common.network.WebdavUtils;
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.jackrabbit.webdav.DavConstants;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;

import java.util.ArrayList;

/**
 * Remote operation performing the read of remote trashbin folder on Nextcloud server.
 *
 * @author Tobias Kaminsky
 */

public class ReadRemoteTrashbinFolderOperation extends RemoteOperation {

    private static final String TAG = ReadRemoteTrashbinFolderOperation.class.getSimpleName();
    
    private String remotePath;
    private ArrayList<Object> folderAndFiles;
    private String userId;
    
    /**
     * Constructor
     *
     * @param remotePath Remote path of the file.
     */
    public ReadRemoteTrashbinFolderOperation(String remotePath, String userId) {
        this.remotePath = remotePath;
        this.userId = userId;
    }

    /**
     * Performs the read operation.
     *
     * @param client Client object to communicate with the remote ownCloud server.
     */
    @Override
    protected RemoteOperationResult run(OwnCloudClient client) {
        RemoteOperationResult result = null;
        PropFindMethod query = null;

        try {
            // remote request
            if (userId.isEmpty()) {
                throw new IllegalArgumentException("UserId may not be empty!");
            }

            String baseUri = client.getNewWebdavUri(false) + "/trashbin/" + userId + "/trash/";
            DavPropertyNameSet propSet = WebdavUtils.getTrashbinPropSet();
                
            query = new PropFindMethod(baseUri + WebdavUtils.encodePath(remotePath), propSet, DavConstants.DEPTH_1);
            int status = client.executeMethod(query);

            // check and process response
            boolean isSuccess = (status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK);
            
            if (isSuccess) {
                // get data from remote folder
                MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
                readData(dataInServer, client);

                // Result of the operation
                result = new RemoteOperationResult(true, query);
                // Add data to the result
                if (result.isSuccess()) {
                    result.setData(folderAndFiles);
                }
            } else {
                // synchronization failed
                client.exhaustResponse(query.getResponseBodyAsStream());
                result = new RemoteOperationResult(false, query);
            }
        } catch (Exception e) {
            result = new RemoteOperationResult(e);
        } finally {
            if (query != null)
                query.releaseConnection();  // let the connection available for other methods

            if (result == null) {
                result = new RemoteOperationResult(new Exception("unknown error"));
                Log_OC.e(TAG, "Synchronized " + remotePath + ": failed");
            } else {
                if (result.isSuccess()) {
                    Log_OC.i(TAG, "Synchronized " + remotePath + ": " + result.getLogMessage());
                } else {
                    if (result.isException()) {
                        Log_OC.e(TAG, "Synchronized " + remotePath + ": " + result.getLogMessage(),
                                result.getException());
                    } else {
                        Log_OC.e(TAG, "Synchronized " + remotePath + ": " + result.getLogMessage());
                    }
                }
            }
        }
        
        return result;
    }

    /**
     * Read the data retrieved from the server about the contents of the target folder
     *
     * @param remoteData Full response got from the server with the data of the target
     *                   folder and its direct children.
     * @param client     Client instance to the remote server where the data were retrieved.
     */
    private void readData(MultiStatus remoteData, OwnCloudClient client) {
        folderAndFiles = new ArrayList<>();

        // parse data from remote folder
        WebdavEntry we;
        String splitElement = client.getNewWebdavUri(false).getPath();

        // loop to update every child
        for (int i = 1; i < remoteData.getResponses().length; ++i) {
            we = new WebdavEntry(remoteData.getResponses()[i], splitElement);
            folderAndFiles.add(new TrashbinFile(we, userId));
        }
    }
}
+87 −0
Original line number Diff line number Diff line
/* ownCloud Android Library is available under MIT license
 *   Copyright (C) 2018 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.owncloud.android.lib.resources.files;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.network.WebdavUtils;
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.jackrabbit.webdav.client.methods.DeleteMethod;

/**
 * Remote operation performing the removal of a file in trashbin.
 *
 * @author Tobias Kaminsky
 */
public class RemoveTrashbinFileOperation extends RemoteOperation {
    private static final String TAG = RemoveTrashbinFileOperation.class.getSimpleName();

    private static final int REMOVE_READ_TIMEOUT = 30000;
    private static final int REMOVE_CONNECTION_TIMEOUT = 5000;

    private String remotePath;

    /**
     * Constructor
     *
     * @param remotePath RemotePath of the remote file or folder to remove from the server
     */
    public RemoveTrashbinFileOperation(String remotePath) {
        this.remotePath = remotePath;
    }

    /**
     * Performs the remove operation.
     *
     * @param client Client object to communicate with the remote ownCloud server.
     */
    @Override
    protected RemoteOperationResult run(OwnCloudClient client) {
        RemoteOperationResult result;
        DeleteMethod delete = null;

        try {
            delete = new DeleteMethod(client.getNewWebdavUri(false) + WebdavUtils.encodePath(remotePath));
            int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);

            delete.getResponseBodyAsString();   // exhaust the response, although not interesting
            result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), delete);
            Log_OC.i(TAG, "Remove " + remotePath + ": " + result.getLogMessage());

        } catch (Exception e) {
            result = new RemoteOperationResult(e);
            Log_OC.e(TAG, "Remove " + remotePath + ": " + result.getLogMessage(), e);

        } finally {
            if (delete != null)
                delete.releaseConnection();
        }

        return result;
    }
}
+18 −0
Original line number Diff line number Diff line
package com.owncloud.android.lib.resources.files;

public interface ServerFileInterface {
    
    String getFileName();
    
    String getMimeType();
    
    String getRemotePath();
    
    String getRemoteId();
    
    boolean getIsFavorite();
    
    boolean isFolder();
    
    long getFileLength();
}
Loading