Skip to content

Commit 5798085

Browse files
committed
feat: stream zip
1 parent 26c5a94 commit 5798085

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

web/components/pdfs/ApplicationDocument.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Document, Font, Page, Text, View } from '@react-pdf/renderer'
1+
import { Document, Page, Text, View } from '@react-pdf/renderer'
22
import ApplicationCreation from '~components/pdfs/ApplicationCreation'
33

44
import ApplicationHeader from '~components/pdfs/ApplicationHeader'
@@ -28,8 +28,9 @@ const ApplicationDocument = ({ application }: { application: Application }) => {
2828
<View style={styles.container}>
2929
{creations?.map((reference, i) => (
3030
<ApplicationReference
31-
reference={reference}
31+
key={i}
3232
index={i}
33+
reference={reference}
3334
referencesTotal={creations.length}
3435
/>
3536
))}

web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"swagger-typescript-api": "^7.0.1",
6666
"swiper": "^6.5.3",
6767
"tsconfig-paths": "^3.9.0",
68+
"yazl": "^3.3.1",
6869
"yup": "^0.32.9"
6970
},
7071
"devDependencies": {

web/pages/api/pdfs/campaign/[id].tsx

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @ts-nocheck
22
import { renderToStream } from '@react-pdf/renderer'
3-
import AdmZip from 'adm-zip'
43
import crypto from 'crypto'
54
import ExcelJS from 'exceljs'
65
import { getSession } from 'next-auth/client'
6+
import yazl from 'yazl'
77
import { client } from '~api/client-api'
88
import ApplicationDocument from '~components/pdfs/ApplicationDocument'
99
import { Application, Espace, UsersPermissionsUser } from '~typings/api'
@@ -375,7 +375,7 @@ const SelectedCampaignApplications = async (req, res) => {
375375
return
376376
}
377377

378-
const zip = new AdmZip()
378+
const zip = new yazl.ZipFile()
379379
const { data: campaign } = await client.campaigns.campaignsDetail(campaignId)
380380

381381
try {
@@ -451,19 +451,20 @@ const SelectedCampaignApplications = async (req, res) => {
451451
const subFolder = `${application?.espace?.name} - ${disponibilityLabel} - ${refLabel} - ${structureName}`
452452

453453
const streamBuffer = await getBufferFromStream(stream)
454-
await zip.addFile(
454+
await zip.addBuffer(
455+
Buffer.from(streamBuffer),
455456
`${name}/${subFolder}/${refLabel} - Candidature.pdf`,
456-
streamBuffer,
457457
)
458458

459459
if (application?.creation_file?.[0]?.url) {
460+
console.log(application?.creation_file?.[0]?.url)
460461
const creationFile = await fetch(application?.creation_file?.[0]?.url)
461462
// @ts-ignore
462463
const creationFileArrayBuffer = await creationFile.buffer()
463464

464-
await zip.addFile(
465+
await zip.addBuffer(
466+
Buffer.from(creationFileArrayBuffer),
465467
`${name}/${subFolder}/${refLabel} - Dossier artistique.pdf`,
466-
creationFileArrayBuffer,
467468
)
468469
}
469470
}
@@ -478,13 +479,16 @@ const SelectedCampaignApplications = async (req, res) => {
478479
withAllApplications,
479480
)
480481

481-
await zip.addFile(
482-
`${campaign?.title} candidatures.xlsx`,
482+
console.log('Adding candidatures.xlsx')
483+
await zip.addBuffer(
483484
applicationsSpreadsheetBuffer,
485+
`${campaign?.title} candidatures.xlsx`,
484486
)
485-
await zip.addFile(
486-
`${campaign?.title} récap ${all ? 'complet' : 'présélection'}.xlsx`,
487+
488+
console.log('Adding récap.xlsx')
489+
await zip.addBuffer(
487490
summarySpreadsheetBuffer,
491+
`${campaign?.title} récap ${all ? 'complet' : 'présélection'}.xlsx`,
488492
)
489493
} catch (error) {
490494
console.error(error)
@@ -494,14 +498,22 @@ const SelectedCampaignApplications = async (req, res) => {
494498
return
495499
}
496500

497-
const zipBuffer = zip.toBuffer()
501+
console.log('Adding zip')
498502

499503
res.setHeader('Content-Type', 'application/zip')
500504
res.setHeader(
501505
'Content-Disposition',
502506
'attachment; filename=' + formatCampaignZipName(campaign),
503507
)
504-
res.send(zipBuffer)
508+
509+
zip.end()
510+
511+
const zipStream = zip.outputStream
512+
zipStream.pipe(res)
513+
514+
return new Promise((resolve) => {
515+
zipStream.on('end', resolve)
516+
})
505517
}
506518

507519
export default SelectedCampaignApplications

web/yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,6 +2885,11 @@ buffer-crc32@^0.2.1, buffer-crc32@^0.2.13:
28852885
resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242"
28862886
integrity sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==
28872887

2888+
buffer-crc32@^1.0.0:
2889+
version "1.0.0"
2890+
resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-1.0.0.tgz#a10993b9055081d55304bd9feb4a072de179f405"
2891+
integrity sha512-Db1SbgBS/fg/392AblrMJk97KggmvYhr4pB5ZIMTWtaivCPMWLkmb7m21cJvpvgK+J3nsU2CmmixNBZx4vFj/w==
2892+
28882893
28892894
version "1.0.1"
28902895
resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819"
@@ -7210,6 +7215,13 @@ yargs@^16.0.0, yargs@^16.0.3, yargs@^16.1.1:
72107215
y18n "^5.0.5"
72117216
yargs-parser "^20.2.2"
72127217

7218+
yazl@^3.3.1:
7219+
version "3.3.1"
7220+
resolved "https://registry.yarnpkg.com/yazl/-/yazl-3.3.1.tgz#a69abad02d80739d3b1a7ffcca8434422477432c"
7221+
integrity sha512-BbETDVWG+VcMUle37k5Fqp//7SDOK2/1+T7X8TD96M3D9G8jK5VLUdQVdVjGi8im7FGkazX7kk5hkU8X4L5Bng==
7222+
dependencies:
7223+
buffer-crc32 "^1.0.0"
7224+
72137225
yup@^0.32.9:
72147226
version "0.32.9"
72157227
resolved "https://registry.yarnpkg.com/yup/-/yup-0.32.9.tgz#9367bec6b1b0e39211ecbca598702e106019d872"

0 commit comments

Comments
 (0)