fix(electron-utils): skip drop files whose path resolution throws (#240)
* fix(electron-utils): skip drop files whose path resolution throws * fix(electron-utils): extract throwing-resolver guard to satisfy lint --------- Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
co-authored by
Aniruddha Adak
parent
4e1c6330d1
commit
bd46b03ee8
@@ -29,6 +29,15 @@ const MAX_DROPPED_FILES = 20
|
||||
/** the resolver signature webUtils.getPathForFile satisfies; injectable for tests */
|
||||
type PathResolver = (file: File) => string
|
||||
|
||||
/** Resolve one dropped file, tolerating resolver failures (see droppableFilePaths). */
|
||||
function tryResolvePath(file: File, getPathForFile: PathResolver): string {
|
||||
try {
|
||||
return getPathForFile(file).trim()
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve an event's dropped files to local paths. Returns null when the drag
|
||||
* carries no OS files at all (internal text/element drags), or [] when it does
|
||||
@@ -42,8 +51,10 @@ export function droppableFilePaths(
|
||||
if (!transfer || !transfer.types.includes('Files')) return null
|
||||
const paths: string[] = []
|
||||
for (const file of Array.from(transfer.files)) {
|
||||
// non-empty guard covers virtual entries (e.g. page-referenced blobs) that resolve to ''
|
||||
const path = getPathForFile(file).trim()
|
||||
// A throwing resolver (e.g. a sandboxed entry Electron cannot map) must
|
||||
// not abort the whole drop: skip that file like a virtual entry.
|
||||
// Non-empty guard covers virtual entries (e.g. page-referenced blobs).
|
||||
const path = tryResolvePath(file, getPathForFile)
|
||||
if (path) paths.push(path)
|
||||
}
|
||||
return paths
|
||||
|
||||
@@ -101,6 +101,14 @@ describe('droppableFilePaths', () => {
|
||||
)
|
||||
expect(result).toEqual(['/tmp/doc.docx'])
|
||||
})
|
||||
|
||||
it('skips files whose path resolution throws instead of aborting the drop', () => {
|
||||
const result = droppableFilePaths(fileDrag(['bad.docx', 'good.docx']) as never, (file) => {
|
||||
if ((file as { name: string }).name === 'bad.docx') throw new Error('denied')
|
||||
return '/tmp/good.docx'
|
||||
})
|
||||
expect(result).toEqual(['/tmp/good.docx'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('partitionDropPayload', () => {
|
||||
|
||||
Reference in New Issue
Block a user