Sharp: Create folder with `.toFile`?

Created on 4 Dec 2017  路  4Comments  路  Source: lovell/sharp

If the folder doesn't exist sharp throws me an error
Is there's a way to sharp to create such folder if it doesn't exist?

.toFile(`./public/uploads/${'resize-' + req.body.file}`)
Error: vips__file_open_write: unable to open file "./public/uploads/greyscale-67badcb0-8a98-4126-828a-4e9fe152979b.jpeg" for writing
unix error: No such file or directory
question

Most helpful comment

const mkdirp = require('mkdirp-promise');
const sharp = require('sharp');
(async () => {
  await mkdirp('public/uploads');

  await sharp()
    // ...
    .toFile(`./public/uploads/${'resize-' + req.body.file}`);
})();

All 4 comments

Hello, please see #874.

@lovell got it. So you suggest something like this: first check is directory is exist, if not then create it with mkdirp and only after fire up sharp? Can I see basic clean example, because right now it looks little messed for me.

const mkdirp = require('mkdirp-promise');
const sharp = require('sharp');
(async () => {
  await mkdirp('public/uploads');

  await sharp()
    // ...
    .toFile(`./public/uploads/${'resize-' + req.body.file}`);
})();

@papandreou thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AVVS picture AVVS  路  3Comments

OleVik picture OleVik  路  3Comments

sansroman picture sansroman  路  3Comments

terbooter picture terbooter  路  3Comments

paulieo10 picture paulieo10  路  3Comments