-
-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validate uploaded image files (#3797)
* chore: upgrade react-dropzone to latest * feat: add basic image validation checks * feat: remove unused image input properties * feat: add compression to image input component * chore: remove unused compressor plugin from file input component * fix: remove on img clicked property from image converter list * test: add feature test --------- Co-authored-by: Ben Furber <ben.furber@googlemail.com>
- Loading branch information
Showing
10 changed files
with
161 additions
and
46 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
Binary file not shown.
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
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
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
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
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
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,20 @@ | ||
import Compressor from 'compressorjs' | ||
|
||
export const compressImage = (image: File): Promise<File> => { | ||
return new Promise((resolve, reject) => { | ||
if (!image) { | ||
reject() | ||
return | ||
} | ||
|
||
new Compressor(image, { | ||
quality: 0.6, // 0 to 1 | ||
success: (compressed) => { | ||
resolve(compressed as File) | ||
}, | ||
error: (err) => { | ||
reject(err) | ||
}, | ||
}) | ||
}) | ||
} |
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,38 @@ | ||
// Basic check using the filereader api to see whether we can create a displayable image | ||
// If this fails then there is a problem with the file | ||
|
||
export const imageValid = (file: File): Promise<void> => { | ||
return new Promise((resolve, reject) => { | ||
if (!file) { | ||
reject() | ||
return | ||
} | ||
|
||
const reader = new FileReader() | ||
|
||
reader.onload = (e: ProgressEvent<FileReader>) => { | ||
const img = document.createElement('img') | ||
|
||
img.onload = () => { | ||
// Image loaded successfully | ||
img.remove() | ||
resolve() | ||
} | ||
|
||
img.onerror = () => { | ||
// Image failed to load (possibly corrupted) | ||
img.remove() | ||
reject() | ||
} | ||
|
||
img.src = e.target?.result as string | ||
} | ||
|
||
reader.onerror = () => { | ||
// Error reading file. It might be corrupted. | ||
reject() | ||
} | ||
|
||
reader.readAsDataURL(file) | ||
}) | ||
} |
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