Skip to content

Commit

Permalink
fix: handle unexpected file formats (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shurtu-gal authored Aug 4, 2023
1 parent 42f9a43 commit ea2b1e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion apps/studio/src/components/Editor/EditorDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const EditorDropdown: React.FunctionComponent<EditorDropdownProps> = () =
>
<input
type="file"
accept='.yaml, .yml, .json'
style={{ position: 'fixed', top: '-100em' }}
onChange={event => {
toast.promise(editorSvc.importFile(event.target.files), {
Expand All @@ -53,7 +54,7 @@ export const EditorDropdown: React.FunctionComponent<EditorDropdownProps> = () =
error: (
<div>
<span className="block text-bold text-red-400">
Failed to import document.
Failed to import document. Maybe the file type is invalid.
</span>
</div>
),
Expand Down
9 changes: 9 additions & 0 deletions apps/studio/src/services/editor.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ export class EditorService extends AbstractService {
if (!file) {
return;
}

// Check if file is valid (only JSON and YAML are allowed currently) ----Change afterwards as per the requirement
if (
file.type !== 'application/json' &&
file.type !== 'application/x-yaml' &&
file.type !== 'application/yaml'
) {
throw new Error('Invalid file type');
}

const fileReader = new FileReader();
fileReader.onload = fileLoadedEvent => {
Expand Down

0 comments on commit ea2b1e4

Please sign in to comment.