Skip to content

Commit d16de72

Browse files
authored
[api] Port chunk file upload related APIs to public (#3921)
1 parent a2ddc9b commit d16de72

File tree

1 file changed

+72
-4
lines changed
  • apps/filebrowser/src/filebrowser

1 file changed

+72
-4
lines changed

apps/filebrowser/src/filebrowser/api.py

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import posixpath
2424
from io import BytesIO as string_io
2525

26+
from django.core.files.uploadhandler import StopUpload
2627
from django.core.paginator import EmptyPage, Paginator
2728
from django.http import HttpResponse, HttpResponseNotModified, HttpResponseRedirect, StreamingHttpResponse
2829
from django.utils.http import http_date
@@ -50,7 +51,6 @@
5051
RESTRICT_FILE_EXTENSIONS,
5152
SHOW_DOWNLOAD_BUTTON,
5253
)
53-
from filebrowser.lib import xxd
5454
from filebrowser.lib.rwx import compress_mode, filetype, rwx
5555
from filebrowser.utils import parse_broker_url
5656
from filebrowser.views import (
@@ -60,6 +60,8 @@
6060
_is_hdfs_superuser,
6161
_massage_page,
6262
_normalize_path,
63+
extract_upload_data,
64+
perform_upload_task,
6365
read_contents,
6466
stat_absolute_path,
6567
)
@@ -386,12 +388,78 @@ def stat(request):
386388

387389
@api_error_handler
388390
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)
390441

391442

392443
@api_error_handler
393444
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)
395463

396464

397465
@api_error_handler
@@ -423,7 +491,7 @@ def upload_file(request):
423491
# Check if the file already exists at the destination path
424492
filepath = request.fs.join(dest_path, uploaded_file.name)
425493
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
427495
if overwrite:
428496
try:
429497
request.fs.rmtree(filepath)

0 commit comments

Comments
 (0)