Skip to content

Commit

Permalink
fix: structure zip (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannNumericite authored Jun 20, 2022
1 parent 12c33ea commit 6d8e4aa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/components/Commission.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const Commission: React.FC<Props> = ({ commission }) => {
const docName = `commission_${frenchDepartementName(
commission.departement
)}_${frenchDateText(commission.date)}`;
const urlDoc = `/api/docs?zipname=${docName}`;
const urlDoc = `/api/docs?zipname=${docName.replace(/[\W]+/g, "_")}`;
return (
<div className="card">
<div className={styles.commissionHeader}>
Expand Down
26 changes: 13 additions & 13 deletions src/lib/generateZip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ import type { CommissionData, DossierData } from "./types";
export default (commission: CommissionData) => {
const zip = new JSZip();

const fileName = `commission_${frenchDepartementName(
commission.departement
)}_${new Date(commission.date).toLocaleDateString("fr-FR", {
day: "numeric",
month: "long",
year: "numeric",
})}`.replace(/[\W]+/g, "_");

const zipFolder = zip.folder(fileName);

_.forEach(
commission.dossiers,
(dossier: DossierData & { files: unknown[] }) => {
const dossierFolder = zip.folder(
const dossierFolder = zipFolder?.folder(
dossier.nom
.slice(0, 75)
.normalize("NFD")
Expand Down Expand Up @@ -174,21 +184,11 @@ Rémunération totale: ${enfant.remunerationTotale}

zip
.generateNodeStream({ streamFiles: true, type: "nodebuffer" })
.pipe(
fs.createWriteStream(
`/mnt/docs/commission_${frenchDepartementName(
commission.departement
)}_${new Date(commission.date).toLocaleDateString("fr-FR", {
day: "numeric",
month: "long",
year: "numeric",
})}.zip`
)
)
.pipe(fs.createWriteStream(`/mnt/docs/${fileName}.zip`))
.on("finish", function () {
// JSZip generates a readable stream with a "end" event,
// but is piped here in a writable stream which emits a "finish" event.
console.log("commission.zip written.");
console.log(fileName, " written.");
});

/*const zipAsBase64 = await zip.generateAsync({ type: "base64" });*/
Expand Down
1 change: 1 addition & 0 deletions src/pages/api/docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const handler: NextApiHandler = async (req, res) => {

const downloadZip: NextApiHandler = async (req, res) => {
const zipname = req.query.zipname;
console.log("zipname : ", zipname);

const filePath = `/mnt/docs/${zipname}.zip`;
const stat = fs.statSync(filePath);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Download: React.FC = () => {
day: "numeric",
month: "long",
year: "numeric",
})}`
})}`.replace(/[\W]+/g, "_")
);
setUrlDoc(
`/api/docs?zipname=${`commission_${frenchDepartementName(
Expand All @@ -50,7 +50,7 @@ const Download: React.FC = () => {
day: "numeric",
month: "long",
year: "numeric",
})}`}`
})}`.replace(/[\W]+/g, "_")}`
);
})
.catch((e) => {
Expand Down

0 comments on commit 6d8e4aa

Please sign in to comment.