You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ma...@apache.org on 2019/10/07 16:43:16 UTC

[airavata-django-portal] 02/03: AIRAVATA-3081 Factor out common tus upload handling code

This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git

commit d7b576e9a4091b51a858e9c11a38629ae7398180
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Wed Oct 2 15:37:25 2019 -0400

    AIRAVATA-3081 Factor out common tus upload handling code
---
 django_airavata/apps/api/tus.py   | 31 +++++++++++++++++++++++++++++
 django_airavata/apps/api/views.py | 41 +++++++++++----------------------------
 2 files changed, 42 insertions(+), 30 deletions(-)

diff --git a/django_airavata/apps/api/tus.py b/django_airavata/apps/api/tus.py
new file mode 100644
index 0000000..c9cf64a
--- /dev/null
+++ b/django_airavata/apps/api/tus.py
@@ -0,0 +1,31 @@
+import json
+import logging
+import os
+from urllib.parse import urlparse
+
+from django.conf import settings
+
+logger = logging.getLogger(__name__)
+
+
+def move_tus_upload(upload_url, move_function):
+    """
+    Move upload identified by upload_url using the provided move_function.
+
+    move_function signature should be (file_path, file_name). It's
+    return value will be returned.
+    """
+    # file UUID is last path component in URL. For example:
+    # http://localhost:1080/files/2c44415fdb6259a22f425145b87d0840
+    upload_uuid = urlparse(upload_url).path.split("/")[-1]
+    upload_bin_path = os.path.join(settings.TUS_DATA_DIR,
+                                   f"{upload_uuid}.bin")
+    logger.debug(f"upload_bin_path={upload_bin_path}")
+    upload_info_path = os.path.join(settings.TUS_DATA_DIR,
+                                    f"{upload_uuid}.info")
+    with open(upload_info_path) as upload_info_file:
+        upload_info = json.load(upload_info_file)
+        filename = upload_info['MetaData']['filename']
+        result = move_function(upload_bin_path, filename)
+    os.remove(upload_info_path)
+    return result
diff --git a/django_airavata/apps/api/views.py b/django_airavata/apps/api/views.py
index 2e35642..dccf505 100644
--- a/django_airavata/apps/api/views.py
+++ b/django_airavata/apps/api/views.py
@@ -2,7 +2,6 @@ import json
 import logging
 import os
 from datetime import datetime, timedelta
-from urllib.parse import urlparse
 
 from django.conf import settings
 from django.contrib.auth.decorators import login_required
@@ -52,6 +51,7 @@ from . import (
     output_views,
     serializers,
     thrift_utils,
+    tus,
     view_utils
 )
 
@@ -912,20 +912,11 @@ def upload_input_file(request):
 def tus_upload_finish(request):
     log.debug("POST={}".format(request.POST))
     uploadURL = request.POST['uploadURL']
-    # file UUID is last path component in URL. For example:
-    # http://localhost:1080/files/2c44415fdb6259a22f425145b87d0840
-    upload_uuid = urlparse(uploadURL).path.split("/")[-1]
-    upload_bin_path = os.path.join(settings.TUS_DATA_DIR, f"{upload_uuid}.bin")
-    log.debug(f"upload_bin_path={upload_bin_path}")
-    upload_info_path = os.path.join(settings.TUS_DATA_DIR,
-                                    f"{upload_uuid}.info")
-    with open(upload_info_path) as upload_info_file:
-        upload_info = json.load(upload_info_file)
-        filename = upload_info['MetaData']['filename']
-        data_product = data_products_helper\
-            .move_input_file_upload_from_filepath(
-                request, upload_bin_path, name=filename)
-    os.remove(upload_info_path)
+
+    def move_input_file(file_path, file_name):
+        return data_products_helper.move_input_file_upload_from_filepath(
+                request, file_path, name=file_name)
+    data_product = tus.move_tus_upload(uploadURL, move_input_file)
     serializer = serializers.DataProductSerializer(
         data_product, context={'request': request})
     return JsonResponse({'uploaded': True,
@@ -1462,22 +1453,12 @@ class UserStoragePathView(APIView):
                 request, path, user_file)
         # Handle a tus upload
         elif 'uploadURL' in request.POST:
-            # TODO: factor out tus uploadURL parsing, retrieval
             uploadURL = request.POST['uploadURL']
-            # file UUID is last path component in URL. For example:
-            # http://localhost:1080/files/2c44415fdb6259a22f425145b87d0840
-            upload_uuid = urlparse(uploadURL).path.split("/")[-1]
-            upload_bin_path = os.path.join(settings.TUS_DATA_DIR,
-                                           f"{upload_uuid}.bin")
-            log.debug(f"upload_bin_path={upload_bin_path}")
-            upload_info_path = os.path.join(settings.TUS_DATA_DIR,
-                                            f"{upload_uuid}.info")
-            with open(upload_info_path) as upload_info_file:
-                upload_info = json.load(upload_info_file)
-                filename = upload_info['MetaData']['filename']
-                data_product = data_products_helper.move_from_filepath(
-                    request, upload_bin_path, path, name=filename)
-            os.remove(upload_info_path)
+
+            def move_file(file_path, file_name):
+                return data_products_helper.move_from_filepath(
+                        request, file_path, path, name=file_name)
+            data_product = tus.move_tus_upload(uploadURL, move_file)
         return self._create_response(request, path, uploaded=data_product)
 
     def delete(self, request, path="/", format=None):