Skip to content

Commit

Permalink
wip: Added file problematic character replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobiClark committed Oct 11, 2024
1 parent e1c9f22 commit 6661447
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const kombuchaServer = axios.create({
timeout: 0,
});



// Retrieve the userid value, and if it's not there, assign it a new uuid.
let userId;
try {
Expand Down
45 changes: 45 additions & 0 deletions src/renderer/src/scripts/others/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4277,11 +4277,13 @@ window.replaceProblematicFilesWithSDSCompliantNames = (datasetStructure) => {

const validSparcFolderAndFileNameRegex = /^[0-9A-Za-z,.\-_ ]*$/;
const identifierConventionsRegex = /^[0-9A-Za-z-_]*$/;
const forbiddenFileNameRegex = /^(CON|PRN|AUX|NUL|(COM|LPT)[0-9])$/;
const forbiddenFiles = new Set([".DS_Store", "Thumbs.db"]);
const forbiddenFilesRegex = /^(CON|PRN|AUX|NUL|(COM|LPT)[0-9])$/;

window.evaluateStringAgainstSdsRequirements = (stringToTest, testType) => {
const tests = {
"folder-or-file-name-contains-forbidden-characters": !forbiddenFileNameRegex.test(stringToTest),
"folder-or-file-name-is-valid": validSparcFolderAndFileNameRegex.test(stringToTest),
"string-adheres-to-identifier-conventions": identifierConventionsRegex.test(stringToTest),
"is-hidden-file": stringToTest.startsWith("."),
Expand Down Expand Up @@ -4428,6 +4430,13 @@ window.buildDatasetStructureJsonFromImportedData = async (
} else if (fileIsEmpty) {
emptyFiles.push(fileObject);
} else {
const fileNameContainsForbiddenCharacters = window.evaluateStringAgainstSdsRequirements(
fileName,
"folder-or-file-name-contains-forbidden-characters"
);
if (fileNameContainsForbiddenCharacters) {
console.log("File name contains forbidden characters");
}
// Check if the file name has any characters that do not comply with SPARC naming requirements
const fileNameIsValid = window.evaluateStringAgainstSdsRequirements(
fileName,
Expand Down Expand Up @@ -4520,16 +4529,52 @@ window.buildDatasetStructureJsonFromImportedData = async (
"Cancel import",
"What would you like to do with the folders with special characters?"
);

if (userResponse === "confirm") {
replaceProblematicFoldersWithSDSCompliantNames(datasetStructure);
}
// If the userResponse is "deny", nothing needs to be done

if (userResponse === "cancel") {
throw new Error("Importation cancelled");
}
}

if (problematicFileNames.length > 0) {
// Create two lists of problematic file names, one for files that contain forbidden characters,
// and one for files that contain unallowed characters but are not forbidden
const problematicFileNamesWithForbiddenCharacters = problematicFileNames.filter((file) =>
window.evaluateStringAgainstSdsRequirements(
file.fileName,
"folder-or-file-name-contains-forbidden-characters"
)
);
const problematicFileNamesWithoutForbiddenCharacters = problematicFileNames.filter(
(file) =>
!window.evaluateStringAgainstSdsRequirements(
file.fileName,
"folder-or-file-name-contains-forbidden-characters"
)
);
console.log(problematicFileNamesWithForbiddenCharacters);
console.log(problematicFileNamesWithoutForbiddenCharacters);

if (problematicFileNamesWithForbiddenCharacters.length > 0) {
const replaceForbiddenCharactersWithSdsCompliantCharacters = await swalFileListDoubleAction(
problematicFileNamesWithForbiddenCharacters.map((file) => file.relativePath),
"<p>Forbidden file names detected</p>",
`The files listed below contain forbidden characters and will not be imported into SODA.
You may choose to either keep them as is, or replace the characters with '-'.
`,
"Replace the forbidden characters with '-'",
"Cancel import for all folders and files",
"What would you like to do with the files with forbidden characters?"
);
if (replaceForbiddenCharactersWithSdsCompliantCharacters) {
window.replaceProblematicFilesWithSDSCompliantNames(datasetStructure);
}
}

const userResponse = await swalFileListTripleAction(
problematicFileNames.map((file) => file.relativePath),
"<p>File name modifications</p>",
Expand Down

0 comments on commit 6661447

Please sign in to comment.