|
23 | 23 | import posixpath |
24 | 24 | from io import BytesIO as string_io |
25 | 25 |
|
| 26 | +from django.core.files.uploadhandler import StopUpload |
26 | 27 | from django.core.paginator import EmptyPage, Paginator |
27 | 28 | from django.http import HttpResponse, HttpResponseNotModified, HttpResponseRedirect, StreamingHttpResponse |
28 | 29 | from django.utils.http import http_date |
|
50 | 51 | RESTRICT_FILE_EXTENSIONS, |
51 | 52 | SHOW_DOWNLOAD_BUTTON, |
52 | 53 | ) |
53 | | -from filebrowser.lib import xxd |
54 | 54 | from filebrowser.lib.rwx import compress_mode, filetype, rwx |
55 | 55 | from filebrowser.utils import parse_broker_url |
56 | 56 | from filebrowser.views import ( |
|
60 | 60 | _is_hdfs_superuser, |
61 | 61 | _massage_page, |
62 | 62 | _normalize_path, |
| 63 | + extract_upload_data, |
| 64 | + perform_upload_task, |
63 | 65 | read_contents, |
64 | 66 | stat_absolute_path, |
65 | 67 | ) |
@@ -386,12 +388,78 @@ def stat(request): |
386 | 388 |
|
387 | 389 | @api_error_handler |
388 | 390 | def upload_chunks(request): |
389 | | - pass |
| 391 | + """ |
| 392 | + Handles chunked file uploads using FineUploaderChunkedUploadHandler. |
| 393 | +
|
| 394 | + This method processes the chunked file uploads and checks if the file is larger |
| 395 | + than the single chunk size. If the file is larger, it returns a JSON response |
| 396 | + with the UUID of the file. If the file is smaller, it extracts the upload data |
| 397 | + and performs the upload task. |
| 398 | +
|
| 399 | + Args: |
| 400 | + request (HttpRequest): The HTTP request object containing the chunked file. |
| 401 | +
|
| 402 | + Returns: |
| 403 | + HttpResponse: A JSON response with the UUID of the file if the file is larger |
| 404 | + than the single chunk size, or a JSON response with the result of the |
| 405 | + upload task if the file is smaller. If an error occurs, returns an HTTP |
| 406 | + response with a 500 status code and an error message. |
| 407 | +
|
| 408 | + Raises: |
| 409 | + StopUpload: If an error occurs during chunk file upload. |
| 410 | +
|
| 411 | + Notes: |
| 412 | + This method expects the following parameters in the request: |
| 413 | + - `qqtotalparts` (int): The total number of parts in the chunked file. |
| 414 | + - `qquuid` (str): The UUID of the file. |
| 415 | + """ |
| 416 | + try: |
| 417 | + # Process the chunked file uploads using FineUploaderChunkedUploadHandler |
| 418 | + for _ in request.FILES.values(): |
| 419 | + pass |
| 420 | + except StopUpload as e: |
| 421 | + error_message = 'Error occurred during chunk file upload.' |
| 422 | + LOG.error(f'{error_message} {str(e)}') |
| 423 | + return HttpResponse(error_message, status=500) |
| 424 | + |
| 425 | + # Check if the file is larger than the single chunk size |
| 426 | + total_parts = int(request.GET.get("qqtotalparts", 0)) |
| 427 | + if total_parts > 0: |
| 428 | + return JsonResponse({'uuid': request.GET.get('qquuid')}) |
| 429 | + |
| 430 | + # Check if the file is smaller than the chunk size |
| 431 | + elif total_parts == 0: |
| 432 | + try: |
| 433 | + chunks = extract_upload_data(request, "GET") |
| 434 | + response = perform_upload_task(request, **chunks) |
| 435 | + return JsonResponse(response) |
| 436 | + |
| 437 | + except Exception as e: |
| 438 | + error_message = 'Error occurred during chunk file upload.' |
| 439 | + LOG.error(f'{error_message} {str(e)}') |
| 440 | + return HttpResponse(error_message, status=500) |
390 | 441 |
|
391 | 442 |
|
392 | 443 | @api_error_handler |
393 | 444 | def upload_complete(request): |
394 | | - pass |
| 445 | + """ |
| 446 | + Handles the completion of a file upload. |
| 447 | +
|
| 448 | + Args: |
| 449 | + request (HttpRequest): The HTTP request object. |
| 450 | +
|
| 451 | + Returns: |
| 452 | + JsonResponse: A JSON response containing the result of the upload. |
| 453 | + """ |
| 454 | + try: |
| 455 | + chunks = extract_upload_data(request, "POST") |
| 456 | + response = perform_upload_task(request, **chunks) |
| 457 | + |
| 458 | + return JsonResponse(response) |
| 459 | + except Exception as e: |
| 460 | + error_message = 'Error occurred during chunk file upload completion.' |
| 461 | + LOG.error(f'{error_message} {str(e)}') |
| 462 | + return HttpResponse(error_message, status=500) |
395 | 463 |
|
396 | 464 |
|
397 | 465 | @api_error_handler |
@@ -423,7 +491,7 @@ def upload_file(request): |
423 | 491 | # Check if the file already exists at the destination path |
424 | 492 | filepath = request.fs.join(dest_path, uploaded_file.name) |
425 | 493 | if request.fs.exists(filepath): |
426 | | - # If overwrite is true, attempt to remove the existing file |
| 494 | + # If overwrite is true, attempt to remove the existing file |
427 | 495 | if overwrite: |
428 | 496 | try: |
429 | 497 | request.fs.rmtree(filepath) |
|
0 commit comments