This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
405a3ed
commit 69a437a
Showing
4 changed files
with
86 additions
and
40 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
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,27 @@ | ||
import { formatBytes } from '~/plugins/shorthands' | ||
|
||
/** | ||
* @param {File | Blob} file the file to validate | ||
* @param {{ maxSize: number, alertOnInvalid: boolean }} validationOptions the | ||
* constraints to validate the file against | ||
* @param validationOptions.maxSize the max file size in bytes | ||
* @param validationOptions.alertOnInvalid if an alert should pop up describing | ||
* each validation error | ||
* @returns `true` if the file is valid; `false` otherise | ||
*/ | ||
export const fileIsValid = (file, validationOptions) => { | ||
const { maxSize, alertOnInvalid } = validationOptions | ||
if (maxSize !== null && maxSize !== undefined && file.size > maxSize) { | ||
console.log(`File size: ${file.size}, max size: ${maxSize}`) | ||
if (alertOnInvalid) { | ||
alert( | ||
`File ${file.name} is too big! Must be less than ${formatBytes( | ||
maxSize | ||
)}` | ||
) | ||
} | ||
return false | ||
} | ||
|
||
return true | ||
} |
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