nextcloud-API icon indicating copy to clipboard operation
nextcloud-API copied to clipboard

Interact with Trashbin

Open rjhenry opened this issue 3 years ago • 1 comments

Interaction with the Trashbin is as simple as sending a regular WebDAV PROPFIND to /remote.php/dav/trashbin/USER/trash rather than remote.php/dav/files/USER (src).

As far as I can tell, this would essentially be a matter of being able to set the API_URL in the WebDAV wrapper to remote.php/dav/trashbin and use the existing functions as they are, but as a Python noob I can't see if there's a way to relatively easily do that in application code, or whether there needs to be some library changes.

If the former - it's a change in application code - a nudge in the right direction (which I can then probably document!) would be appreciated - if it's the latter, I'd like to submit this as a feature request. The Python noob-iness would unfortunately interfere with adding the necessary code myself, I'm afraid (too many moving cogs!)

Thanks for an excellent product!

rjhenry avatar Jan 01 '22 20:01 rjhenry

The test need to be implemented (in tests/test_webdav.py). For the trash feature this may be similar to that (not tested) (for example in api_wrappers/webdav_trash.py).

See branch https://github.com/luffah/nextcloud-API/tree/dev-trashbin https://github.com/luffah/nextcloud-API/commit/7112b5e75cdd1676c9db1ed371a2395285611dc5

"""
Webdav Trashbin wrapper
See https://docs.nextcloud.com/server/14/admin_manual/configuration_user/user_auth_ldap_api.html
"""
import re
from nextcloud import base
from ..api.properties import NCProp
from . import webdav

class File(webdav.File):
    """
    Define properties on a WebDav file in trash
    """

    trashbin_filename = NCProp()
    trashbin_original_location = NCProp()
    trashbin_delection_time = NCProp()
    
    
    def delete(self):
        """
        Delete file (see WebDAVTrash wrapper)
        """
        resp = self._wrapper.delete_trashbin_file(self._get_remote_path())

        return resp.is_ok

    def restore(self):
        """
        Restore file (see WebDAVTrash wrapper)
        """
        resp = self._wrapper.restore_trashbin_file(self._get_remote_path())

        return resp.is_ok
    


class WebDAVTrash(base.WebDAVApiWrapper):
    """ WebDav API wrapper """
    API_URL = "/remote.php/dav/trashbin"

    def _get_path(self, operation):
        return '/'.join([self.client.user, operation]).replace('//', '/')

    def list_trashbin_files(self, all_properties=False, fields=None):
        """
        Get files list with files properties
        (for current user)

        Args:
            all_properties (bool): list all available file properties in Nextcloud
            fields (str list): file properties to fetch

        Returns:
            list of (trash) File objects
        """
        data = File.build_xml_propfind(
            use_default=all_properties,
            fields=fields
        )
        resp = self.requester.propfind(self._get_path('trash'),
                                       data=data)
        return File.from_response(resp, wrapper=self)

    def delete_trashbin_file(self, path):
        return self.requester.delete(url=self._get_path(path))

    def restore_trashbin_file(self, path):
        return self.requester.move(url=self._get_path(path),
                                   destination=self._get_path('restore'))

    def empty_trashbin(self):
        return self.requester.delete(url=self._get_path('trash'))

luffah avatar Apr 03 '22 16:04 luffah