-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(storage): fixing drop handler for file extensions (#4649)
- Loading branch information
1 parent
bb4e683
commit 1dbe3f4
Showing
6 changed files
with
112 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@aws-amplify/ui-react-storage": patch | ||
"@aws-amplify/ui-react": patch | ||
--- | ||
|
||
fix(storage): fixing drop handler for file extensions | ||
|
||
Previously, adding a file extension for an `acceptedFileTypes` when a customer would drop a file it would show as rejected even if it was a valid file type. This fixes that issue. |
2 changes: 2 additions & 0 deletions
2
...les/next/pages/ui/components/storage/storage-manager/accept-file-extension/aws-exports.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import awsExports from '@environments/storage/file-uploader/src/aws-exports'; | ||
export default awsExports; |
19 changes: 19 additions & 0 deletions
19
...les/next/pages/ui/components/storage/storage-manager/accept-file-extension/index.page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Amplify } from 'aws-amplify'; | ||
import { StorageManager } from '@aws-amplify/ui-react-storage'; | ||
import '@aws-amplify/ui-react/styles.css'; | ||
import awsExports from './aws-exports'; | ||
Amplify.configure(awsExports); | ||
|
||
export function StorageManagerExample() { | ||
return ( | ||
<> | ||
<StorageManager | ||
acceptedFileTypes={['.png']} | ||
accessLevel="public" | ||
maxFileCount={1} | ||
showThumbnails | ||
/> | ||
</> | ||
); | ||
} | ||
export default StorageManagerExample; |
32 changes: 32 additions & 0 deletions
32
packages/react/src/primitives/DropZone/__tests__/filterAllowedFiles.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { filterAllowedFiles } from '../filterAllowedFiles'; | ||
|
||
describe('filterAllowFiles', () => { | ||
const droppedFiles = [ | ||
new File([], 'test.jpg', { type: 'image/jpg' }), | ||
new File([], 'test.png', { type: 'image/png' }), | ||
]; | ||
|
||
it('should work with * MIME types', () => { | ||
const { acceptedFiles, rejectedFiles } = filterAllowedFiles(droppedFiles, [ | ||
'image/*', | ||
]); | ||
expect(rejectedFiles).toHaveLength(0); | ||
expect(acceptedFiles).toHaveLength(2); | ||
}); | ||
|
||
it('should work with extension types', () => { | ||
const { acceptedFiles, rejectedFiles } = filterAllowedFiles(droppedFiles, [ | ||
'.png', | ||
]); | ||
expect(rejectedFiles).toHaveLength(1); | ||
expect(acceptedFiles).toHaveLength(1); | ||
}); | ||
|
||
it('should work with *', () => { | ||
const { acceptedFiles, rejectedFiles } = filterAllowedFiles(droppedFiles, [ | ||
'*', | ||
]); | ||
expect(rejectedFiles).toHaveLength(0); | ||
expect(acceptedFiles).toHaveLength(2); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
packages/react/src/primitives/DropZone/filterAllowedFiles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Drag event file shape is different than the drop event fileshape | ||
type DragFile = | ||
| { | ||
kind: string; | ||
type: string; | ||
name?: string; | ||
} | ||
| File; | ||
|
||
export function filterAllowedFiles<FileType extends DragFile = DragFile>( | ||
files: FileType[], | ||
acceptedFileTypes: string[] | ||
): { acceptedFiles: FileType[]; rejectedFiles: FileType[] } { | ||
// Allow any files if acceptedFileTypes is undefined, empty array, or contains '*' | ||
if ( | ||
!acceptedFileTypes || | ||
acceptedFileTypes.length === 0 || | ||
acceptedFileTypes.includes('*') | ||
) { | ||
return { acceptedFiles: files, rejectedFiles: [] }; | ||
} | ||
const acceptedFiles: FileType[] = []; | ||
const rejectedFiles: FileType[] = []; | ||
|
||
function filterFile(file: DragFile) { | ||
const { type = '', name = '' } = file; | ||
const mimeType = type.toLowerCase(); | ||
const baseMimeType = mimeType.split('/')[0]; | ||
|
||
return acceptedFileTypes.some((type) => { | ||
const validType = type.trim().toLowerCase(); | ||
// if the accepted file type is a file extension | ||
// it will start with '.', check against the file name | ||
if (validType.charAt(0) === '.') { | ||
return name.toLowerCase().endsWith(validType); | ||
} | ||
// This is something like a image/* mime type | ||
if (validType.endsWith('/*')) { | ||
return baseMimeType === validType.split('/')[0]; | ||
} | ||
return mimeType === validType; | ||
}); | ||
} | ||
|
||
files.forEach((file) => { | ||
(filterFile(file) ? acceptedFiles : rejectedFiles).push(file); | ||
}); | ||
|
||
return { acceptedFiles, rejectedFiles }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters