memHandler.js 866 B

123456789101112131415161718192021222324252627282930313233
  1. const crypto = require('crypto');
  2. const {debugLog} = require('./utilities');
  3. /**
  4. * memHandler - In memory upload handler
  5. * @param {Object} options
  6. * @param {String} fieldname
  7. * @param {String} filename
  8. * @returns {Object}
  9. */
  10. module.exports = (options, fieldname, filename) => {
  11. let buffers = [];
  12. let fileSize = 0; // eslint-disable-line
  13. let hash = crypto.createHash('md5');
  14. const getBuffer = () => Buffer.concat(buffers);
  15. const emptyFunc = () => '';
  16. return {
  17. dataHandler: (data) => {
  18. buffers.push(data);
  19. hash.update(data);
  20. fileSize += data.length;
  21. debugLog(options, `Uploading ${fieldname} -> ${filename}, bytes: ${fileSize}`);
  22. },
  23. getBuffer: getBuffer,
  24. getFilePath: emptyFunc,
  25. getFileSize: () => fileSize,
  26. getHash: () => hash.digest('hex'),
  27. complete: getBuffer,
  28. cleanup: emptyFunc
  29. };
  30. };