Ns descriptor related stuffs. 89/62989/1
authorlaili <lai.li@zte.com.cn>
Mon, 27 Aug 2018 07:12:21 +0000 (15:12 +0800)
committerlaili <lai.li@zte.com.cn>
Mon, 27 Aug 2018 07:12:33 +0000 (15:12 +0800)
Modify urlpattern.
Implement the view of fetch partial nsd.

Change-Id: I0a181b1b507eb32bcddc3edce7f028172a0c52bd
Issue-ID: VFC-1037
Signed-off-by: laili <lai.li@zte.com.cn>
catalog/packages/urls.py
catalog/packages/views/ns_descriptor_views.py

index d6589b5..861f0ca 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2017 ZTE Corporation.
+# Copyright 2017-2018 ZTE Corporation.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -33,10 +33,10 @@ urlpatterns = [
 
     # PNF
     url(r'^api/nsd/v1/pnf_descriptors$', pnf_descriptor_views.pnf_descriptors_rc, name='pnf_descriptors_rc'),
+    url(r'^api/nsd/v1/pnf_descriptors/(?P<pnfdInfoId>[0-9a-zA-Z\-\_]+)$', pnf_descriptor_views.pnfd_info_rd, name='pnfd_info_rd'),
     url(r'^api/nsd/v1/pnf_descriptors/(?P<pnfdInfoId>[0-9a-zA-Z\-\_]+)/pnfd_content$', pnf_descriptor_views.pnfd_content_ru, name='pnfd_content_ru'),
     # TODO SOL005 & SOL003
 
-    url(r'^api/nsd/v1/pnf_descriptors/(?P<pnfdInfoId>[0-9a-zA-Z\-\_]+)$', pnf_descriptor_views.pnfd_info_rd, name='pnfd_info_rd'),
 
     # url(r'^api/nsd/v1/subscriptions', nsd_subscriptions.as_view(), name='subscriptions_rc'),
     # url(r'^api/nsd/v1/subscriptions/(?P<subscriptionId>[0-9a-zA-Z\-\_]+)$', nsd_subscription.as_view(), name='subscription_rd'),
index 9ba5538..86785ac 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2017 ZTE Corporation.
+# Copyright 2018 ZTE Corporation.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 import logging
+import os
 import traceback
 
 from drf_yasg.utils import no_body, swagger_auto_schema
@@ -20,6 +21,7 @@ from rest_framework import status
 from rest_framework.decorators import api_view
 from rest_framework.response import Response
 from django.http import FileResponse
+from django.http import StreamingHttpResponse
 
 from catalog.packages.biz.ns_descriptor import create, query_multiple, query_single, delete_single, upload, download
 from catalog.packages.serializers.create_nsd_info_request import \
@@ -162,11 +164,35 @@ def nsd_content_ru(request, *args, **kwargs):
             file_path = download(nsd_info_id)
             file_name = file_path.split('/')[-1]
             file_name = file_name.split('\\')[-1]
-            response = FileResponse(open(file_path, 'rb'), status=status.HTTP_200_OK)
+
+            file_range = request.META.get('RANGE')
+            if file_range:
+                [start, end] = file_range.split('-')
+                start, end = start.strip(), end.strip()
+                start, end = int(start), int(end)
+                response = StreamingHttpResponse(
+                    read_partial_file(file_path, start, end),
+                    status=status.HTTP_200_OK
+                )
+                response['Content-Range'] = file_range
+            else:
+                response = FileResponse(open(file_path, 'rb'), status=status.HTTP_200_OK)
             response['Content-Disposition'] = 'attachment; filename=%s' % file_name.encode('utf-8')
+            response['Content-Length'] = os.path.getsize(file_path)
             return response
         except IOError:
             logger.error(traceback.format_exc())
             raise CatalogException
             return Response(data={'error': 'Downloading nsd content failed.'},
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+
+def read_partial_file(file_path, start, end):
+    fp = open(file_path, 'rb')
+    fp.seek(start)
+    pos = start
+    CHUNK_SIZE = 1024 * 8
+    while pos + CHUNK_SIZE < end:
+        yield fp.read(CHUNK_SIZE)
+        pos = fp.tell()
+    yield fp.read(end - pos)