-
Notifications
You must be signed in to change notification settings - Fork 71
feat: configure mime/type option #398
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
Open
isaac-martin
wants to merge
3
commits into
sanity-io:main
Choose a base branch
from
isaac-martin:audioFileHandling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||
| import {ErrorOutlineIcon} from '@sanity/icons' | ||||
| import {Button, CardTone, Flex, Text, useToast} from '@sanity/ui' | ||||
| import React, {useEffect, useReducer, useRef, useState} from 'react' | ||||
| import React, {useCallback, useEffect, useReducer, useRef, useState} from 'react' | ||||
| import {type Observable, Subject, Subscription} from 'rxjs' | ||||
| import {takeUntil, tap} from 'rxjs/operators' | ||||
| import type {SanityClient} from 'sanity' | ||||
|
|
@@ -222,12 +222,40 @@ export default function Uploader(props: Props) { | |||
| }) | ||||
| } | ||||
|
|
||||
| const invalidFileToast = useCallback(() => { | ||||
| toast.push({ | ||||
| status: 'error', | ||||
| title: `Invalid file type. Accepted types: ${props.config.acceptedMimeTypes?.join(', ')}`, | ||||
| }) | ||||
| }, [props.config.acceptedMimeTypes, toast]) | ||||
|
|
||||
| /** | ||||
| * Validates if any file in the provided FileList or File array has an unsupported MIME type | ||||
| * @param files - FileList or File array to validate | ||||
| * @returns true if any file has an invalid MIME type, false if all files are valid | ||||
| */ | ||||
|
|
||||
| const isInvalidFile = (files: FileList | File[]) => { | ||||
| const isInvalid = Array.from(files).some((file) => { | ||||
| return !props.config.acceptedMimeTypes?.some((acceptedType) => | ||||
| file.type.match(new RegExp(acceptedType)) | ||||
| ) | ||||
| }) | ||||
|
|
||||
| if (isInvalid) { | ||||
| invalidFileToast() | ||||
| return true | ||||
| } | ||||
| return false | ||||
| } | ||||
|
|
||||
| /* -------------------------- Upload Initialization ------------------------- */ | ||||
| // The below populate the uploadInput state field, which then triggers the | ||||
| // upload configuration, or the startUpload function if no config is required. | ||||
|
|
||||
| // Stages an upload from the file selector | ||||
| const handleUpload = (files: FileList | File[]) => { | ||||
| if (isInvalidFile(files)) return | ||||
| dispatch({ | ||||
| action: 'stageUpload', | ||||
| input: {type: 'file', files}, | ||||
|
|
@@ -249,9 +277,14 @@ export default function Uploader(props: Props) { | |||
|
|
||||
| // Stages and validates an upload from dragging+dropping files or folders | ||||
| const handleDrop: React.DragEventHandler<HTMLDivElement> = (event) => { | ||||
| setDragState(null) | ||||
| event.preventDefault() | ||||
| event.stopPropagation() | ||||
| if (dragState === 'invalid') { | ||||
| invalidFileToast() | ||||
| setDragState(null) | ||||
| return | ||||
| } | ||||
| setDragState(null) | ||||
| extractDroppedFiles(event.nativeEvent.dataTransfer!).then((files) => { | ||||
| dispatch({ | ||||
| action: 'stageUpload', | ||||
|
|
@@ -271,7 +304,16 @@ export default function Uploader(props: Props) { | |||
| event.stopPropagation() | ||||
| dragEnteredEls.current.push(event.target) | ||||
| const type = event.dataTransfer.items?.[0]?.type | ||||
| setDragState(type?.startsWith('video/') ? 'valid' : 'invalid') | ||||
| const mimeTypes = props.config.acceptedMimeTypes | ||||
|
|
||||
| // Check if the dragged file type matches any of the accepted mime types | ||||
| const isValidType = mimeTypes?.some((acceptedType) => { | ||||
| // Convert mime type pattern to regex (e.g., 'video/*' -> /^video\/.*$/) | ||||
| const pattern = `^${acceptedType.replace('*', '.*')}$` | ||||
| return new RegExp(pattern).test(type) | ||||
| }) | ||||
|
|
||||
| setDragState(isValidType ? 'valid' : 'invalid') | ||||
| } | ||||
|
|
||||
| const handleDragLeave: React.DragEventHandler<HTMLDivElement> = (event) => { | ||||
|
|
@@ -335,6 +377,10 @@ export default function Uploader(props: Props) { | |||
| let tone: CardTone | undefined | ||||
| if (dragState) tone = dragState === 'valid' ? 'positive' : 'critical' | ||||
|
|
||||
| const acceptMimeString = props.config?.acceptedMimeTypes?.length | ||||
| ? props.config.acceptedMimeTypes.join(',') | ||||
| : 'video/*, audio/*' | ||||
|
|
||||
| return ( | ||||
| <> | ||||
| <UploadCard | ||||
|
|
@@ -353,6 +399,7 @@ export default function Uploader(props: Props) { | |||
| onChange={props.onChange} | ||||
| buttons={ | ||||
| <PlayerActionsMenu | ||||
| accept={acceptMimeString} | ||||
| asset={props.asset} | ||||
| dialogState={props.dialogState} | ||||
| setDialogState={props.setDialogState} | ||||
|
|
@@ -364,6 +411,8 @@ export default function Uploader(props: Props) { | |||
| /> | ||||
| ) : ( | ||||
| <UploadPlaceholder | ||||
| accept={acceptMimeString} | ||||
| config={props.config} | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| hovering={dragState !== null} | ||||
| onSelect={handleUpload} | ||||
| readOnly={!!props.readOnly} | ||||
|
|
||||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
encoding_tierhas been renamed tovideo_quality(andsmart->plusandbaseline->basic)I don't think the Sanity plugin supports
video_qualityyet, so this isn't an actionable comment. Just making a note that, at some point, we'll need to update the Sanity plugin to reflect this.