diff --git a/src/actions/filesDropped/constants.js b/src/actions/filesDropped/constants.js new file mode 100644 index 000000000..f3f10fd15 --- /dev/null +++ b/src/actions/filesDropped/constants.js @@ -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 +}; diff --git a/src/actions/filesDropped/index.js b/src/actions/filesDropped/index.js index d34a7f351..489eb8619 100644 --- a/src/actions/filesDropped/index.js +++ b/src/actions/filesDropped/index.js @@ -1,5 +1,6 @@ import { warningNotification } from "../notifications"; import handleMetadata from "./metadata"; +import { is_csv_or_tsv } from "./constants"; /** @@ -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); } diff --git a/src/actions/filesDropped/metadata.js b/src/actions/filesDropped/metadata.js index 13332e4ff..3636a168c 100644 --- a/src/actions/filesDropped/metadata.js +++ b/src/actions/filesDropped/metadata.js @@ -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"; /** @@ -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, { @@ -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 });