index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. const webkitGetAsEntryApi = require("./utils/webkitGetAsEntryApi/index.js");
  3. const fallbackApi = require("./utils/fallbackApi.js");
  4. /**
  5. * Returns a promise that resolves to the array of dropped files (if a folder is
  6. * dropped, and browser supports folder parsing - promise resolves to the flat
  7. * array of all files in all directories).
  8. * Each file has .relativePath prop appended to it (e.g. "/docs/Prague/ticket_from_prague_to_ufa.pdf")
  9. * if browser supports it. Otherwise it's undefined.
  10. *
  11. * @param {DataTransfer} dataTransfer
  12. * @param {Function} logDropError - a function that's called every time some
  13. * folder or some file error out (e.g. because of the folder name being too long
  14. * on Windows). Notice that resulting promise will always be resolved anyway.
  15. *
  16. * @returns {Promise} - Array<File>
  17. */
  18. async function getDroppedFiles(dataTransfer, _temp) {
  19. let {
  20. logDropError = () => {}
  21. } = _temp === void 0 ? {} : _temp;
  22. // Get all files from all subdirs. Works (at least) in Chrome, Mozilla, and Safari
  23. try {
  24. const accumulator = [];
  25. for await (const file of webkitGetAsEntryApi(dataTransfer, logDropError)) {
  26. accumulator.push(file);
  27. }
  28. return accumulator; // Otherwise just return all first-order files
  29. } catch {
  30. return fallbackApi(dataTransfer);
  31. }
  32. }
  33. module.exports = getDroppedFiles;