Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

fix: decode a link's file name when an earlier folder has a bad escape (#178)

Rudy Mizrahi Celekli481e5299638a
diff --git a/e2e/probeFileName.spec.ts b/e2e/probeFileName.spec.ts
new file mode 100644
index 0000000..f3e589b
--- /dev/null
+++ b/e2e/probeFileName.spec.ts
@@ -0,0 +1,9 @@
+import { test, expect, BLOCK } from './fixtures'
+
+test.describe('probe file name from the link @smoke', () => {
+ test('a bad escape in a folder still decodes the file name', async ({ plexo, serve }) => {
+ const origin = await serve({ size: 3 * BLOCK })
+ const result = await plexo.api.probeUrl(origin.url('/50%/My%20Report.pdf'))
+ expect(result.suggestedFileName).toBe('My Report.pdf')
+ })
+})
diff --git a/src/main/download/probe.ts b/src/main/download/probe.ts
index 81d795a..deccb01 100644
--- a/src/main/download/probe.ts
+++ b/src/main/download/probe.ts
@@ -80,13 +80,14 @@ function fileNameFromHeaders(headers: Headers, url: URL): string {
const parsed = parseContentDispositionFilename(disposition)
if (parsed) return parsed
}
- let pathname = url.pathname
+ // Only the last segment is decoded: a malformed %-escape in a folder doesn't keep the file's
+ // name escaped, and an escaped slash stays part of the name.
+ let base = url.pathname.split('/').filter(Boolean).pop()
try {
- pathname = decodeURIComponent(pathname)
+ if (base) base = decodeURIComponent(base)
} catch {
- // A malformed %-escape: the raw path still names the file well enough.
+ // A malformed %-escape: the raw name still names the file well enough.
}
- const base = pathname.split('/').filter(Boolean).pop()
return base && base.length > 0 ? base : 'download'
}