Skip to content

Commit

Permalink
refactor: Replaces manual path creation with path.join
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkoelle committed Jul 10, 2021
1 parent 5b62f24 commit 2e95137
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
63 changes: 36 additions & 27 deletions app/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export type ImportConflicts = {
};

export default class Importer {
private static FILE_DIR_NAME = 'files';

private static CONFIG_NAME = 'config.json';

private static ENCODING: BufferEncoding = 'utf8';

private mainWindow: BrowserWindow;

private index = 0;
Expand Down Expand Up @@ -86,7 +92,6 @@ export default class Importer {
return;
}
createNewCorFile(p);
// dispatch(workspaceSetPath(p));
workspace = p;
}

Expand Down Expand Up @@ -154,7 +159,7 @@ export default class Importer {
index: this.index,
total: this.total,
});
const content = fs.readFileSync(importPath, 'utf8');
const content = fs.readFileSync(importPath, Importer.ENCODING);
const correction: Correction = parser.deserialize(
content,
Path.basename(Path.dirname(importPath))
Expand Down Expand Up @@ -216,7 +221,7 @@ export default class Importer {
index: this.index,
total: this.total,
});
const content = zipEntry.getData().toString('utf8');
const content = zipEntry.getData().toString(Importer.ENCODING);
const correction: Correction = parser.deserialize(
content,
Path.basename(Path.dirname(zipEntry.entryName))
Expand Down Expand Up @@ -263,12 +268,14 @@ export default class Importer {
sender: WebContents
) {
const submissionName = correction.submission.name;
const fileDir = Path.join(submissionName, Importer.FILE_DIR_NAME);
const configPath = Path.join(submissionName, Importer.CONFIG_NAME);

// Create correction directory in workspace
createDirectoryInWorkspace(`${submissionName}/`, workspace);
createDirectoryInWorkspace(submissionName, workspace);

// Create file folder and copy submission files
createDirectoryInWorkspace(`${submissionName}/files/`, workspace);
createDirectoryInWorkspace(fileDir, workspace);

// This is not really modular, other parsers could use a different folder structure -> need to add new parser method
const files: string[] = getAllFilesInDirectory(Path.dirname(path)).filter(
Expand All @@ -282,14 +289,14 @@ export default class Importer {
);

correction.submission.files = targetFiles.map((f) => {
return { path: Path.parse(f).base, unread: true };
return { path: Path.basename(f), unread: true };
});

// Save config file
const { entities } = normalize(correction, CorrectionSchema);
addFileToWorkspace(
`${submissionName}/config.json`,
Buffer.from(JSON.stringify(entities), 'utf8'),
configPath,
Buffer.from(JSON.stringify(entities)),
workspace
);

Expand All @@ -305,9 +312,11 @@ export default class Importer {
sender: WebContents
) {
const submissionName = correction.submission.name;
const fileDir = Path.join(submissionName, Importer.FILE_DIR_NAME);
const configPath = Path.join(submissionName, Importer.CONFIG_NAME);

// Create correction directory in workspace
createDirectoryInWorkspace(`${submissionName}/`, workspace);
createDirectoryInWorkspace(submissionName, workspace);
const zipEntries = zip.getEntries();
const files: string[] = zipEntries
.filter(
Expand All @@ -318,33 +327,31 @@ export default class Importer {
)
.map((entry) => entry.entryName);

createDirectoryInWorkspace(`${submissionName}/files/`, workspace);
createDirectoryInWorkspace(fileDir, workspace);
const targetFiles: string[] = [];
files.forEach((file) => {
sender.send(IMPORT_PROGRESS, {
name: Path.basename(file),
index: this.index,
total: this.total,
});
const filesDir: string = Path.join(submissionName, 'files');
const { base } = Path.parse(file);
const fileName = base;
const filePath: string = Path.join(fileDir, Path.basename(file));
const buffer: Buffer | null = zip.readFile(file);
if (buffer != null) {
addFileToWorkspace(`${filesDir}/${fileName}`, buffer, workspace);
targetFiles.push(`${filesDir}/${fileName}`);
addFileToWorkspace(filePath, buffer, workspace);
targetFiles.push(filePath);
}
});

correction.submission.files = targetFiles.map((f) => {
return { path: Path.parse(f).base, unread: true };
return { path: Path.basename(f), unread: true };
});

// Save config file
const { entities } = normalize(correction, CorrectionSchema);
addFileToWorkspace(
`${submissionName}/config.json`,
Buffer.from(JSON.stringify(entities), 'utf8'),
configPath,
Buffer.from(JSON.stringify(entities), Importer.ENCODING),
workspace
);

Expand All @@ -370,7 +377,7 @@ export default class Importer {
total: this.total,
});
const zipEntry = zip.getEntry(c.path);
const content = zipEntry.getData().toString('utf8');
const content = zipEntry.getData().toString(Importer.ENCODING);
const parser: Parser = instanciateParser(c.parser);
const correction: Correction = parser.deserialize(
content,
Expand All @@ -396,7 +403,7 @@ export default class Importer {
index: this.index,
total: this.total,
});
const content = fs.readFileSync(c.path, 'utf8');
const content = fs.readFileSync(c.path, Importer.ENCODING);
const parser: Parser = instanciateParser(c.parser);
const correction: Correction = parser.deserialize(
content,
Expand Down Expand Up @@ -430,23 +437,25 @@ export default class Importer {
const zip = new AdmZip(workspace);
const targetFiles: string[] = [];
files.forEach((file) => {
// Send import progress
sender.send(IMPORT_PROGRESS, {
name: Path.basename(file),
index: this.index,
total: this.total,
});
const { base } = Path.parse(file);

// Copy submission file
if (submissionId) {
const fileName = base;
const fileDir = `${submissionId}/files`;
const fullPath = `${fileDir}/${fileName}`;
if (zip.getEntry(fullPath) === null) {
const fileName = Path.basename(file);
const fileDir = Path.join(submissionId, Importer.FILE_DIR_NAME);
const filePath = Path.join(fileDir, fileName);
if (zip.getEntry(filePath) === null) {
zip.addLocalFile(file, fileDir, fileName);
} else {
zip.deleteFile(fullPath);
zip.deleteFile(filePath);
zip.addLocalFile(file, fileDir, fileName);
}
targetFiles.push(fullPath);
targetFiles.push(filePath);
}
});
zip.writeZip();
Expand Down
10 changes: 7 additions & 3 deletions app/utils/FileAccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
} from '../features/workspace/workspaceSlice';
import { reportSaved } from '../model/SaveSlice';

export function createDirectoryInWorkspace(dir: string, workspace) {
export function createDirectoryInWorkspace(dirName: string, workspace: string) {
const zip = new AdmZip(workspace);
zip.addFile(dir, Buffer.alloc(0));
zip.addFile(`${dirName}/`, Buffer.alloc(0));
zip.writeZip();
}

Expand Down Expand Up @@ -168,7 +168,11 @@ export function loadFilesFromWorkspaceToUserDataDir(
);
})
.forEach((entry) => {
const path = Path.join(tempDir, submissionName, entry.name);
const path = Path.join(
tempDir,
submissionName,
Path.basename(entry.name)
);
zip.extractEntryTo(entry, dest, false, true);
tempPaths.push(path);
});
Expand Down

0 comments on commit 2e95137

Please sign in to comment.