Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Shorten a long name with an early dot instead of refusing the download (#144)

Arpit Khandelwal884683ff8288
diff --git a/e2e/blockProgress.spec.ts b/e2e/blockProgress.spec.ts
index 63a9d12..bc6beb5 100644
--- a/e2e/blockProgress.spec.ts
+++ b/e2e/blockProgress.spec.ts
@@ -1,6 +1,6 @@
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
-import { join } from 'node:path'
+import { basename, join } from 'node:path'
import { expect, test } from '@playwright/test'
import fc from 'fast-check'
import { advanceBlock, retractBlock } from '../src/main/download/blockProgress'
@@ -99,6 +99,22 @@ test.describe('destination-side staging file', () => {
await expect(readFile(second)).rejects.toMatchObject({ code: 'ENOENT' })
})
+ test('a long name with a dot early in it is shortened, not refused', async () => {
+ for (const name of [
+ `St. Louis Cardinals vs Chicago Cubs full game replay ${'x'.repeat(200)}`,
+ `1. ${'漢'.repeat(85)}`
+ ]) {
+ const first = await reserveDestinationPath(dir, name)
+ const second = await reserveDestinationPath(dir, name)
+ expect(Buffer.byteLength(`${basename(first)}.plexo`)).toBeLessThanOrEqual(255)
+ expect(basename(second)).toBe(`${basename(first)} (1)`)
+ expect(name.startsWith(basename(first))).toBe(true)
+ }
+ // A real extension still stays last.
+ const video = await reserveDestinationPath(dir, `${'a'.repeat(300)}.mkv`)
+ expect(basename(video)).toMatch(/^a+\.mkv$/)
+ })
+
test('out-of-order ranges and a racing hedge produce one exact file', async () => {
const destination = join(dir, 'result.bin')
const file = new DownloadFile(`${destination}.plexo`)
diff --git a/src/main/download/paths.ts b/src/main/download/paths.ts
index 936544c..4be9b3f 100644
--- a/src/main/download/paths.ts
+++ b/src/main/download/paths.ts
@@ -15,6 +15,10 @@ export function getHomeDir(): string {
// (or if something outside the app is creating them as fast as we try).
const MAX_NAME_ATTEMPTS = 10_000
+// Longer than any real extension. Past this, what follows the last dot is part of a title
+// ("St. Louis Cardinals vs Chicago Cubs full game replay…"), shortened along with the rest.
+const MAX_EXTENSION_BYTES = 32
+
export async function pathExists(path: string): Promise<boolean> {
try {
await lstat(path)
@@ -61,9 +65,9 @@ async function claimName(
): Promise<string> {
await ensureDirectory(directory)
name = sanitizeFileName(name)
- const ext = extension ? extname(name) : ''
+ let ext = extension ? extname(name) : ''
+ if (Buffer.byteLength(ext) > MAX_EXTENSION_BYTES) ext = ''
const maxBaseBytes = 255 - Buffer.byteLength(` (9999)${suffix}`) - Buffer.byteLength(ext)
- if (maxBaseBytes <= 0) throw new Error('The file extension is too long')
const baseCharacters = Array.from(ext ? basename(name, ext) : name)
while (Buffer.byteLength(baseCharacters.join('')) > maxBaseBytes) baseCharacters.pop()
const base = baseCharacters.join('')