Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: uppy.addFile should accept browser File objects #4020

Merged
merged 4 commits into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.data && !fileDescriptor.isRemote) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a indirect way to guess that we are dealing with a File. Wouldn't it be better to do:

Suggested change
if (!fileDescriptor.data && !fileDescriptor.isRemote) {
if (fileDescriptor instanceof File) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I suspect there might be issue with this detection failing for some weird reason, but let’s try. @aduh95 do you see any potential problems?

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