Skip to content

Commit

Permalink
fix: Fixed submission file not correctly loading if file name is the …
Browse files Browse the repository at this point in the history
…same as previous file
  • Loading branch information
michaelkoelle committed Jul 1, 2021
1 parent f868bbf commit d5ac66f
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions app/utils/FileAccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,32 @@ export function deleteEverythingInDir(dir: string) {
}
}

export function loadFilesFromWorkspace(
export function loadFilesFromWorkspaceToUserDataDir(
submissionName: string,
workspace: string,
userDataPath: string,
dir = 'temp'
): string[] {
if (!fs.existsSync(workspace) || Path.extname(workspace) !== '.cor') {
return [];
}
const tempPaths: string[] = [];
const userDataPath: string = remote.app.getPath('userData');
const tempDir = Path.join(userDataPath, dir);

// Create temp dir
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}

// Delete everything in tempDir
deleteEverythingInDir(tempDir);

// Create submission dir
const dest = Path.join(tempDir, submissionName);
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}

const zip = new AdmZip(workspace);
zip
.getEntries()
Expand All @@ -159,13 +168,41 @@ export function loadFilesFromWorkspace(
);
})
.forEach((entry) => {
const path = Path.join(tempDir, entry.name);
zip.extractEntryTo(entry, tempDir, false, true);
const path = Path.join(tempDir, submissionName, entry.name);
zip.extractEntryTo(entry, dest, false, true);
tempPaths.push(path);
});
return tempPaths;
}

export function loadFilesFromWorkspace(
submissionName: string,
workspace: string,
dir = 'temp'
): string[] {
const userDataPath: string = remote.app.getPath('userData');
return loadFilesFromWorkspaceToUserDataDir(
submissionName,
workspace,
userDataPath,
dir
);
}

export function loadFilesFromWorkspaceMainProcess(
submissionName: string,
workspace: string,
dir = 'temp'
): string[] {
const userDataPath: string = app.getPath('userData');
return loadFilesFromWorkspaceToUserDataDir(
submissionName,
workspace,
userDataPath,
dir
);
}

export function deleteCorrectionFromWorkspace(
correction: Correction,
workspace: string
Expand Down Expand Up @@ -291,37 +328,3 @@ export function exportWorkspace(zipPath: string, workspace: string) {
zip.addLocalFolder(workspace);
zip.writeZip(zipPath);
}

export function loadFilesFromWorkspaceMainProcess(
submissionName: string,
workspace: string,
dir = 'temp'
): string[] {
if (!fs.existsSync(workspace) || Path.extname(workspace) !== '.cor') {
return [];
}
const tempPaths: string[] = [];
const userDataPath: string = app.getPath('userData');
const tempDir = Path.join(userDataPath, dir);
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}

deleteEverythingInDir(tempDir);

const zip = new AdmZip(workspace);
zip
.getEntries()
.filter((entry) => {
return (
Path.dirname(entry.entryName).replaceAll('\\', '/') ===
Path.join(submissionName, 'files').replaceAll('\\', '/')
);
})
.forEach((entry) => {
const path = Path.join(tempDir, entry.name);
zip.extractEntryTo(entry, tempDir, false, true);
tempPaths.push(path);
});
return tempPaths;
}

0 comments on commit d5ac66f

Please sign in to comment.