[File upload] Enforce file-type check in file dialog (#55063) (#55449)

This commit is contained in:
Thomas Neirynck 2020-01-21 16:37:03 -05:00 committed by GitHub
parent 09c2b8eccd
commit fca83a67c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 3 deletions

View file

@ -13,6 +13,8 @@ import { MAX_FILE_SIZE } from '../../common/constants/file_import';
import _ from 'lodash'; import _ from 'lodash';
const ACCEPTABLE_FILETYPES = ['json', 'geojson']; const ACCEPTABLE_FILETYPES = ['json', 'geojson'];
const acceptedFileTypeString = ACCEPTABLE_FILETYPES.map(type => `.${type}`).join(',');
const acceptedFileTypeStringMessage = ACCEPTABLE_FILETYPES.map(type => `.${type}`).join(', ');
export class JsonIndexFilePicker extends Component { export class JsonIndexFilePicker extends Component {
state = { state = {
@ -103,6 +105,7 @@ export class JsonIndexFilePicker extends Component {
const splitNameArr = name.split('.'); const splitNameArr = name.split('.');
const fileType = splitNameArr.pop(); const fileType = splitNameArr.pop();
if (!ACCEPTABLE_FILETYPES.includes(fileType)) { if (!ACCEPTABLE_FILETYPES.includes(fileType)) {
//should only occur if browser does not accept the <File> accept parameter
throw new Error( throw new Error(
i18n.translate('xpack.fileUpload.jsonIndexFilePicker.acceptableTypesError', { i18n.translate('xpack.fileUpload.jsonIndexFilePicker.acceptableTypesError', {
defaultMessage: 'File is not one of acceptable types: {types}', defaultMessage: 'File is not one of acceptable types: {types}',
@ -252,7 +255,10 @@ export class JsonIndexFilePicker extends Component {
) : ( ) : (
<span> <span>
{i18n.translate('xpack.fileUpload.jsonIndexFilePicker.formatsAccepted', { {i18n.translate('xpack.fileUpload.jsonIndexFilePicker.formatsAccepted', {
defaultMessage: 'Formats accepted: .json, .geojson', defaultMessage: 'Formats accepted: {acceptedFileTypeStringMessage}',
values: {
acceptedFileTypeStringMessage,
},
})}{' '} })}{' '}
<br /> <br />
<FormattedMessage <FormattedMessage
@ -274,6 +280,7 @@ export class JsonIndexFilePicker extends Component {
/> />
} }
onChange={this._fileHandler} onChange={this._fileHandler}
accept={acceptedFileTypeString}
/> />
</EuiFormRow> </EuiFormRow>
</Fragment> </Fragment>

View file

@ -5690,7 +5690,6 @@
"xpack.fileUpload.jsonIndexFilePicker.filePickerLabel": "アップロードするファイルを選択", "xpack.fileUpload.jsonIndexFilePicker.filePickerLabel": "アップロードするファイルを選択",
"xpack.fileUpload.jsonIndexFilePicker.fileProcessingError": "ファイル処理エラー: {errorMessage}", "xpack.fileUpload.jsonIndexFilePicker.fileProcessingError": "ファイル処理エラー: {errorMessage}",
"xpack.fileUpload.jsonIndexFilePicker.fileSizeError": "ファイルサイズエラー: {errorMessage}", "xpack.fileUpload.jsonIndexFilePicker.fileSizeError": "ファイルサイズエラー: {errorMessage}",
"xpack.fileUpload.jsonIndexFilePicker.formatsAccepted": "許可されている形式:.json、.geojson",
"xpack.fileUpload.jsonIndexFilePicker.maxSize": "最大サイズ:{maxFileSize}", "xpack.fileUpload.jsonIndexFilePicker.maxSize": "最大サイズ:{maxFileSize}",
"xpack.fileUpload.jsonIndexFilePicker.noFileNameError": "ファイル名が指定されていません", "xpack.fileUpload.jsonIndexFilePicker.noFileNameError": "ファイル名が指定されていません",
"xpack.fileUpload.jsonIndexFilePicker.parsingFile": "{featuresProcessed} 件の機能が解析されました…", "xpack.fileUpload.jsonIndexFilePicker.parsingFile": "{featuresProcessed} 件の機能が解析されました…",

View file

@ -5689,7 +5689,6 @@
"xpack.fileUpload.jsonIndexFilePicker.filePickerLabel": "选择文件进行上传", "xpack.fileUpload.jsonIndexFilePicker.filePickerLabel": "选择文件进行上传",
"xpack.fileUpload.jsonIndexFilePicker.fileProcessingError": "文件处理错误:{errorMessage}", "xpack.fileUpload.jsonIndexFilePicker.fileProcessingError": "文件处理错误:{errorMessage}",
"xpack.fileUpload.jsonIndexFilePicker.fileSizeError": "文件大小错误:{errorMessage}", "xpack.fileUpload.jsonIndexFilePicker.fileSizeError": "文件大小错误:{errorMessage}",
"xpack.fileUpload.jsonIndexFilePicker.formatsAccepted": "接受的格式:.json、.geojson",
"xpack.fileUpload.jsonIndexFilePicker.maxSize": "最大大小:{maxFileSize}", "xpack.fileUpload.jsonIndexFilePicker.maxSize": "最大大小:{maxFileSize}",
"xpack.fileUpload.jsonIndexFilePicker.noFileNameError": "未提供任何文件名称", "xpack.fileUpload.jsonIndexFilePicker.noFileNameError": "未提供任何文件名称",
"xpack.fileUpload.jsonIndexFilePicker.parsingFile": "{featuresProcessed} 特征已解析......", "xpack.fileUpload.jsonIndexFilePicker.parsingFile": "{featuresProcessed} 特征已解析......",