66import enum
77import typing
88
9+ from .. import _misc
10+
911
1012@dataclasses .dataclass
1113class FsNodeInfo :
@@ -216,6 +218,26 @@ class FilePermissions(enum.IntFlag):
216218 """Access to re-share object(s)"""
217219
218220
221+ def permissions_to_str (permissions : int , is_dir : bool = False ) -> str :
222+ """Converts integer permissions to string permissions.
223+
224+ :param permissions: concatenation of ``FilePermissions`` integer flags.
225+ :param is_dir: Flag indicating is permissions related to the directory object or not.
226+ """
227+ r = ""
228+ if permissions & FilePermissions .PERMISSION_SHARE :
229+ r += "R"
230+ if permissions & FilePermissions .PERMISSION_READ :
231+ r += "G"
232+ if permissions & FilePermissions .PERMISSION_DELETE :
233+ r += "D"
234+ if permissions & FilePermissions .PERMISSION_UPDATE :
235+ r += "NV" if is_dir else "NVW"
236+ if is_dir and permissions & FilePermissions .PERMISSION_CREATE :
237+ r += "CK"
238+ return r
239+
240+
219241@dataclasses .dataclass
220242class SystemTag :
221243 """Nextcloud System Tag."""
@@ -242,3 +264,113 @@ def user_visible(self) -> bool:
242264 def user_assignable (self ) -> bool :
243265 """Flag indicating if User can assign this Tag."""
244266 return bool (self ._raw_data .get ("oc:user-assignable" , "false" ).lower () == "true" )
267+
268+
269+ class ShareType (enum .IntEnum ):
270+ """Type of the object that will receive share."""
271+
272+ TYPE_USER = 0
273+ """Share to the user"""
274+ TYPE_GROUP = 1
275+ """Share to the group"""
276+ TYPE_LINK = 3
277+ """Share by link"""
278+ TYPE_EMAIL = 4
279+ """Share by the email"""
280+ TYPE_REMOTE = 6
281+ """Share to the Federation"""
282+ TYPE_CIRCLE = 7
283+ """Share to the Nextcloud Circle"""
284+ TYPE_GUEST = 8
285+ """Share to `Guest`"""
286+ TYPE_REMOTE_GROUP = 9
287+ """Share to the Federation group"""
288+ TYPE_ROOM = 10
289+ """Share to the Talk room"""
290+ TYPE_DECK = 11
291+ """Share to the Nextcloud Deck"""
292+ TYPE_SCIENCE_MESH = 15
293+ """Share to the Reva instance(Science Mesh)"""
294+
295+
296+ class Share :
297+ """Information about Share."""
298+
299+ def __init__ (self , raw_data : dict ):
300+ self .raw_data = raw_data
301+
302+ @property
303+ def share_id (self ) -> int :
304+ """Unique ID of the share."""
305+ return int (self .raw_data ["id" ])
306+
307+ @property
308+ def share_type (self ) -> ShareType :
309+ """Type of the share."""
310+ return ShareType (int (self .raw_data ["share_type" ]))
311+
312+ @property
313+ def share_with (self ) -> str :
314+ """To whom Share was created."""
315+ return self .raw_data ["share_with" ]
316+
317+ @property
318+ def permissions (self ) -> FilePermissions :
319+ """Recipient permissions."""
320+ return FilePermissions (int (self .raw_data ["permissions" ]))
321+
322+ @property
323+ def url (self ) -> str :
324+ """URL at which Share is avalaible."""
325+ return self .raw_data .get ("url" , "" )
326+
327+ @property
328+ def path (self ) -> str :
329+ """Share path relative to the user's root directory."""
330+ return self .raw_data .get ("path" , "" ).lstrip ("/" )
331+
332+ @property
333+ def label (self ) -> str :
334+ """Label for the Shared object."""
335+ return self .raw_data .get ("label" , "" )
336+
337+ @property
338+ def note (self ) -> str :
339+ """Note for the Shared object."""
340+ return self .raw_data .get ("note" , "" )
341+
342+ @property
343+ def mimetype (self ) -> str :
344+ """Mimetype of the Shared object."""
345+ return self .raw_data .get ("mimetype" , "" )
346+
347+ @property
348+ def share_owner (self ) -> str :
349+ """Share's creator ID."""
350+ return self .raw_data .get ("uid_owner" , "" )
351+
352+ @property
353+ def file_owner (self ) -> str :
354+ """File/directory owner ID."""
355+ return self .raw_data .get ("uid_file_owner" , "" )
356+
357+ @property
358+ def password (self ) -> str :
359+ """Password to access share."""
360+ return self .raw_data .get ("password" , "" )
361+
362+ @property
363+ def send_password_by_talk (self ) -> bool :
364+ """Flag indicating was password send by Talk."""
365+ return self .raw_data .get ("send_password_by_talk" , False )
366+
367+ @property
368+ def expire_date (self ) -> datetime .datetime :
369+ """Share expiration time."""
370+ return _misc .nc_iso_time_to_datetime (self .raw_data .get ("expiration" , "" ))
371+
372+ def __str__ (self ):
373+ return (
374+ f"{ self .share_type .name } : `{ self .path } ` with id={ self .share_id } "
375+ f" from { self .share_owner } to { self .share_with } "
376+ )
0 commit comments