Guest User

Untitled

a guest
Feb 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. import crypto from 'crypto';
  2. import multer from 'multer';
  3.  
  4. const storage = multer.diskStorage({
  5. destination(req, file, cb) {
  6. cb(null, './uploads/');
  7. },
  8. filename(req, file, cb) {
  9. crypto.pseudoRandomBytes(16, (err, raw) => {
  10. const extension = file.mimetype.split('/')[1];
  11.  
  12. if (extension === 'jpeg' || extension === 'png') {
  13. cb(null, raw.toString('hex') +
  14. Date.now() +
  15. '.' +
  16. extension);
  17. } else {
  18. cb(new Error('not an image'));
  19. }
  20. });
  21. }
  22. });
  23.  
  24. export default storage;
Add Comment
Please, Sign In to add comment