Skip to content

Commit de9f660

Browse files
authored
Fix user and user_path in FSNode for subpath deployments (#297)
Fixes #296 . Changes proposed in this pull request: * Regex instantiated on import for performance * Regex to fetch accurate user path regardless of subpath * Regex to fetch accurate user regardless of subpath Tested against three Nextcloud instances, with or without /nextcloud and index.php in the path. --------- Signed-off-by: Scott Williams <[email protected]>
1 parent aef1ed2 commit de9f660

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

nc_py_api/files/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
import email.utils
66
import enum
77
import os
8+
import re
89
import warnings
910

1011
from pydantic import BaseModel
1112

1213
from .. import _misc
1314

15+
user_regex = re.compile(r"(?:files|trashbin|versions)/([^/]+)/")
16+
"""Regex for evaluating user from full path string; instantiated once on import."""
17+
user_path_regex = re.compile(r".*?(files|trashbin|versions)/([^/]+)/")
18+
"""Regex for evaluating user path from full path string; instantiated once on import."""
19+
1420

1521
class LockType(enum.IntEnum):
1622
"""Nextcloud File Locks types."""
@@ -218,12 +224,12 @@ def name(self) -> str:
218224
@property
219225
def user(self) -> str:
220226
"""Returns user ID extracted from the `full_path`."""
221-
return self.full_path.lstrip("/").split("/", maxsplit=2)[1]
227+
return user_regex.findall(self.full_path)[0]
222228

223229
@property
224230
def user_path(self) -> str:
225231
"""Returns path relative to the user's root directory."""
226-
return self.full_path.lstrip("/").split("/", maxsplit=2)[-1]
232+
return user_path_regex.sub("", self.full_path, count=1)
227233

228234
@property
229235
def is_shared(self) -> bool:

0 commit comments

Comments
 (0)