IO-3281 Adjust zip to stream.
This commit is contained in:
@@ -56,22 +56,25 @@ export function JobsDocumentsImgproxyDownloadButton({ bodyshop, galleryImages, i
|
|||||||
const handleDownload = async () => {
|
const handleDownload = async () => {
|
||||||
logImEXEvent("jobs_documents_download");
|
logImEXEvent("jobs_documents_download");
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const zipUrl = await axios({
|
try {
|
||||||
url: "/media/imgproxy/download",
|
const response = await axios({
|
||||||
method: "POST",
|
url: "/media/imgproxy/download",
|
||||||
data: { jobId, documentids: imagesToDownload.map((_) => _.id) }
|
method: "POST",
|
||||||
});
|
responseType: "blob",
|
||||||
|
data: { jobId, documentids: imagesToDownload.map((_) => _.id) },
|
||||||
|
onDownloadProgress: downloadProgress
|
||||||
|
});
|
||||||
|
|
||||||
const theDownloadedZip = await cleanAxios({
|
setLoading(false);
|
||||||
url: zipUrl.data.url,
|
setDownload(null);
|
||||||
method: "GET",
|
|
||||||
responseType: "arraybuffer",
|
|
||||||
onDownloadProgress: downloadProgress
|
|
||||||
});
|
|
||||||
setLoading(false);
|
|
||||||
setDownload(null);
|
|
||||||
|
|
||||||
standardMediaDownload(theDownloadedZip.data);
|
// Use the response data (Blob) to trigger download
|
||||||
|
standardMediaDownload(response.data);
|
||||||
|
} catch (error) {
|
||||||
|
setLoading(false);
|
||||||
|
setDownload(null);
|
||||||
|
// handle error (optional)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -383,7 +383,7 @@ export function ShopEmployeesFormComponent({ bodyshop }) {
|
|||||||
title={() => <ShopEmployeeAddVacation employee={data && data.employees_by_pk} />}
|
title={() => <ShopEmployeeAddVacation employee={data && data.employees_by_pk} />}
|
||||||
columns={columns}
|
columns={columns}
|
||||||
rowKey={"id"}
|
rowKey={"id"}
|
||||||
dataSource={data ? data.employees_by_pk.employee_vacations : []}
|
dataSource={data?.employees_by_pk?.employee_vacations ?? []}
|
||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -180,10 +180,14 @@ const downloadFiles = async (req, res) => {
|
|||||||
|
|
||||||
const s3client = new S3Client({ region: InstanceRegion() });
|
const s3client = new S3Client({ region: InstanceRegion() });
|
||||||
const zipfile = new yazl.ZipFile();
|
const zipfile = new yazl.ZipFile();
|
||||||
const passThrough = new stream.PassThrough();
|
|
||||||
|
|
||||||
// Pipe the zipfile output to the passThrough stream
|
// Set response headers for zip download
|
||||||
zipfile.outputStream.pipe(passThrough);
|
const filename = `archive-${jobId || "na"}-${new Date().toISOString().replace(/[:.]/g, "-")}.zip`;
|
||||||
|
res.setHeader("Content-Type", "application/zip");
|
||||||
|
res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
|
||||||
|
|
||||||
|
// Pipe the zipfile output directly to the response
|
||||||
|
zipfile.outputStream.pipe(res);
|
||||||
|
|
||||||
// Add each file to the zip as a stream
|
// Add each file to the zip as a stream
|
||||||
for (const doc of data.documents) {
|
for (const doc of data.documents) {
|
||||||
@@ -200,27 +204,8 @@ const downloadFiles = async (req, res) => {
|
|||||||
|
|
||||||
// Finalize the zip after all files are added
|
// Finalize the zip after all files are added
|
||||||
zipfile.end();
|
zipfile.end();
|
||||||
|
// No need to send a JSON response, as the zip is streamed directly
|
||||||
|
|
||||||
const archiveKey = `archives/${jobId || "na"}/archive-${new Date().toISOString()}.zip`;
|
|
||||||
|
|
||||||
// Upload the zip stream to S3
|
|
||||||
const parallelUploads3 = new Upload({
|
|
||||||
client: s3client,
|
|
||||||
queueSize: 4,
|
|
||||||
leavePartsOnError: false,
|
|
||||||
params: { Bucket: imgproxyDestinationBucket, Key: archiveKey, Body: passThrough }
|
|
||||||
});
|
|
||||||
|
|
||||||
await parallelUploads3.done();
|
|
||||||
|
|
||||||
// Generate the presigned URL to download it.
|
|
||||||
const presignedUrl = await getSignedUrl(
|
|
||||||
s3client,
|
|
||||||
new GetObjectCommand({ Bucket: imgproxyDestinationBucket, Key: archiveKey }),
|
|
||||||
{ expiresIn: 360 }
|
|
||||||
);
|
|
||||||
|
|
||||||
return res.json({ success: true, url: presignedUrl });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.log("imgproxy-download-error", "ERROR", req.user?.email, jobId, {
|
logger.log("imgproxy-download-error", "ERROR", req.user?.email, jobId, {
|
||||||
jobId,
|
jobId,
|
||||||
|
|||||||
Reference in New Issue
Block a user