diff --git a/e2e.spec.ts b/e2e.spec.ts index 00d91cf..805b67e 100644 --- a/e2e.spec.ts +++ b/e2e.spec.ts @@ -2,7 +2,7 @@ import { createCipheriv, createDecipheriv, createHash, hkdfSync, pbkdf2Sync, ran import { createSocket, type Socket } from 'node:dgram'; import { once } from 'node:events'; import { createReadStream } from 'node:fs'; -import { open as openFile, readFile, writeFile } from 'node:fs/promises'; +import { mkdir, open as openFile, readFile, writeFile } from 'node:fs/promises'; import type { AddressInfo } from 'node:net'; import { networkInterfaces } from 'node:os'; import { @@ -576,6 +576,79 @@ test('build: chromium sends to firefox from the production build under a path', } }); +// The file picker and a drag from the file manager, the way the browser itself delivers them. +// setInputFiles fires a change event of its own whatever was picked, and no page can make up a +// dropped folder. Only Chromium's protocol reaches both, and it is the one that fires no change +// event for a file its input already holds. +async function fileManager(page: Page) { + const client = await page.context().newCDPSession(page); + const zone = page.locator('label:has(> [type=file])'); + return { + zone, + async pick(path: string) { + const { result } = await client.send('Runtime.evaluate', { + expression: "document.querySelector('input[type=file]')", + }); + await client.send('DOM.setFileInputFiles', { files: [path], objectId: result.objectId }); + }, + async drop(path: string) { + const box = (await zone.boundingBox())!; + const at = { x: box.x + box.width / 2, y: box.y + box.height / 2 }; + const data = { items: [], files: [path], dragOperationsMask: 1 }; + for (const type of ['dragEnter', 'dragOver', 'drop'] as const) { + await client.send('Input.dispatchDragEvent', { type, ...at, data }); + } + }, + }; +} + +// Chromium fires no change event when the picker returns the file its input holds. The page has to +// take it anyway: the file may have changed on disk, what the page held of it can't be read anymore +// then, or the user dropped another one in between, which the input never saw. +test('pick: chromium takes a file picked again, changed on disk or after a drop', async () => { + const sender = await open('chromium'); + const { zone, pick, drop } = await fileManager(sender); + const source = await payload(randomBytes(1000), 'picked.bin'); + + await pick(source); + await expect(zone).toHaveText('picked.bin 1000 B'); + await writeFile(source, randomBytes(1536)); + await pick(source); + await expect(zone).toHaveText('picked.bin 1.5 KB'); + + await drop(await payload(randomBytes(2048), 'dropped.bin')); + await expect(zone).toHaveText('dropped.bin 2.0 KB'); + // The input stays empty, its tooltip would say no file is chosen. + await expect(zone.locator('[type=file]')).toHaveAttribute('title', 'dropped.bin'); + await pick(source); + await expect(zone).toHaveText('picked.bin 1.5 KB'); +}); + +// A folder comes as a file that can't be read. Unnoticed, it would fail only once the receiver +// downloads it. +test('pick: chromium refuses a dropped folder and keeps the file it had', async () => { + const sender = await open('chromium'); + const { zone, pick, drop } = await fileManager(sender); + const folder = test.info().outputPath('holiday'); + await mkdir(folder); + const refused = sender.locator('#not-a-file'); + + await drop(folder); + await expect(refused).toHaveText('holiday is a folder, only a single file can be sent'); + await expect(zone).toContainText('Drop a file or click to choose'); + await expect(sender.locator('#share')).toBeDisabled(); + + // A file ends the note, whether picked or dropped. + await pick(await payload(randomBytes(1000), 'picked.bin')); + await expect(zone).toHaveText('picked.bin 1000 B'); + await drop(folder); + await expect(refused).toBeVisible(); + await expect(zone).toContainText('picked.bin 1000 B'); + await expect(sender.locator('#share')).toBeEnabled(); + await drop(await payload(randomBytes(2048), 'dropped.bin')); + await expect(zone).toHaveText('dropped.bin 2.0 KB'); +}); + for (const name of Object.keys(BROWSERS)) { test(`servers: ${name} skips invalid lines, a reload brings back the defaults`, async () => { const page = await open(name); diff --git a/package.json b/package.json index 3bd1587..96018b0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "hellcon", "private": true, - "version": "1.0.1", + "version": "1.0.2", "license": "MIT", "type": "module", "scripts": { diff --git a/src/App.vue b/src/App.vue index f2c4046..4e9117a 100644 --- a/src/App.vue +++ b/src/App.vue @@ -242,15 +242,35 @@ async function attempt(signal: AbortSignal, step: Promise) { } } +// The name of a dropped folder, until a file takes its place. +const folder = ref(''); + +function take(picked: File) { + file.value = picked; + folder.value = ''; +} + +// The input is emptied right away: Chromium fires no change event for the file its input already +// holds, though that file may have changed on disk, or another one was dropped since, which the +// input never saw. An event without a file has nothing to take. Empty, the input's own tooltip says no +// file is chosen, so its title names the one taken. function onPick(event: Event) { - file.value = (event.target as HTMLInputElement).files?.[0]; + const input = event.target as HTMLInputElement; + const picked = input.files?.[0]; + input.value = ''; + if (picked) take(picked); } const dragging = ref(false); +// A folder comes as a file too, one that can't be read: refused here, it would fail only once the +// receiver downloads it. The file picked before stays. function onDrop(event: DragEvent) { dragging.value = false; - file.value = event.dataTransfer?.files[0] ?? file.value; + const dropped = event.dataTransfer?.files[0]; + const entry = [...(event.dataTransfer?.items ?? [])].find((item) => item.kind === 'file')?.webkitGetAsEntry(); + if (entry?.isDirectory) folder.value = entry.name; + else if (dropped) take(dropped); } function formatSize(bytes: number) { @@ -432,12 +452,13 @@ document.addEventListener('visibilitychange', holdWakeLock); @dragleave="dragging = false" @drop.prevent="onDrop" > - + + {{ folder }} is a folder, only a single file can be sent

{{ shared!.name }}

diff --git a/src/style.css b/src/style.css index d0e7209..6c0c045 100644 --- a/src/style.css +++ b/src/style.css @@ -411,6 +411,12 @@ label strong, overflow-wrap: anywhere; } +/* A dropped folder, refused */ +#not-a-file { + color: var(--color-secondary-text); + overflow-wrap: anywhere; +} + /* How the session is doing, below the file on its side. Until the transfer starts a pill passes through the bar, blue, or pink while it tries again after a failure. Then the transfer's progress, green once done, muted once stopped, pink for an error. The line below says what. */