Skip to content
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

fix(app): disallow non .py or .json protocol upload #16076

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/assets/localization/en/protocol_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"import_a_file": "Import a protocol to get started",
"import_new_protocol": "Import a Protocol",
"import": "Import",
"incompatible_file_type": "Incompatible file type",
"instrument_cal_data_title": "Calibration data",
"instrument_not_attached": "Not attached",
"instruments_title": "Required Pipettes",
Expand Down
17 changes: 15 additions & 2 deletions app/src/organisms/ProtocolsLanding/ProtocolUploadInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,51 @@ import {
SPACING,
LegacyStyledText,
} from '@opentrons/components'
import { ERROR_TOAST } from '../../atoms/Toast'
import { UploadInput } from '../../molecules/UploadInput'
import { addProtocol } from '../../redux/protocol-storage'
import {
useTrackEvent,
ANALYTICS_IMPORT_PROTOCOL_TO_APP,
} from '../../redux/analytics'
import { useLogger } from '../../logger'
import { useToaster } from '../ToasterOven'

import type { Dispatch } from '../../redux/types'

export interface UploadInputProps {
onUpload?: () => void
}

const isValidProtocolFileName = (protocolFileName: string): boolean => {
return protocolFileName.endsWith('.py') || protocolFileName.endsWith('.json')
}

export function ProtocolUploadInput(
props: UploadInputProps
): JSX.Element | null {
const { t } = useTranslation(['protocol_info', 'shared'])
const dispatch = useDispatch<Dispatch>()
const logger = useLogger(new URL('', import.meta.url).pathname)
const trackEvent = useTrackEvent()
const { makeToast } = useToaster()

const handleUpload = (file: File): void => {
if (file.path === null) {
logger.warn('Failed to upload file, path not found')
}
dispatch(addProtocol(file.path))
if (isValidProtocolFileName(file.name)) {
dispatch(addProtocol(file.path))
} else {
makeToast(t('incompatible_file_type') as string, ERROR_TOAST, {
closeButton: true,
})
}
props.onUpload?.()
trackEvent({
name: ANALYTICS_IMPORT_PROTOCOL_TO_APP,
properties: { protocolFileName: file.name },
})
props.onUpload?.()
}

return (
Expand Down
Loading