-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstaging_controller.py
446 lines (333 loc) · 12.3 KB
/
staging_controller.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import os
import logging
import shutil
import zipfile
import json
from flask import Blueprint, jsonify, request, send_file
from werkzeug.utils import secure_filename
from service.errors import ApiError
from service.user.user_service import auth
from utils import cilantro_info_file
staging_controller = Blueprint('staging', __name__)
staging_dir = os.environ['STAGING_DIR']
allowed_extensions = ['pdf', 'tif', 'tiff', 'zip', 'txt']
log = logging.getLogger(__name__)
def _get_directory_structure(dir_path, depths=0):
"""
Recursively creates a dictionary of the directory structure (files and subdirectories) for dir_path.
Arguments:
dir_path: string - The root directory that should be evaluated.
depths: int - The recursion depths, if you provide a negative value,
the complete directory structure for dir_path is retrieved (default 0,
only listing top level contents).
"""
tree = {}
with os.scandir(dir_path) as it:
for entry in sorted(it, key=lambda e: e.name):
if entry.is_file():
tree[entry.name] = {"type": "file", "name": entry.name}
else:
tree[entry.name] = {"type": "directory", "name": entry.name}
if depths != 0:
path = os.path.join(dir_path, entry.name)
# handle directories with legacy .info file, existence implies successful import
if os.path.exists(os.path.join(path, ".info")):
tree[entry.name]["job_info"] = {"status": "success", "msg": cilantro_info_file.DEFAULT_SUCCESS_MESSAGE}
else:
tree[entry.name]["job_info"] = _parse_info_file(os.path.join(path, cilantro_info_file.FILE_NAME))
tree[entry.name]["contents"] = _get_directory_structure(path, depths-1)
return tree
def _parse_info_file(path):
if not os.path.exists(path):
return None
else:
with open(path, 'r') as f:
return json.load(f)
@staging_controller.route('', methods=['GET'], strict_slashes=False, defaults={'path': '.'})
@staging_controller.route('/<path:path>', methods=['GET'], strict_slashes=False)
@auth.login_required
def get_path(path):
"""
Retrieve a file or folder content from the staging folder.
Returns A JSON array containing all files, if it's a directory or
the file's content if it's a file.
.. :quickref: Staging Controller; Retrieve file/folder from staging folder
**Example request**:
.. sourcecode:: http
GET /staging/<path>?depths=5 HTTP/1.1
**Example response ERROR**:
.. sourcecode:: http
HTTP/1.1 404 NOT FOUND
{
"error": {
"code": "file_not_found",
"message": "No resource was found under the path test"
},
"success": false
}
:reqheader Accept: application/json
:param str path: path to file
:param int depths: the directory stucture depths to reteive at `path` (default: 1,
returns direct subdirectories of path and their direct subdirectories.). If depths < 0
the complete subdirectory structure will be returned.
:resheader Content-Type: application/json
:status 200: array containing all file names, if it's a directory or the
file's content if it's a file
:status 404: file was not found
"""
depths = request.args.get('depths', 1, int)
abs_path = _get_absolute_path(path)
if os.path.isdir(abs_path):
return jsonify(_get_directory_structure(abs_path, depths=depths))
elif os.path.isfile(abs_path):
return send_file(abs_path)
else:
raise ApiError("file_not_found",
f"No resource was found under the path {path}", 404)
@staging_controller.route('/<path:path>', methods=['DELETE'],
strict_slashes=False)
@auth.login_required
def delete_from_staging(path):
"""
Delete file or directory from the staging area.
.. :quickref: Staging Controller; Delete file or dir from the staging area
**Example request**:
.. sourcecode:: http
DELETE /staging/<path> HTTP/1.1
**Example response SUCCESS**:
.. sourcecode:: http
HTTP/1.1 200 OK
{
"success": true
}
**Example response ERROR**:
.. sourcecode:: http
HTTP/1.1 404 NOT FOUND
{
"error": {
"code": "file_not_found",
"message": "No resource was found under the path <path>"
},
"success": false
}
:reqheader Accept: application/json
:resheader Content-Type: application/json
:param str path: path to file or directory to be deleted
"""
abs_path = _get_absolute_path(path)
try:
os.remove(abs_path)
except (FileNotFoundError, IsADirectoryError):
try:
shutil.rmtree(abs_path)
except FileNotFoundError:
raise ApiError("file_not_found",
f"No resource was found under the path {path}", 404)
return jsonify({"success": True}), 200
@staging_controller.route('/folder', methods=['POST'],
strict_slashes=False)
@auth.login_required
def create_folder():
"""
Create folders in the staging area.
Can be passed folders with subfolders separated by slashes.
.. :quickref: Staging Controller; Upload files to the staging area
**Example request**:
.. sourcecode:: http
POST /staging/folder HTTP/1.1
{
"folderpath": "test-folder/subfolder"
}
**Example response SUCCESS**:
.. sourcecode:: http
HTTP/1.1 200 OK
{
"success": true
}
:reqheader Accept: application/json
:<json string folderpath: folders to be created
:resheader Content-Type: application/json
:>json dict: operation result
:status 200: OK
:return: A JSON object containing the status of the operation
"""
if not request.data:
raise ApiError("no_payload_found", "No request payload found")
params = request.get_json(force=True)
if not params['folderpath']:
raise ApiError("param_not found", "Missing folderpath parameter")
folderpath = params['folderpath']
try:
os.makedirs(
_get_absolute_path(folderpath),
exist_ok=True
)
return jsonify({"success": True}), 200
except Exception as e:
return _generate_error_result(
folderpath,
"folder_creation_failed",
"An unknown error occurred.",
e)
@staging_controller.route('', methods=['POST'], strict_slashes=False)
@auth.login_required
def upload_to_staging():
"""
Upload files to the staging area.
If the names of the given files contain folders, these are created in the
staging area if not already present.
Alternatively you can pass an extra argument 'target_folder' which is
prepended to all passed file names.
The upload endpoint is able to handle single and multiple files provided
under any key.
.. :quickref: Staging Controller; Upload files to the staging area
**Example request**:
.. sourcecode:: http
POST /staging/ HTTP/1.1
**Example response SUCCESS**:
.. sourcecode:: http
HTTP/1.1 200 OK
{
"result": {
"<filename>": {
"success": true
}
}
}
**Example response ERROR**:
.. sourcecode:: http
HTTP/1.1 400 BAD REQUEST
{
"error": {
"code": "no_files_provided",
"message": "The request did not contain any files"
},
"success": false
}
:reqheader Accept: multipart/form-data
:formparam file: file to be uploaded
:formparam target_folder: (optional) target folder for all files
:resheader Content-Type: application/json
:>json dict: operation result
:status 200: OK
:status 400: no files were provided
:status 415: one of the files' extension is not allowed
:return: a JSON object
"""
log.debug(f"Uploading {len(request.files)} files")
results = {}
if 'target_folder' in request.form:
target_folder = request.form['target_folder']
else:
target_folder = ""
abs_path = _get_absolute_path(target_folder)
if request.files:
for key in request.files:
for file in request.files.getlist(key):
file.path = f"{abs_path}/{file.filename}"
results[file.filename] = _process_file(file)
return jsonify({"result": results}), 200
raise ApiError("no_files_provided",
f"The request did not contain any files")
@staging_controller.route('/move', methods=['POST'],
strict_slashes=False)
@auth.login_required
def move():
"""
Move a file or folder inside the staging area.
Folders inside the target path must already exist.
Uses :py:func:~os.rename.
.. :quickref: Staging Controller; Move files inside the staging area
**Example request**:
.. sourcecode:: http
POST /staging/move HTTP/1.1
{
"source": "test-folder/file.txt",
"target": "test-folder2/subfolder/"
}
**Example response SUCCESS**:
.. sourcecode:: http
HTTP/1.1 200 OK
{
"success": true
}
:reqheader Accept: application/json
:<json string source: path of the file to be moved
:<json string target: new path of the file to be moved
:resheader Content-Type: application/json
:>json dict: operation result
:status 200: OK
:return: A JSON object containing the status of the operation
"""
if not request.data:
raise ApiError("no_payload_found", "No request payload found")
params = request.get_json(force=True)
if 'source' not in params:
raise ApiError("param_not found", "Missing source parameter")
if 'target' not in params:
raise ApiError("param_not found", "Missing target parameter")
source = _get_absolute_path(params['source'])
target = _get_absolute_path(params['target'])
try:
os.rename(source, target)
return jsonify({"success": True}), 200
except Exception as e:
return _generate_error_result(
source,
"move_failed",
"An unknown error occurred.",
e)
def _process_file(file):
if not _is_allowed_file_extension(file.filename):
return _generate_error_result(
file,
"extension_not_allowed",
f"File extension '{_get_file_extension(file.filename)}'"
f" is not allowed.")
elif os.path.exists(file.path):
return _generate_error_result(
file,
"file_already_exists",
f"File already exists in folder.")
else:
try:
_upload_file(file)
if _get_file_extension(file.filename) == "zip":
_unzip_file(file.path)
return {"success": True}
except Exception as e:
return _generate_error_result(
file,
"upload_failed",
"An unknown error occurred.",
e)
def _generate_error_result(file, code, message, e=None):
log.error(f"Error during upload of {file.filename}. {message}.")
if e:
log.error(f" Cause: {str(e)}")
return {
"success": False,
"error": {
"code": code,
"message": message}
}
def _upload_file(file):
directory_structure, filename = os.path.split(file.path)
os.makedirs(directory_structure, exist_ok=True)
file.save(file.path)
def _unzip_file(path):
target_path = os.path.dirname(path)
with zipfile.ZipFile(path, 'r') as zip_ref:
zip_ref.extractall(target_path)
os.remove(path)
def _is_allowed_file_extension(filename):
return _get_file_extension(filename) in allowed_extensions
def _get_file_extension(filename):
if '.' not in filename:
return ""
else:
return filename.rsplit('.', 1)[1].lower()
def _get_absolute_path(path):
if auth.username() == "admin":
return os.path.join(staging_dir, path)
return os.path.join(staging_dir, auth.username(), path)