Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

fix: shorten a download path under home when the name's accents differ (#185)

Rudy Mizrahi Celeklia101497ca0db
diff --git a/e2e/displayPath.spec.ts b/e2e/displayPath.spec.ts
new file mode 100644
index 0000000..9f94f25
--- /dev/null
+++ b/e2e/displayPath.spec.ts
@@ -0,0 +1,14 @@
+import { expect, test } from '@playwright/test'
+import { toDisplayPath } from '../src/renderer/src/utils/format'
+
+test("a download under home still shortens when the folder's accents differ", () => {
+ const home = '/Users/caf\u00e9'
+ const path = '/Users/cafe\u0301/Downloads'
+ expect(toDisplayPath(path, home)).toBe('~/Downloads')
+ expect(toDisplayPath('C:\\Users\\Me\\Downloads', 'C:\\Users\\Me')).toBe('~\\Downloads')
+ expect(toDisplayPath('/Users/Me/Downloads', '/Users/Me')).toBe('~/Downloads')
+ expect(toDisplayPath('/Users/me', '/Users/me')).toBe('~')
+ expect(toDisplayPath('/Users/me2/Downloads', '/Users/me')).toBe('/Users/me2/Downloads')
+ const other = '/tmp/cafe\u0301/file'
+ expect(toDisplayPath(other, home)).toBe(other)
+})
diff --git a/src/renderer/src/utils/format.ts b/src/renderer/src/utils/format.ts
index 12a8abb..57f0f29 100644
--- a/src/renderer/src/utils/format.ts
+++ b/src/renderer/src/utils/format.ts
@@ -253,20 +253,27 @@ export function networkActive(group: NetworkGroup): boolean {
: group.peers.some((peer) => peer.status === 'receiving')
}
+function canonicalPath(value: string): string {
+ return value.normalize('NFC').replace(/\\/g, '/').replace(/\/$/, '')
+}
+
+function windowsPath(value: string): boolean {
+ return /^[a-z]:/i.test(value) || value.startsWith('//')
+}
+
/** Shortens an absolute path under the user's home directory to a "~/..." form for display. */
export function toDisplayPath(path: string, homeDir: string): string {
- const normalize = (value: string): string => {
- const normalized = value.replace(/\\/g, '/').replace(/\/$/, '')
- return /^[a-z]:/i.test(normalized) || normalized.startsWith('//')
- ? normalized.toLowerCase()
- : normalized
- }
- const home = normalize(homeDir)
- const target = normalize(path)
- if (home && (target === home || target.startsWith(`${home}/`))) {
- return `~${path.slice(home.length)}`
- }
- return path
+ const home = canonicalPath(homeDir)
+ const target = canonicalPath(path)
+ if (!home) return path
+ const fold = (value: string): string => (windowsPath(value) ? value.toLowerCase() : value)
+ const homeKey = fold(home)
+ const targetKey = fold(target)
+ if (targetKey !== homeKey && !targetKey.startsWith(`${homeKey}/`)) return path
+ const suffix = target.slice(home.length)
+ if (!suffix) return '~'
+ const sep = path.includes('\\') ? '\\' : '/'
+ return `~${suffix.replace(/\//g, sep)}`
}
export { describeError } from '@shared/errors'