-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(edit-content): Validate proper files #30061
- Loading branch information
Showing
5 changed files
with
127 additions
and
44 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
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
46 changes: 46 additions & 0 deletions
46
core-web/libs/edit-content/src/lib/fields/dot-edit-content-host-folder-field/utils/index.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,46 @@ | ||
import { DotCMSContentlet, DotCMSTempFile } from '@dotcms/dotcms-models'; | ||
|
||
/** | ||
* Given an array of mime types, this function will convert all of them to | ||
* lower case and remove any asterisks from the mime type. It will then | ||
* filter out any empty strings from the array. | ||
* | ||
* @param mimeTypes an array of mime types | ||
* @returns an array of cleaned mime types | ||
*/ | ||
export function cleanMimeTypes(mimeTypes: string[]): string[] { | ||
return mimeTypes | ||
.map((type) => { | ||
return type.toLowerCase().replace(/\*/g, ''); | ||
}) | ||
.filter((type) => type !== ''); | ||
} | ||
|
||
/** | ||
* Checks if a file's mime type is in a list of accepted mime types. | ||
* | ||
* If the acceptedFiles array is empty, then this function will return true. | ||
* | ||
* @param file the file to check, either a DotCMSTempFile or DotCMSContentlet | ||
* @param acceptedFiles an array of mime types to check against | ||
* @returns true if the file's mime type is in the list of accepted mime types, false otherwise | ||
*/ | ||
export function checkMimeType( | ||
file: DotCMSTempFile | DotCMSContentlet, | ||
acceptedFiles: string[] | ||
): boolean { | ||
if (acceptedFiles.length === 0) { | ||
return true; | ||
} | ||
|
||
const mimeTypes = cleanMimeTypes(acceptedFiles); | ||
console.log('file', file); | ||
console.log('acceptedFiles', acceptedFiles); | ||
console.log('mimeTypes', mimeTypes); | ||
|
||
if (file.mimeType) { | ||
return mimeTypes.includes(file.mimeType); | ||
} | ||
|
||
return false; | ||
} |