Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(h5p-server): reduce memory and IO when uploading package #2100

Merged
merged 4 commits into from
Mar 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/h5p-server/src/ContentStorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,18 @@ export default class ContentStorer {
)
);

const filepathTonewFilenameMap = new Map<string, string>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably be filepathToNewFilenameMap

Copy link
Contributor Author

@cabauman cabauman Feb 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I'll also go ahead and change the filepath part to filePath for consistency with the rest of the code base.

for (const reference of fileReferencesInParams) {
const filepath = path.join(
packageDirectory,
'content',
reference.filePath
);
let newFilename = filepathTonewFilenameMap.get(filepath);
if (newFilename) {
reference.context.params.path = `${newFilename}#tmp`;
continue;
}
// If the file referenced in the parameters isn't included in the
// package, we first check if the path is actually a URL and if not,
// we delete the reference.
Expand All @@ -257,12 +263,13 @@ export default class ContentStorer {
continue;
}
const readStream = fsExtra.createReadStream(filepath);
const newFilename = await this.temporaryFileManager.addFile(
newFilename = await this.temporaryFileManager.addFile(
reference.filePath,
readStream,
user
);
reference.context.params.path = `${newFilename}#tmp`;
filepathTonewFilenameMap.set(filepath, newFilename);
}
return { metadata, parameters };
}
Expand Down Expand Up @@ -487,6 +494,7 @@ export default class ContentStorer {
oldFiles: string[]
): Promise<IFileReference[]> {
const filesToCopyFromTemporaryStorage: IFileReference[] = [];
const hashSet = new Set<string>();

for (const ref of fileReferencesInNewParams) {
// We mark the file to be copied over from temporary storage if the
Expand All @@ -495,11 +503,15 @@ export default class ContentStorer {
// We only save temporary file for later copying, however, if
// the there isn't already a file with the exact name. This
// might be the case if the user presses "save" twice.
if (!oldFiles.some((f) => f === ref.filePath)) {
if (
!hashSet.has(ref.filePath) &&
!oldFiles.some((f) => f === ref.filePath)
) {
filesToCopyFromTemporaryStorage.push(ref);
}
// remove temporary file marker from parameters
ref.context.params.path = ref.filePath;
hashSet.add(ref.filePath);
}
}
return filesToCopyFromTemporaryStorage;
Expand Down