utils.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. export const byteKB = 1024;
  2. export const byteMB = 1048576;
  3. export function getFileSize(number: number): string {
  4. if (number < byteKB) {
  5. return `${(number / byteKB).toFixed(2) }KB`;
  6. } else if (number >= byteKB && number < byteMB) {
  7. return `${(number / byteKB).toFixed(1) }KB`;
  8. } else if (number >= byteMB) {
  9. return `${(number / byteMB).toFixed(1) }MB`;
  10. }
  11. return undefined;
  12. }
  13. export function endsWith(str: string, suffix: string): boolean {
  14. return str.indexOf(suffix, str.length - suffix.length) !== -1;
  15. }
  16. export async function loopFiles(item: FileSystemDirectoryEntry): Promise<Array<FileSystemEntry>> {
  17. return new Promise((res, rej) => {
  18. const dirReader = item.createReader();
  19. let fileList: Array<FileSystemEntry> = [];
  20. function sequence(): void {
  21. dirReader.readEntries(entries => {
  22. const entryList = Array.prototype.slice.apply(entries);
  23. fileList = fileList.concat(entryList);
  24. // Check if all the file has been viewed
  25. const isFinished = !entryList.length;
  26. if (isFinished) {
  27. res(fileList);
  28. } else {
  29. sequence();
  30. }
  31. }, rej);
  32. }
  33. sequence();
  34. });
  35. }
  36. export async function mapFileTree(items: Array<DataTransferItem>): Promise<Array<File>> {
  37. const promises: Array<Promise<File>> = [];
  38. const _traverseFileTree = async (item: FileSystemEntry, path?: string): Promise<void> => {
  39. path = path || '';
  40. //@ts-ignore add path property into item
  41. item.path = path;
  42. if (item.isFile) {
  43. promises.push(new Promise((res, rej) => {
  44. (item as FileSystemFileEntry).file(file => {
  45. if (item.fullPath && !file.webkitRelativePath) {
  46. // This file is provided to the user based on the relative path of the drag and drop folder
  47. // If you drag the Upload folder, the path of the internal file may be Upload/File/a.png, etc
  48. Object.defineProperties(file, {
  49. webkitRelativePath: {
  50. writable: true,
  51. },
  52. });
  53. //@ts-ignore add webkitRelativePath property into file
  54. file.webkitRelativePath = item.fullPath.replace(/^\//, '');
  55. Object.defineProperties(file, {
  56. webkitRelativePath: {
  57. writable: false,
  58. },
  59. });
  60. }
  61. res(file);
  62. }, rej);
  63. }));
  64. } else if (item.isDirectory) {
  65. const entries = await loopFiles(item as FileSystemDirectoryEntry);
  66. for (let index = 0; index < entries.length; index++) {
  67. const entry = entries[index];
  68. await _traverseFileTree(entry, `${path}${item.name}/`);
  69. }
  70. }
  71. };
  72. try {
  73. const batches = items.map(i => _traverseFileTree(i.webkitGetAsEntry()));
  74. // Perform asynchronous operations to add the required promises to the queue
  75. await Promise.all(batches);
  76. // Execution queue
  77. const result = await Promise.all(promises);
  78. return result;
  79. } catch (error) {
  80. console.warn('Captured error while loop directory.');
  81. console.error(error);
  82. return [];
  83. }
  84. }