Skip to content

Commit

Permalink
Experiment streaming for file download to handle the random crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-smith committed Dec 8, 2024
1 parent c840235 commit f185a29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
6 changes: 4 additions & 2 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from pathlib import Path
import humanize

from django.http import StreamingHttpResponse
from wsgiref.util import FileWrapper

import io

Expand Down Expand Up @@ -2176,8 +2178,8 @@ def word_report(self, request, pk):
doc.save(buffer_doc)
buffer_doc.seek(0)

response = HttpResponse(
buffer_doc.getvalue(),
response = StreamingHttpResponse(
FileWrapper(buffer_doc),
content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
)
response["Content-Disposition"] = "attachment; filename=sales_report.docx"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import { BASE_API_URL } from '$lib/utils/constants';
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export const GET: RequestHandler = async ({ fetch, params }) => {
const URLModel = 'compliance-assessments';
const endpoint = `${BASE_API_URL}/${URLModel}/${params.id}/word_report/`;
const URLModel = 'compliance-assessments';
const endpoint = `${BASE_API_URL}/${URLModel}/${params.id}/word_report/`;

const res = await fetch(endpoint);
if (!res.ok) {
error(400, 'Error fetching the Word file');
}
const res = await fetch(endpoint);
if (!res.ok) {
error(400, 'Error fetching the Word file');
}

const fileName = `audit-exec-summary-${new Date().toISOString()}.docx`;
const fileName = `audit-exec-summary-${new Date().toISOString()}.docx`;

return new Response(await res.blob(), {
headers: {
'Content-Type': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'Content-Disposition': `attachment; filename="${fileName}"`
}
});
return new Response(res.body, {
headers: {
'Content-Type': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'Content-Disposition': `attachment; filename="${fileName}"`,
'Transfer-Encoding': 'chunked'
}
});
};

0 comments on commit f185a29

Please sign in to comment.