|
1 | 1 | // @ts-ignore |
2 | 2 | import { renderToStream } from '@react-pdf/renderer' |
3 | | -import AdmZip from 'adm-zip' |
4 | 3 | import { getSession } from 'next-auth/client' |
| 4 | +import yazl from 'yazl' |
5 | 5 | import { client } from '~api/client-api' |
6 | 6 | import ApplicationDocument from '~components/pdfs/ApplicationDocument' |
7 | 7 | import { formatDisponibilityZipName, getBufferFromStream } from '~utils/pdf' |
@@ -29,38 +29,48 @@ const MultipleApplication = async (req, res) => { |
29 | 29 | ) |
30 | 30 |
|
31 | 31 | const disponibility = applications?.[0]?.disponibility |
32 | | - const zip = new AdmZip() |
| 32 | + const zip = new yazl.ZipFile() |
33 | 33 |
|
34 | 34 | for (const application of applications) { |
35 | 35 | const refLabel = `Ref. ${application.id}` |
36 | 36 | const name = `${refLabel} - ${application.company?.structureName}` |
37 | 37 | const stream = await renderToStream( |
38 | 38 | <ApplicationDocument application={application} />, |
39 | 39 | ) |
| 40 | + |
40 | 41 | const streamBuffer = await getBufferFromStream(stream) |
41 | | - await zip.addFile(`${name}/${refLabel} - Candidature.pdf`, streamBuffer) |
| 42 | + await zip.addBuffer( |
| 43 | + Buffer.from(streamBuffer), |
| 44 | + `${name}/${refLabel} - Candidature.pdf`, |
| 45 | + ) |
42 | 46 |
|
43 | 47 | if (application?.creation_file?.[0]?.url) { |
44 | 48 | const creationFile = await fetch(application?.creation_file?.[0]?.url) |
45 | 49 |
|
46 | 50 | // @ts-ignore |
47 | 51 | const creationFileArrayBuffer = await creationFile.buffer() |
48 | | - await zip.addFile( |
| 52 | + await zip.addBuffer( |
| 53 | + Buffer.from(creationFileArrayBuffer), |
49 | 54 | `${name}/${refLabel} - Dossier artistique.pdf`, |
50 | | - creationFileArrayBuffer, |
51 | 55 | ) |
52 | 56 | } |
53 | 57 | } |
54 | 58 |
|
55 | | - const zipBuffer = zip.toBuffer() |
56 | | - |
57 | 59 | res.setHeader('Content-Type', 'application/zip') |
58 | 60 | res.setHeader( |
59 | 61 | 'Content-Disposition', |
60 | 62 | // @ts-expect-error |
61 | 63 | 'attachment; filename=' + formatDisponibilityZipName(disponibility), |
62 | 64 | ) |
63 | | - res.send(zipBuffer) |
| 65 | + |
| 66 | + zip.end() |
| 67 | + |
| 68 | + const zipStream = zip.outputStream |
| 69 | + zipStream.pipe(res) |
| 70 | + |
| 71 | + return new Promise((resolve) => { |
| 72 | + zipStream.on('end', resolve) |
| 73 | + }) |
64 | 74 | } |
65 | 75 |
|
66 | 76 | export default MultipleApplication |
0 commit comments