So I have users requesting their files from a django server inside which some processes take place on the requested file. Currently I am writing the file to the disk and serve it from there.
Is there anyway to serve the file directly to the user from the memory for a better efficiency and performance (using x-sendfile)?
Here is my current view:
def ServeSegment(request, segmentID):
segments =[]
obj = MainFile.objects.filter(owner=request.user)
file_name = MainFile.objects.get(file_id=segmentID).file_name
file_suffix = file_name = MainFile.objects.get(file_id=segmentID).file_suffix
if request.method == 'GET':
hosts = settings.HOSTS
for i in hosts:
try:
url = 'http://' + i + ':8000/foo/'+str(segmentID)
r = requests.get(url, timeout=1, stream=True)
if r.status_code == 200:
segments.append(r.content)
except:
continue
instance = SeIDA(filename='test', x=settings.M, y=settings.N)
docfile = instance.decoder(segments)
with open('/tmp/Files/'+file_name, 'wb') as f:
f.write(docfile)
response = HttpResponse()
response['Content-Disposition'] = 'attachment; filename={0}'.format(file_name)
response['X-Sendfile'] = "/tmp/Files/{0}".format(file_name)
return response
Note: SeIDA module being used, encodes data into N segments such that M segments is sufficed to construct the data. So in the view above I am retrieving the segments from the storage servers and combining them.
My question being: How can I directly serve the docfile without saving it. Is there anyway?