Skip to content

Commit

Permalink
core: uppy.addFile should accept browser File objects (#4020)
Browse files Browse the repository at this point in the history
* If non-remote file has no .data, it’s a File object from the browser, convert it to Uppy format

* Add test

* Update packages/@uppy/core/src/Uppy.js

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>

* Check for instanceof File

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
arturi and aduh95 committed Aug 22, 2022
1 parent 35013e1 commit 7c460f3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/@uppy/core/src/Uppy.js
Expand Up @@ -434,6 +434,18 @@ class Uppy {
* The `files` value is passed in because it may be updated by the caller without updating the store.
*/
#checkAndCreateFileStateObject (files, fileDescriptor) {
// Uppy expects files in { name, type, size, data } format.
// If the actual File object is passed from input[type=file] or drag-drop,
// we normalize it to match Uppy file object
if (fileDescriptor instanceof File) {
fileDescriptor = {
name: fileDescriptor.name,
type: fileDescriptor.type,
size: fileDescriptor.size,
data: fileDescriptor,
}
}

const fileType = getFileType(fileDescriptor)
const fileName = getFileName(fileType, fileDescriptor)
const fileExtension = getFileNameAndExtension(fileName).extension
Expand Down
8 changes: 8 additions & 0 deletions packages/@uppy/core/src/Uppy.test.js
Expand Up @@ -736,6 +736,14 @@ describe('src/Core', () => {
expect(fileAddedEventMock.mock.calls[0][0]).toEqual(newFile)
})

it('should add a file from a File object', () => {
const fileData = new File([sampleImage], { type: 'image/jpeg' })
const core = new Core()

const fileId = core.addFile(fileData)
expect(core.getFile(fileId).id).toEqual(fileId)
})

it('should not allow a file that does not meet the restrictions', () => {
const core = new Core({
restrictions: {
Expand Down

0 comments on commit 7c460f3

Please sign in to comment.