|
1
|
|
|
from NextCloud.base import WithRequester, ShareType |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class Share(WithRequester): |
|
5
|
|
|
API_URL = "/ocs/v2.php/apps/files_sharing/api/v1" |
|
6
|
|
|
LOCAL = "shares" |
|
7
|
|
|
|
|
8
|
|
|
def get_local_url(self, additional_url=""): |
|
9
|
|
|
if additional_url: |
|
10
|
|
|
return "/".join([self.LOCAL, additional_url]) |
|
11
|
|
|
return self.LOCAL |
|
12
|
|
|
|
|
13
|
|
|
@staticmethod |
|
14
|
|
|
def validate_share_parameters(path, share_type, share_with): |
|
15
|
|
|
""" |
|
16
|
|
|
Check if share parameters make sense |
|
17
|
|
|
|
|
18
|
|
|
Args: |
|
19
|
|
|
path (str): path to the file/folder which should be shared |
|
20
|
|
|
share_type (int): ShareType attribute |
|
21
|
|
|
share_with (str): user/group id with which the file should be shared |
|
22
|
|
|
|
|
23
|
|
|
Returns: |
|
24
|
|
|
bool: True if parameters make sense together, False otherwise |
|
25
|
|
|
""" |
|
26
|
|
|
if (path is None or not isinstance(share_type, int)) \ |
|
27
|
|
|
or (share_type in [ShareType.GROUP, ShareType.USER, ShareType.FEDERATED_CLOUD_SHARE] |
|
28
|
|
|
and share_with is None): |
|
29
|
|
|
return False |
|
30
|
|
|
return True |
|
31
|
|
|
|
|
32
|
|
|
def get_shares(self): |
|
33
|
|
|
""" Get all shares from the user """ |
|
34
|
|
|
return self.requester.get(self.get_local_url()) |
|
35
|
|
|
|
|
36
|
|
|
def get_shares_from_path(self, path, reshares=None, subfiles=None): |
|
37
|
|
|
""" |
|
38
|
|
|
Get all shares from a given file/folder |
|
39
|
|
|
|
|
40
|
|
|
Args: |
|
41
|
|
|
path (str): path to file/folder |
|
42
|
|
|
reshares (bool): (optional) return not only the shares from the current user but all shares from the given file |
|
43
|
|
|
subfiles (bool): (optional) return all shares within a folder, given that path defines a folder |
|
44
|
|
|
|
|
45
|
|
|
Returns: |
|
46
|
|
|
|
|
47
|
|
|
""" |
|
48
|
|
|
url = self.get_local_url() |
|
49
|
|
|
params = { |
|
50
|
|
|
"path": path, |
|
51
|
|
|
"reshares": None if reshares is None else str(bool(reshares)).lower(), # TODO: test reshares, subfiles |
|
52
|
|
|
"subfiles": None if subfiles is None else str(bool(subfiles)).lower(), |
|
53
|
|
|
} |
|
54
|
|
|
return self.requester.get(url, params=params) |
|
55
|
|
|
|
|
56
|
|
|
def get_share_info(self, sid): |
|
57
|
|
|
""" |
|
58
|
|
|
Get information about a given share |
|
59
|
|
|
|
|
60
|
|
|
Args: |
|
61
|
|
|
sid (int): share id |
|
62
|
|
|
|
|
63
|
|
|
Returns: |
|
64
|
|
|
""" |
|
65
|
|
|
return self.requester.get(self.get_local_url(sid)) |
|
66
|
|
|
|
|
67
|
|
|
def create_share( |
|
68
|
|
|
self, path, share_type, share_with=None, public_upload=None, |
|
69
|
|
|
password=None, permissions=None): |
|
70
|
|
|
""" |
|
71
|
|
|
Share a file/folder with a user/group or as public link |
|
72
|
|
|
|
|
73
|
|
|
Mandatory fields: share_type, path and share_with for share_type USER (0) or GROUP (1). |
|
74
|
|
|
|
|
75
|
|
|
Args: |
|
76
|
|
|
path (str): path to the file/folder which should be shared |
|
77
|
|
|
share_type (int): ShareType attribute |
|
78
|
|
|
share_with (str): user/group id with which the file should be shared |
|
79
|
|
|
public_upload (bool): bool, allow public upload to a public shared folder (true/false) |
|
80
|
|
|
password (str): password to protect public link Share with |
|
81
|
|
|
permissions (int): sum of selected Permission attributes |
|
82
|
|
|
|
|
83
|
|
|
Returns: |
|
84
|
|
|
|
|
85
|
|
|
""" |
|
86
|
|
|
if not self.validate_share_parameters(path, share_type, share_with): |
|
87
|
|
|
return False |
|
88
|
|
|
|
|
89
|
|
|
url = self.get_local_url() |
|
90
|
|
|
if public_upload: |
|
91
|
|
|
public_upload = "true" |
|
92
|
|
|
|
|
93
|
|
|
data = {"path": path, "shareType": share_type} |
|
94
|
|
|
if share_type in [ShareType.GROUP, ShareType.USER, ShareType.FEDERATED_CLOUD_SHARE]: |
|
95
|
|
|
data["shareWith"] = share_with |
|
96
|
|
|
if public_upload: |
|
97
|
|
|
data["publicUpload"] = public_upload |
|
98
|
|
|
if share_type == ShareType.PUBLIC_LINK and password is not None: |
|
99
|
|
|
data["password"] = str(password) |
|
100
|
|
|
if permissions is not None: |
|
101
|
|
|
data["permissions"] = permissions |
|
102
|
|
|
return self.requester.post(url, data) |
|
103
|
|
|
|
|
104
|
|
|
def delete_share(self, sid): |
|
105
|
|
|
""" |
|
106
|
|
|
Remove the given share |
|
107
|
|
|
|
|
108
|
|
|
Args: |
|
109
|
|
|
sid (str): share id |
|
110
|
|
|
|
|
111
|
|
|
Returns: |
|
112
|
|
|
|
|
113
|
|
|
""" |
|
114
|
|
|
return self.requester.delete(self.get_local_url(sid)) |
|
115
|
|
|
|
|
116
|
|
|
def update_share(self, sid, permissions=None, password=None, public_upload=None, expire_date=""): |
|
117
|
|
|
""" |
|
118
|
|
|
Update a given share, only one value can be updated per request |
|
119
|
|
|
|
|
120
|
|
|
Args: |
|
121
|
|
|
sid (str): share id |
|
122
|
|
|
permissions (int): sum of selected Permission attributes |
|
123
|
|
|
password (str): password to protect public link Share with |
|
124
|
|
|
public_upload (bool): bool, allow public upload to a public shared folder (true/false) |
|
125
|
|
|
expire_date (str): set an expire date for public link shares. Format: ‘YYYY-MM-DD’ |
|
126
|
|
|
|
|
127
|
|
|
Returns: |
|
128
|
|
|
|
|
129
|
|
|
""" |
|
130
|
|
|
params = dict( |
|
131
|
|
|
permissions=permissions, |
|
132
|
|
|
password=password, |
|
133
|
|
|
expireDate=expire_date |
|
134
|
|
|
) |
|
135
|
|
|
if public_upload: |
|
136
|
|
|
params["publicUpload"] = "true" |
|
137
|
|
|
if public_upload is False: |
|
138
|
|
|
params["publicUpload"] = "false" |
|
139
|
|
|
|
|
140
|
|
|
# check if only one param specified |
|
141
|
|
|
specified_params_count = sum([int(bool(each)) for each in params.values()]) |
|
142
|
|
|
if specified_params_count > 1: |
|
143
|
|
|
raise ValueError("Only one parameter for update can be specified per request") |
|
144
|
|
|
|
|
145
|
|
|
url = self.get_local_url(sid) |
|
146
|
|
|
return self.requester.put(url, data=params) |
|
147
|
|
|
|