-# 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.
# 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'),
-# 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.
# limitations under the License.
import logging
+import os
import traceback
from drf_yasg.utils import no_body, swagger_auto_schema
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 \
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)