Update ZIP exports to use username for filename and internal folders
This commit is contained in:
@@ -335,7 +335,10 @@ export default function AdminUserDetail() {
|
|||||||
const downloadUrl = window.URL.createObjectURL(blob);
|
const downloadUrl = window.URL.createObjectURL(blob);
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
a.href = downloadUrl;
|
a.href = downloadUrl;
|
||||||
a.download = `user_${userId}_documents.zip`;
|
|
||||||
|
const safeName = (userDetail?.name || userDetail?.email?.split('@')[0] || `user_${userId}`).replace(/[^a-zA-Z0-9_\- ]/g, '_').trim();
|
||||||
|
a.download = `${safeName}_documents_${taxYear ? taxYear : 'all'}.zip`;
|
||||||
|
|
||||||
document.body.appendChild(a);
|
document.body.appendChild(a);
|
||||||
a.click();
|
a.click();
|
||||||
a.remove();
|
a.remove();
|
||||||
|
|||||||
@@ -1203,6 +1203,10 @@ def export_user_documents_excel(
|
|||||||
return "'" + s
|
return "'" + s
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
import re
|
||||||
|
raw_name = user.name or user.email.split('@')[0]
|
||||||
|
safe_user_name = re.sub(r'[^a-zA-Z0-9_\- ]', '_', raw_name).strip()
|
||||||
|
|
||||||
zip_buffer = io.BytesIO()
|
zip_buffer = io.BytesIO()
|
||||||
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
|
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
|
||||||
for docs, prefix in [(personal_docs, "Personal"), (business_docs, "Business")]:
|
for docs, prefix in [(personal_docs, "Personal"), (business_docs, "Business")]:
|
||||||
@@ -1249,12 +1253,12 @@ def export_user_documents_excel(
|
|||||||
excel_buffer.seek(0)
|
excel_buffer.seek(0)
|
||||||
|
|
||||||
safe_filename = d.filename.replace("/", "_").replace("\\", "_")
|
safe_filename = d.filename.replace("/", "_").replace("\\", "_")
|
||||||
file_name = f"{prefix}_{d.id}_{safe_filename}.xlsx"
|
file_name = f"{safe_user_name}/{prefix}_{d.id}_{safe_filename}.xlsx"
|
||||||
zip_file.writestr(file_name, excel_buffer.read())
|
zip_file.writestr(file_name, excel_buffer.read())
|
||||||
|
|
||||||
zip_buffer.seek(0)
|
zip_buffer.seek(0)
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Disposition": f'attachment; filename="user_{user_id}_excel_summaries.zip"',
|
"Content-Disposition": f'attachment; filename="{safe_user_name}_excel_summaries.zip"',
|
||||||
"Content-Type": "application/zip",
|
"Content-Type": "application/zip",
|
||||||
}
|
}
|
||||||
return StreamingResponse(zip_buffer, headers=headers)
|
return StreamingResponse(zip_buffer, headers=headers)
|
||||||
@@ -1295,6 +1299,10 @@ def export_user_documents_zip(
|
|||||||
|
|
||||||
import zipfile
|
import zipfile
|
||||||
import io
|
import io
|
||||||
|
import re
|
||||||
|
|
||||||
|
raw_name = target_user.name or target_user.email.split('@')[0]
|
||||||
|
safe_user_name = re.sub(r'[^a-zA-Z0-9_\- ]', '_', raw_name).strip()
|
||||||
|
|
||||||
zip_buffer = io.BytesIO()
|
zip_buffer = io.BytesIO()
|
||||||
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
|
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
|
||||||
@@ -1308,11 +1316,11 @@ def export_user_documents_zip(
|
|||||||
if file_path.exists():
|
if file_path.exists():
|
||||||
# Make sure filename is safe and unique
|
# Make sure filename is safe and unique
|
||||||
filename = doc.filename or "document"
|
filename = doc.filename or "document"
|
||||||
zip_file.write(file_path, f"{prefix}/{doc.id}_{filename}")
|
zip_file.write(file_path, f"{safe_user_name}/{prefix}/{doc.id}_{filename}")
|
||||||
|
|
||||||
zip_buffer.seek(0)
|
zip_buffer.seek(0)
|
||||||
|
|
||||||
filename = f"user_{user_id}_documents_{tax_year if tax_year else 'all'}.zip"
|
filename = f"{safe_user_name}_documents_{tax_year if tax_year else 'all'}.zip"
|
||||||
|
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
zip_buffer,
|
zip_buffer,
|
||||||
|
|||||||
Reference in New Issue
Block a user