-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathocsharetools.py
executable file
·292 lines (247 loc) · 9.44 KB
/
ocsharetools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/python3
import requests
import os
import configparser as ConfigParser
DEBUG = False
if DEBUG:
import http.client
http.client.HTTPConnection.debuglevel = 1
SHARETYPE_USER = 0
SHARETYPE_GROUP = 1
SHARETYPE_PUBLIC = 3
PERMISSION_READ = 1
PERMISSION_UPDATE = 2
PERMISSION_CREATE = 4
PERMISSION_DELETE = 8
PERMISSION_SHARE = 16
API_PATH = '/ocs/v1.php/apps/files_sharing/api/v1'
SHARE_PATH = '/public.php?service=files&t='
from sys import platform as _platform
if _platform == 'linux' or _platform == 'linux2':
CONFIG_PATH = os.path.expanduser('~/.local/share/data/ownCloud')
FOLDER_CHAR = '/'
elif _platform == 'darwin':
CONFIG_PATH = os.path.expanduser('~/Library/Application Support/ownCloud')
FOLDER_CHAR = '/'
elif _platform == 'win32':
CONFIG_PATH = os.environ['APPDATA']+'\ownCloud\owncloud.cfg'
FOLDER_CHAR = '\\'
def check_status(jsonfeed):
if jsonfeed['ocs']['meta']['statuscode'] != 100:
raise OCShareException(jsonfeed['ocs']['meta'])
def check_request(request):
if request.status_code != 200:
request.raise_for_status()
raise requests.exceptions.HTTPError(
'%s Client Error: %s' % (request.status_code, request.reason))
def full_path_to_cloud(fullPath):
paths = []
for f in os.listdir(CONFIG_PATH+'/folders'):
config = ConfigParser.ConfigParser()
config.read('%s/%s' % (CONFIG_PATH+'/folders', f))
paths.append(config['ownCloud']['localPath'])
for path in paths:
if fullPath[:len(path)] == path:
return fullPath[len(path)-1:]
return None
def get_instant_upload_path():
for f in os.listdir(CONFIG_PATH+'/folders'):
config = ConfigParser.ConfigParser()
config.read('%s/%s' % (CONFIG_PATH+'/folders', f))
if config['ownCloud']['targetPath'] == '/':
return (
config['ownCloud']['localPath'] +
FOLDER_CHAR +
'InstantUpload' +
FOLDER_CHAR
)
return None
class OCShareException(Exception):
def __init__(self, meta):
self.status = meta['status']
self.status_code = meta['statuscode']
self.message = meta['message']
def __str__(self):
return '%d %s' % (self.status_code, self.message)
class OCShareAPI:
def __init__(self, url, username, password, disable_ssl_verification=False):
self.username = username
self.password = password
self.url = url
self.disable_ssl_verification = disable_ssl_verification
def get_shares(self, path=None, reshares=None, subfiles=None):
"""Get a list of shares
Keyword arguments:
path -- Path to file/folder, or none for all shares (default None)
reshares -- Return not only the shares from the current user
but all shares from the given file. (Default False)
subfiles -- returns all shares within a folder, given that
path defines a folder
"""
request = requests.get(
'%s%s/shares' % (self.url, API_PATH),
auth=(self.username, self.password),
params={
'format': 'json',
'path': path,
'reshares': reshares,
'subfiles': subfiles
},
verify=not self.disable_ssl_verification
)
check_request(request)
jsonfeed = request.json()
check_status(jsonfeed)
shares = []
for share in jsonfeed['ocs']['data']:
shares.append(OCShare(self, **share))
return shares
def get_share_by_id(self, share_id):
"""Gets a share by ID
Keyword Arguments:
share_id -- The ID of the share
"""
request = requests.get(
'%s%s/shares/%d' % (self.url, API_PATH, share_id),
auth=(self.username, self.password),
params={'format': 'json'},
verify=not self.disable_ssl_verification
)
print('get_share_by_id: ', self.disable_ssl_verification)
check_request(request)
jsonfeed = request.json()
check_status(jsonfeed)
return OCShare(self, **jsonfeed['ocs']['data'][0])
def create_share(self, path, share_type, share_with=None,
public_upload=False, password=None, permissions=None):
"""Create a share
Keyboard arguments:
path -- Path to file/folder (Required)
share_type -- One of the following (Required)
SHARETYPE_USER - share with a user
SHARETYPE_GROUP - share with a group
SHARETYPE_PUBLIC - get a public link
share_with -- User/Group to share with, if share_type is
SHARETYPE_GROUP or SHARETYPE_USER, this is required
public_upload -- Allow public upload to a public shared folder
(True/False)
password -- Password to protect public link share with
permissions -- Bitwise permissions, use the PERMISSION_* constants
"""
request = requests.post(
'%s%s/shares' % (self.url, API_PATH),
allow_redirects=False,
auth=(self.username, self.password),
params={'format': 'json'},
data={
'path': path,
'shareType': share_type,
'shareWith': share_with,
'publicUpload': int(public_upload),
'password': password,
'permissions': permissions
},
verify=not self.disable_ssl_verification
)
check_request(request)
jsonfeed = request.json()
check_status(jsonfeed)
return self.get_share_by_id(jsonfeed['ocs']['data']['id'])
def delete_share(self, share):
"""Delete a share"""
return self.delete_share_by_id(share.id)
def delete_share_by_id(self, share_id):
"""Delete a share by ID"""
request = requests.delete(
'%s%s/shares/%d' % (self.url, API_PATH, share_id),
auth=(self.username, self.password),
params={'format': 'json'},
verify=not self.disable_ssl_verification
)
check_request(request)
jsonfeed = request.json()
check_status(jsonfeed)
def update_share(self, share, permissions=None,
password=None, public_upload=None, expire_date=None):
"""Update a share
Keyword Arguments:
share -- a share object
permissions -- Bitwise permissions, use the PERMISSION_* constants
password -- Password to protect public link share with, False to
disable password.
public_upload -- Allow public upload to a public shared folder
(True/False)
expire_date -- Expiry date, a python datetime object.
"""
return self.update_share_by_id(
share.id,
permissions,
password,
public_upload,
expireDate=expire_date
)
def update_share_by_id(self, share_id, permissions=None,
password=None, public_upload=None,
expire_date=None):
"""Update a share by ID
Keyword Arguments:
share -- a share ID
permissions -- Bitwise permissions, use the PERMISSION_* constants
password -- Password to protect public link share with, False to
disable password.
public_upload -- Allow public upload to a public shared folder
(True/False)
expire_date -- Expiry date, a python datetime object.
"""
if expire_date:
expire_date = expire_date.strftime('%d-%m-%Y')
elif expire_date is False:
expire_date = ''
if password is False:
password = ''
request = requests.put(
'%s%s/shares/%d' % (self.url, API_PATH, share_id),
auth=(self.username, self.password),
params={'format': 'json'},
data={
'permissions': permissions,
'password': password,
'publicUpload': public_upload,
'expireDate': expire_date
},
verify=not self.disable_ssl_verification
)
check_request(request)
jsonfeed = request.json()
check_status(jsonfeed)
class OCShare:
def __init__(self, ocshareapi, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
self.ocshareapi = ocshareapi
if self.token:
self.url = self.ocshareapi.url+SHARE_PATH+self.token
else:
self.url = None
def delete(self):
self.ocshareapi.delete_share_by_id(self.id)
def update(self, permissions=None,
password=None, public_upload=None, expire_date=None):
if permissions is not None:
self.permissions = permissions
return self.ocshareapi.update_share_by_id(
self.id,
permissions,
password,
public_upload,
expire_date
)
def __str__(self):
return '<OCShare #%d>' % (self.id)
def __unicode__(self):
return '<OCShare #%d>' % (self.id)
def __repr__(self):
return '<OCShare #%d>' % (self.id)
if __name__ == '__main__':
import ocsharetools_cli
ocsharetools_cli.run()