Skip to content

Concurrent file generation #1

@federicosan

Description

@federicosan

Not sure because I haven't tested this yet, but it seems that this may get some bugs with high concurrency, because files are always being generated with the same name, please tell me if I am wrong. Maybe a way to fix this intuitive problem is to generate a random filename on creation, send to before fetching. Please tell me what do you think about this, have you tested the concurrency on this?

 createAndDownloadPdf = () => {
    axios.post('/create-pdf', this.state)
      .then((filename) => axios.get('fetch-pdf', { responseType: 'blob', filename}))
      .then((res) => {
        const pdfBlob = new Blob([res.data], { type: 'application/pdf' });

        saveAs(pdfBlob, 'newPdf.pdf');
      })
  }


app.post('/create-pdf', (req, res) => {
    const rndFilename = randomfilename(); /* you can implement this as you wish maybe using a your favourite hashfuncion*/
    pdf.create(pdfTemplate(req.body), {}).toFile( rndFilename  , (err, rndFilename ) => {
        if(err) {
            res.send(Promise.reject());
        }

        res.send(Promise.resolve(rndFilename));
    });
});

app.get('/fetch-pdf', (req, res) => {
    res.sendFile(`${__dirname}/${res.filename}`)
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions