@@ -326,17 +326,65 @@ def setfav(self, path: Union[str, FsNode], value: Union[int, bool]) -> None:
326326 )
327327 check_error (webdav_response .status_code , f"setfav: path={ path } , value={ value } " )
328328
329- def _listdir (self , user : str , path : str , properties : list [str ], depth : int , exclude_self : bool ) -> list [FsNode ]:
329+ def trashbin_list (self ) -> list [FsNode ]:
330+ """Returns a list of all entries in the TrashBin."""
331+ properties = PROPFIND_PROPERTIES
332+ properties += ["nc:trashbin-filename" , "nc:trashbin-original-location" , "nc:trashbin-deletion-time" ]
333+ return self ._listdir (self ._session .user , "" , properties = properties , depth = 1 , exclude_self = False , trashbin = True )
334+
335+ def trashbin_restore (self , path : Union [str , FsNode ]) -> None :
336+ """Restore a file/directory from the TrashBin.
337+
338+ :param path: path to delete, e.g., the ``user_path`` field from ``FsNode`` or the **FsNode** class itself.
339+ """
340+ restore_name = path .name if isinstance (path , FsNode ) else path .split ("/" , maxsplit = 1 )[- 1 ]
341+ path = path .user_path if isinstance (path , FsNode ) else path
342+
343+ dest = self ._session .cfg .dav_endpoint + f"/trashbin/{ self ._session .user } /restore/{ restore_name } "
344+ headers = {"Destination" : dest }
345+ response = self ._session .dav (
346+ "MOVE" ,
347+ path = f"/trashbin/{ self ._session .user } /{ path } " ,
348+ headers = headers ,
349+ )
350+ check_error (response .status_code , f"trashbin_restore: user={ self ._session .user } , src={ path } , dest={ dest } " )
351+
352+ def trashbin_delete (self , path : Union [str , FsNode ], not_fail = False ) -> None :
353+ """Deletes a file/directory permanently from the TrashBin.
354+
355+ :param path: path to delete, e.g., the ``user_path`` field from ``FsNode`` or the **FsNode** class itself.
356+ :param not_fail: if set to ``True`` and the object is not found, it does not raise an exception.
357+ """
358+ path = path .user_path if isinstance (path , FsNode ) else path
359+ response = self ._session .dav (method = "DELETE" , path = f"/trashbin/{ self ._session .user } /{ path } " )
360+ if response .status_code == 404 and not_fail :
361+ return
362+ check_error (response .status_code , f"delete_from_trashbin: user={ self ._session .user } , path={ path } " )
363+
364+ def trashbin_cleanup (self ) -> None :
365+ """Empties the TrashBin."""
366+ response = self ._session .dav (method = "DELETE" , path = f"/trashbin/{ self ._session .user } /trash" )
367+ check_error (response .status_code , f"trashbin_cleanup: user={ self ._session .user } " )
368+
369+ def _listdir (
370+ self , user : str , path : str , properties : list [str ], depth : int , exclude_self : bool , trashbin : bool = False
371+ ) -> list [FsNode ]:
330372 root = ElementTree .Element (
331373 "d:propfind" ,
332374 attrib = {"xmlns:d" : "DAV:" , "xmlns:oc" : "http://owncloud.org/ns" , "xmlns:nc" : "http://nextcloud.org/ns" },
333375 )
334376 prop = ElementTree .SubElement (root , "d:prop" )
335377 for i in properties :
336378 ElementTree .SubElement (prop , i )
337- headers = {"Depth" : "infinity" if depth == - 1 else str (depth )}
379+ if trashbin :
380+ dav_path = self ._dav_get_obj_path (f"trashbin/{ user } /trash" , path , root_path = "" )
381+ else :
382+ dav_path = self ._dav_get_obj_path (user , path )
338383 webdav_response = self ._session .dav (
339- "PROPFIND" , self ._dav_get_obj_path (user , path ), data = self ._element_tree_as_str (root ), headers = headers
384+ "PROPFIND" ,
385+ dav_path ,
386+ self ._element_tree_as_str (root ),
387+ headers = {"Depth" : "infinity" if depth == - 1 else str (depth )},
340388 )
341389 request_info = f"list: { user } , { path } , { properties } "
342390 result = self ._lf_parse_webdav_records (webdav_response , request_info )
@@ -387,6 +435,12 @@ def _parse_record(full_path: str, prop_stats: list[dict]) -> FsNode:
387435 fs_node_args ["permissions" ] = prop ["oc:permissions" ]
388436 if "oc:favorite" in prop_keys :
389437 fs_node_args ["favorite" ] = bool (int (prop ["oc:favorite" ]))
438+ if "nc:trashbin-filename" in prop_keys :
439+ fs_node_args ["trashbin_filename" ] = prop ["nc:trashbin-filename" ]
440+ if "nc:trashbin-original-location" in prop_keys :
441+ fs_node_args ["trashbin_original_location" ] = prop ["nc:trashbin-original-location" ]
442+ if "nc:trashbin-deletion-time" in prop_keys :
443+ fs_node_args ["trashbin_deletion_time" ] = prop ["nc:trashbin-deletion-time" ]
390444 # xz = prop.get("oc:dDC", "")
391445 return FsNode (full_path , ** fs_node_args )
392446
0 commit comments