From 2a42d4ecb7b79eb24abbed7c6efe0cfcffb830ff Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Fri, 27 Nov 2026 23:11:07 +0200 Subject: [PATCH] fix(play): a base64 + read as a space, and a late file from the chat replacing the person's (#130) --- src/h5p/build/build-source.ts | 3 ++- src/h5p/play/play-page.ts | 22 ++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/h5p/build/build-source.ts b/src/h5p/build/build-source.ts index a7af976..dbcc5f3 100644 --- a/src/h5p/build/build-source.ts +++ b/src/h5p/build/build-source.ts @@ -58,7 +58,8 @@ export async function sourceFromFragment(hash: string): Promise { if (!value) return null let bytes: Uint8Array try { - const base64 = value.replace(/-/g, '+').replace(/_/g, '/').replace(/\s/g, '') + // URLSearchParams reads a "+" as a space: in base64 that space was a "+" (#130). + const base64 = value.replace(/ /g, '+').replace(/-/g, '+').replace(/_/g, '/').replace(/\s/g, '') bytes = Uint8Array.from(atob(base64 + '='.repeat((4 - (base64.length % 4)) % 4)), (char) => char.charCodeAt(0)) } catch { throw new Error('The link’s #source= is not base64url') diff --git a/src/h5p/play/play-page.ts b/src/h5p/play/play-page.ts index 2fbbd0e..8203c36 100644 --- a/src/h5p/play/play-page.ts +++ b/src/h5p/play/play-page.ts @@ -130,6 +130,8 @@ let source: unknown let wanted: string[] = [] /** The files given so far, by name. */ const chosen = new Map() +/** The names of those the person chose themselves: one from the chat does not take their place. */ +const own = new Set() /** The address of the package last built, given back when the next one replaces it. */ let packageUrl: string | null = null /** Counts builds, so that one overtaken by a newer one shows nothing. */ @@ -389,8 +391,10 @@ function fetching(name: string, received: number, total: number): void { if (main.dataset.state !== 'fetching') show('fetching', `Getting your ${what} from the chat.`) } -/** Files given, by the person or by the parent: the activity is built once none is missing. */ -function take(files: File[]): void { +/** Files given, by the person (`mine`) or by the parent: the activity is built once none is missing. */ +function take(given: File[], mine = false): void { + // One from the chat that comes after the person chose theirs is not built from again (#130). + const files = mine ? given : given.filter((file) => !own.has(file.name) && !(wanted.length === 1 && own.size)) if (!files.length || !wanted.length) return // A file that comes late, from a parent still bringing what the person chose themselves // meanwhile, must not start the activity again under them, nor take the place of the file @@ -403,8 +407,14 @@ function take(files: File[]): void { links.clear() } // Where one file is named, a second choice replaces the first. - if (wanted.length === 1) chosen.clear() - for (const file of files) chosen.set(file.name, file) + if (wanted.length === 1) { + chosen.clear() + own.clear() + } + for (const file of files) { + chosen.set(file.name, file) + if (mine) own.add(file.name) + } // While a preview plays, the files make the package to keep, and the player is left playing: // saved where Download asked for them, and only made ready for it where the parent sent them unasked. if (previewed.length) void keep() @@ -895,7 +905,7 @@ document.addEventListener('click', (event) => { filesInput.addEventListener('change', () => { const files = [...(filesInput.files ?? [])] filesInput.value = '' - take(files) + take(files, true) }) // A file dropped on the stage while it is asked for, as one chosen. const asked = (): boolean => main.dataset.state === 'asking' || main.dataset.state === 'fetching' @@ -909,7 +919,7 @@ main.addEventListener('drop', (event) => { delete main.dataset.drop if (!asked() || !event.dataTransfer?.files.length) return event.preventDefault() - take([...event.dataTransfer.files]) + take([...event.dataTransfer.files], true) }) /** A count of bytes as a message gives it: a number that is one, and not below nothing. */