Skip to content

Commit 9ac433b

Browse files
authored
made UiActionFileInfo.to_fs_node more consistent (#105)
Now created `FsNode` has the same `file_id` as from WebDAV request with type `str`. Was not able to do it earlier, as only recent AppEcosystem version start providing Nextcloud Instance ID. Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent cdbac4e commit 9ac433b

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [0.0.42 - 2023-08-30]
6+
7+
### Fixed
8+
9+
- Created `FsNode` from `UiActionFileInfo` now have the `file_id` with the NC instance ID as from the DAV requests.
10+
511
## [0.0.41 - 2023-08-26]
612

713
### Added

nc_py_api/ex_app/ui/files.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
from ..._session import NcSessionApp
1111
from ...files import FilePermissions, FsNode
1212

13-
ENDPOINT_SUFFIX = "files/actions/menu"
14-
1513

1614
class UiActionFileInfo(BaseModel):
1715
"""File Information Nextcloud sends to the External Application."""
@@ -40,18 +38,17 @@ class UiActionFileInfo(BaseModel):
4038
"""If the object is shared, this is a display name of the share owner."""
4139
shareOwnerId: typing.Optional[str]
4240
"""If the object is shared, this is the owner ID of the share."""
41+
instanceId: typing.Optional[str]
42+
"""Nextcloud instance ID."""
4343

4444
def to_fs_node(self) -> FsNode:
45-
"""Returns created ``FsNode`` from the file info given.
46-
47-
.. note:: :py:attr:`~nc_py_api.files.FsNode.file_id` in this case is ``without`` **instance_id**
48-
and equal to :py:attr:`~nc_py_api.files.FsNodeInfo.fileid`.
49-
"""
45+
"""Returns usual :py:class:`~nc_py_api.files.FsNode` created from this class."""
5046
user_path = os.path.join(self.directory, self.name).rstrip("/")
5147
is_dir = bool(self.fileType.lower() == "dir")
5248
if is_dir:
5349
user_path += "/"
5450
full_path = os.path.join(f"files/{self.userId}", user_path.lstrip("/"))
51+
file_id = str(self.fileId).rjust(8, "0")
5552

5653
permissions = "S" if self.shareOwnerId else ""
5754
if self.permissions & FilePermissions.PERMISSION_SHARE:
@@ -71,7 +68,7 @@ def to_fs_node(self) -> FsNode:
7168
content_length=0 if is_dir else self.size,
7269
permissions=permissions,
7370
favorite=bool(self.favorite.lower() == "true"),
74-
file_id=self.fileId,
71+
file_id=file_id + self.instanceId if self.instanceId else file_id,
7572
fileid=self.fileId,
7673
last_modified=datetime.utcfromtimestamp(self.mtime).replace(tzinfo=timezone.utc),
7774
)
@@ -91,6 +88,8 @@ class UiFileActionHandlerInfo(BaseModel):
9188
class _UiFilesActionsAPI:
9289
"""API for the drop-down menu in Nextcloud **Files app**."""
9390

91+
_ep_suffix: str = "files/actions/menu"
92+
9493
def __init__(self, session: NcSessionApp):
9594
self._session = session
9695

@@ -109,14 +108,14 @@ def register(self, name: str, display_name: str, callback_url: str, **kwargs) ->
109108
"action_handler": callback_url,
110109
},
111110
}
112-
self._session.ocs(method="POST", path=f"{self._session.ae_url}/{ENDPOINT_SUFFIX}", json=params)
111+
self._session.ocs(method="POST", path=f"{self._session.ae_url}/{self._ep_suffix}", json=params)
113112

114113
def unregister(self, name: str, not_fail=True) -> None:
115114
"""Removes files dropdown menu element."""
116115
require_capabilities("app_ecosystem_v2", self._session.capabilities)
117116
params = {"fileActionMenuName": name}
118117
try:
119-
self._session.ocs(method="DELETE", path=f"{self._session.ae_url}/{ENDPOINT_SUFFIX}", json=params)
118+
self._session.ocs(method="DELETE", path=f"{self._session.ae_url}/{self._ep_suffix}", json=params)
120119
except NextcloudExceptionNotFound as e:
121120
if not not_fail:
122121
raise e from None

tests/ui_files_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def ui_action_check(directory: str, fs_object: FsNode):
8888
permissions += FilePermissions.PERMISSION_DELETE
8989
if fs_object.is_shareable:
9090
permissions += FilePermissions.PERMISSION_SHARE
91+
fileid_str = str(fs_object.info.fileid)
92+
i = fs_object.file_id.find(fileid_str)
9193
file_info = ex_app.UiActionFileInfo(
9294
fileId=fs_object.info.fileid,
9395
name=fs_object.name,
@@ -102,14 +104,15 @@ def ui_action_check(directory: str, fs_object: FsNode):
102104
userId=fs_object.user,
103105
shareOwner="some_user" if fs_object.is_shared else None,
104106
shareOwnerId="some_user_id" if fs_object.is_shared else None,
107+
instanceId=fs_object.file_id[i + len(fileid_str) :],
105108
)
106109
fs_node = file_info.to_fs_node()
107110
assert isinstance(fs_node, FsNode)
108111
assert fs_node.etag == fs_object.etag
109112
assert fs_node.name == fs_object.name
110113
assert fs_node.user_path == fs_object.user_path
111114
assert fs_node.full_path == fs_object.full_path
112-
assert fs_node.file_id == fs_object.info.fileid
115+
assert fs_node.file_id == fs_object.file_id
113116
assert fs_node.is_dir == fs_object.is_dir
114117
# assert fs_node.mime == fs_object.mime
115118
assert fs_node.info.permissions == fs_object.info.permissions

0 commit comments

Comments
 (0)