mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
Adds sanity check for uploaded workpad JSON (#24297)
* Extracted upload handler. Added sanity check for uploaded JSON * Changed file name and error verbiage * Fixed import * Added early return for null file * Cleaned up update_workpad
This commit is contained in:
parent
c90e0eb756
commit
bc2803d151
3 changed files with 47 additions and 45 deletions
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { get } from 'lodash';
|
||||
import { getId } from '../../lib/get_id';
|
||||
import { notify } from '../../lib/notify';
|
||||
|
||||
export const uploadWorkpad = (file, onUpload) => {
|
||||
if (!file) return;
|
||||
|
||||
if (get(file, 'type') !== 'application/json') {
|
||||
return notify.warning('Only JSON files are accepted', {
|
||||
title: `Couldn't upload '${file.name || 'file'}'`,
|
||||
});
|
||||
}
|
||||
// TODO: Clean up this file, this loading stuff can, and should be, abstracted
|
||||
const reader = new FileReader();
|
||||
|
||||
// handle reading the uploaded file
|
||||
reader.onload = () => {
|
||||
try {
|
||||
const workpad = JSON.parse(reader.result);
|
||||
workpad.id = getId('workpad');
|
||||
|
||||
// sanity check for workpad object
|
||||
if (!Array.isArray(workpad.pages) || workpad.pages.length === 0 || !workpad.assets) {
|
||||
throw new Error(
|
||||
`Some properties required for a Canvas workpad are missing. Edit your JSON file to provide the correct property values and try again.`
|
||||
);
|
||||
}
|
||||
|
||||
onUpload(workpad);
|
||||
} catch (e) {
|
||||
notify.error(e, { title: `Couldn't upload '${file.name || 'file'}'` });
|
||||
}
|
||||
};
|
||||
|
||||
// read the uploaded file
|
||||
reader.readAsText(file);
|
||||
};
|
|
@ -6,30 +6,13 @@
|
|||
|
||||
import PropTypes from 'prop-types';
|
||||
import { compose, withHandlers } from 'recompose';
|
||||
import { getId } from '../../../lib/get_id';
|
||||
import { notify } from '../../../lib/notify';
|
||||
import { uploadWorkpad } from '../upload_workpad';
|
||||
import { WorkpadDropzone as Component } from './workpad_dropzone';
|
||||
|
||||
export const WorkpadDropzone = compose(
|
||||
withHandlers({
|
||||
onDropAccepted: ({ onUpload }) => ([file]) => {
|
||||
// TODO: Clean up this file, this loading stuff can, and should be, abstracted
|
||||
const reader = new FileReader();
|
||||
|
||||
// handle reading the uploaded file
|
||||
reader.onload = () => {
|
||||
try {
|
||||
const workpad = JSON.parse(reader.result);
|
||||
workpad.id = getId('workpad');
|
||||
onUpload(workpad);
|
||||
} catch (e) {
|
||||
notify.error(e, { title: `Couldn't upload '${file.name || 'file'}'` });
|
||||
}
|
||||
};
|
||||
|
||||
// read the uploaded file
|
||||
reader.readAsText(file);
|
||||
},
|
||||
onDropAccepted: ({ onUpload }) => ([file]) => uploadWorkpad(file, onUpload),
|
||||
onDropRejected: () => ([file]) => {
|
||||
notify.warning('Only JSON files are accepted', {
|
||||
title: `Couldn't upload '${file.name || 'file'}'`,
|
||||
|
|
|
@ -7,38 +7,14 @@
|
|||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { EuiFilePicker } from '@elastic/eui';
|
||||
import { get } from 'lodash';
|
||||
import { getId } from '../../lib/get_id';
|
||||
import { notify } from '../../lib/notify';
|
||||
import { uploadWorkpad } from './upload_workpad';
|
||||
|
||||
export const WorkpadUpload = ({ onUpload, ...rest }) => (
|
||||
<EuiFilePicker
|
||||
{...rest}
|
||||
compressed
|
||||
initialPromptText="Import workpad JSON file"
|
||||
onChange={([file]) => {
|
||||
if (get(file, 'type') !== 'application/json') {
|
||||
return notify.warning('Only JSON files are accepted', {
|
||||
title: `Couldn't upload '${file.name || 'file'}'`,
|
||||
});
|
||||
}
|
||||
// TODO: Clean up this file, this loading stuff can, and should be, abstracted
|
||||
const reader = new FileReader();
|
||||
|
||||
// handle reading the uploaded file
|
||||
reader.onload = () => {
|
||||
try {
|
||||
const workpad = JSON.parse(reader.result);
|
||||
workpad.id = getId('workpad');
|
||||
onUpload(workpad);
|
||||
} catch (e) {
|
||||
notify.error(e, { title: `Couldn't upload '${file.name || 'file'}'` });
|
||||
}
|
||||
};
|
||||
|
||||
// read the uploaded file
|
||||
reader.readAsText(file);
|
||||
}}
|
||||
onChange={([file]) => uploadWorkpad(file, onUpload)}
|
||||
/>
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue