Skip to content

Commit

Permalink
Merge pull request #913 from nextstrain/fix-windows-drag-n-drop-bug
Browse files Browse the repository at this point in the history
Fix Windows drag & drop bug
  • Loading branch information
jameshadfield authored Mar 4, 2020
2 parents fe00bb3 + 28500a2 commit 33da070
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/actions/filesDropped/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Defines acceptable file types for the auspice drag & drop functionality.
*/

const csv_file_types = ["text/csv", "application/vnd.ms-excel"];

// Add MacOS & Linux .tsv to accepted file types
const accepted_file_types = csv_file_types.concat("text/tab-separated-values");

// Handle Windows .tsv edge case with empty file type
const is_windows_tsv = (file) => file.type === "" && file.name.endsWith('.tsv');

const is_csv_or_tsv = (file) => accepted_file_types.includes(file.type) || is_windows_tsv(file);

export {
csv_file_types,
is_csv_or_tsv
};
4 changes: 3 additions & 1 deletion src/actions/filesDropped/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { warningNotification } from "../notifications";
import handleMetadata from "./metadata";
import { is_csv_or_tsv } from "./constants";


/**
Expand All @@ -16,7 +17,8 @@ const handleFilesDropped = (files) => (dispatch, getState) => {
}

const file = files[0];
if (file.type === "text/csv" || file.type === "text/tab-separated-values") {

if (is_csv_or_tsv(file)) {
return handleMetadata(dispatch, getState, file);
}

Expand Down
5 changes: 3 additions & 2 deletions src/actions/filesDropped/metadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Papa from "papaparse";
import { errorNotification, successNotification, warningNotification } from "../notifications";
import { ADD_COLOR_BYS } from "../types";
import { csv_file_types, is_csv_or_tsv } from "./constants";


/**
Expand All @@ -12,7 +13,7 @@ import { ADD_COLOR_BYS } from "../types";
* @param {DataTransfer} file a DataTransfer object
*/
const parseCsv = (file) => new Promise((resolve, reject) => {
if (!["text/csv", "text/tab-separated-values"].includes(file.type)) {
if (!(is_csv_or_tsv(file))) {
reject(new Error("Cannot parse this filetype"));
}
Papa.parse(file, {
Expand All @@ -25,7 +26,7 @@ const parseCsv = (file) => new Promise((resolve, reject) => {
},
encoding: "UTF-8",
comments: "#",
delimiter: file.type === "text/csv" ? "," : "\t",
delimiter: (csv_file_types.includes(file.type)) ? "," : "\t",
skipEmptyLines: true,
dynamicTyping: false
});
Expand Down

0 comments on commit 33da070

Please sign in to comment.