From c658883cbfe1ef570dab38518bd87f0686dd9125 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 24 Aug 2022 16:28:22 +0200 Subject: [PATCH] @uppy/utils: fix `relativePath` when drag&dropping a folder (#4043) Fixes: https://github.com/transloadit/uppy/issues/4041 --- e2e/cypress/integration/dashboard-ui.spec.ts | 3 +++ .../webkitGetAsEntryApi/getRelativePath.js | 17 ----------------- .../utils/webkitGetAsEntryApi/index.js | 10 +++++----- 3 files changed, 8 insertions(+), 22 deletions(-) delete mode 100644 packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/getRelativePath.js diff --git a/e2e/cypress/integration/dashboard-ui.spec.ts b/e2e/cypress/integration/dashboard-ui.spec.ts index f322ead87f..6c9133d274 100644 --- a/e2e/cypress/integration/dashboard-ui.spec.ts +++ b/e2e/cypress/integration/dashboard-ui.spec.ts @@ -33,5 +33,8 @@ describe('dashboard-ui', () => { cy.get('.uppy-Dashboard-Item-previewImg') .should('have.length', 3) .each((element) => expect(element).attr('src').to.include('blob:')) + cy.window().then(({ uppy }) => { + expect(JSON.stringify(uppy.getFiles().map(file => file.meta.relativePath))).to.be.equal('[null,null,null,null]') + }) }) }) diff --git a/packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/getRelativePath.js b/packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/getRelativePath.js deleted file mode 100644 index b7cc375181..0000000000 --- a/packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/getRelativePath.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Get the relative path from the FileEntry#fullPath, because File#webkitRelativePath is always '', at least onDrop. - * - * @param {FileEntry} fileEntry - * - * @returns {string|null} - if file is not in a folder - return null (this is to - * be consistent with .relativePath-s of files selected from My Device). If file - * is in a folder - return its fullPath, e.g. '/simpsons/hi.jpeg'. - */ -export default function getRelativePath (fileEntry) { - // fileEntry.fullPath - "/simpsons/hi.jpeg" or undefined (for browsers that don't support it) - // fileEntry.name - "hi.jpeg" - if (!fileEntry.fullPath || fileEntry.fullPath === `/${fileEntry.name}`) { - return null - } - return fileEntry.fullPath -} diff --git a/packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/index.js b/packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/index.js index 4b6c9a17fb..31214ec439 100644 --- a/packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/index.js +++ b/packages/@uppy/utils/src/getDroppedFiles/utils/webkitGetAsEntryApi/index.js @@ -1,4 +1,3 @@ -import getRelativePath from './getRelativePath.js' import getFilesAndDirectoriesFromDirectory from './getFilesAndDirectoriesFromDirectory.js' /** @@ -9,6 +8,7 @@ function getAsFileSystemHandleFromEntry (entry, logDropError) { return { // eslint-disable-next-line no-nested-ternary kind: entry.isFile ? 'file' : entry.isDirectory ? 'directory' : undefined, + name: entry.name, getFile () { return new Promise((resolve, reject) => entry.file(resolve, reject)) }, @@ -25,17 +25,17 @@ function getAsFileSystemHandleFromEntry (entry, logDropError) { } } -async function* createPromiseToAddFileOrParseDirectory (entry) { +async function* createPromiseToAddFileOrParseDirectory (entry, relativePath) { // For each dropped item, - make sure it's a file/directory, and start deepening in! if (entry.kind === 'file') { const file = await entry.getFile() if (file !== null) { - file.relativePath = getRelativePath(entry) + file.relativePath = relativePath ? `${relativePath}/${entry.name}` : null yield file } } else if (entry.kind === 'directory') { for await (const handle of entry.values()) { - yield* createPromiseToAddFileOrParseDirectory(handle) + yield* createPromiseToAddFileOrParseDirectory(handle, `${relativePath}/${entry.name}`) } } } @@ -53,7 +53,7 @@ export default async function* getFilesFromDataTransfer (dataTransfer, logDropEr // :entry can be null when we drop the url e.g. if (entry != null) { try { - yield* createPromiseToAddFileOrParseDirectory(entry, logDropError) + yield* createPromiseToAddFileOrParseDirectory(entry, '') } catch (err) { if (lastResortFile) { yield lastResortFile