From 82defbdd7b93a229a62df28dc18e246c3407c01a Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 23 Nov 2021 12:12:27 +0000 Subject: [PATCH] Chore: Fixed mobile build --- .../gui/NoteEditor/utils/useMessageHandler.ts | 3 +- packages/lib/package-lock.json | 14 ---- packages/lib/package.json | 1 - packages/lib/urlUtils.js | 74 ++++++++++++++++++- packages/lib/urlUtils.test.js | 29 ++++++-- 5 files changed, 95 insertions(+), 26 deletions(-) diff --git a/packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.ts b/packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.ts index ee38c82513..a96ddb40e4 100644 --- a/packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.ts +++ b/packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.ts @@ -6,6 +6,7 @@ import CommandService from '@joplin/lib/services/CommandService'; import PostMessageService from '@joplin/lib/services/PostMessageService'; import ResourceFetcher from '@joplin/lib/services/ResourceFetcher'; import { reg } from '@joplin/lib/registry'; +import shim from '@joplin/lib/shim'; const bridge = require('@electron/remote').require('./bridge').default; const { urlDecode } = require('@joplin/lib/string-utils'); const urlUtils = require('@joplin/lib/urlUtils'); @@ -58,7 +59,7 @@ export default function useMessageHandler(scrollWhenReady: any, setScrollWhenRea // shell.openPath seems to work with file:// urls on Windows, // but doesn't on macOS, so we need to convert it to a path // before passing it to openPath. - const decodedPath = fileUriToPath(urlDecode(msg)); + const decodedPath = fileUriToPath(urlDecode(msg), shim.platformName()); require('electron').shell.openPath(decodedPath); } else { require('electron').shell.openExternal(msg); diff --git a/packages/lib/package-lock.json b/packages/lib/package-lock.json index 5a4b231bc7..e75e801725 100644 --- a/packages/lib/package-lock.json +++ b/packages/lib/package-lock.json @@ -21,7 +21,6 @@ "diff-match-patch": "^1.0.4", "es6-promise-pool": "^2.5.0", "fast-deep-equal": "^3.1.3", - "file-uri-to-path": "^2.0.0", "follow-redirects": "^1.2.4", "form-data": "^2.1.4", "fs-extra": "^5.0.0", @@ -2954,14 +2953,6 @@ "node": ">=6" } }, - "node_modules/file-uri-to-path": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-2.0.0.tgz", - "integrity": "sha512-hjPFI8oE/2iQPVe4gbrJ73Pp+Xfub2+WI2LlXDbsaJBwT5wuMh35WNWVYYTpnz895shtwfyutMFLFywpQAFdLg==", - "engines": { - "node": ">= 6" - } - }, "node_modules/fill-range": { "version": "7.0.1", "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", @@ -10671,11 +10662,6 @@ "version": "10.11.0", "integrity": "sha512-uzk64HRpUZyTGZtVuvrjP0FYxzQrBf4rojot6J65YMEbwBLB0CWm0CLojVpwpmFmxcE/lkvYICgfcGozbBq6rw==" }, - "file-uri-to-path": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-2.0.0.tgz", - "integrity": "sha512-hjPFI8oE/2iQPVe4gbrJ73Pp+Xfub2+WI2LlXDbsaJBwT5wuMh35WNWVYYTpnz895shtwfyutMFLFywpQAFdLg==" - }, "fill-range": { "version": "7.0.1", "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", diff --git a/packages/lib/package.json b/packages/lib/package.json index cc09f74b44..1d2ddd07c1 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -46,7 +46,6 @@ "diff-match-patch": "^1.0.4", "es6-promise-pool": "^2.5.0", "fast-deep-equal": "^3.1.3", - "file-uri-to-path": "^2.0.0", "follow-redirects": "^1.2.4", "form-data": "^2.1.4", "fs-extra": "^5.0.0", diff --git a/packages/lib/urlUtils.js b/packages/lib/urlUtils.js index 052dd512d4..f7bf754bf5 100644 --- a/packages/lib/urlUtils.js +++ b/packages/lib/urlUtils.js @@ -1,6 +1,5 @@ const { rtrimSlashes } = require('./path-utils'); const { urlDecode } = require('./string-utils'); -const fileUriToPath = require('file-uri-to-path'); const urlUtils = {}; @@ -106,8 +105,75 @@ urlUtils.objectToQueryString = function(query) { return queryString; }; -urlUtils.fileUriToPath = (path) => { - const output = fileUriToPath(path); +// This is a modified version of the file-uri-to-path package: +// +// - It removes the dependency to the "path" package, which wouldn't work with +// React Native. +// +// - It always returns paths with forward slashes "/". This is normally handled +// properly everywhere. +// +// - Adds the "platform" parameter to optionall return paths with "\" for win32 +function fileUriToPath_(uri, platform) { + const sep = '/'; + + if ( + typeof uri !== 'string' || + uri.length <= 7 || + uri.substring(0, 7) !== 'file://' + ) { + throw new TypeError( + 'must pass in a file:// URI to convert to a file path' + ); + } + + const rest = decodeURI(uri.substring(7)); + const firstSlash = rest.indexOf('/'); + let host = rest.substring(0, firstSlash); + let path = rest.substring(firstSlash + 1); + + // 2. Scheme Definition + // As a special case, can be the string "localhost" or the empty + // string; this is interpreted as "the machine from which the URL is + // being interpreted". + if (host === 'localhost') { + host = ''; + } + + if (host) { + host = sep + sep + host; + } + + // 3.2 Drives, drive letters, mount points, file system root + // Drive letters are mapped into the top of a file URI in various ways, + // depending on the implementation; some applications substitute + // vertical bar ("|") for the colon after the drive letter, yielding + // "file:///c|/tmp/test.txt". In some cases, the colon is left + // unchanged, as in "file:///c:/tmp/test.txt". In other cases, the + // colon is simply omitted, as in "file:///c/tmp/test.txt". + path = path.replace(/^(.+)\|/, '$1:'); + + // for Windows, we need to invert the path separators from what a URI uses + // if (sep === '\\') { + // path = path.replace(/\//g, '\\'); + // } + + if (/^.+:/.test(path)) { + // has Windows drive at beginning of path + } else { + // unix path… + path = sep + path; + } + + if (platform === 'win32') { + return (host + path).replace(/\//g, '\\'); + } else { + return host + path; + } +} + +urlUtils.fileUriToPath = (path, platform = 'linux') => { + const output = fileUriToPath_(path, platform); // The file-uri-to-path module converts Windows path such as // @@ -129,7 +195,7 @@ urlUtils.fileUriToPath = (path) => { // // https://github.com/laurent22/joplin/issues/5693 - if (output.match(/^\\\\[a-zA-Z]:/)) { + if (output.match(/^\/\/[a-zA-Z]:/)) { return output.substr(2); } diff --git a/packages/lib/urlUtils.test.js b/packages/lib/urlUtils.test.js index 303a5ef3c4..03dce86a27 100644 --- a/packages/lib/urlUtils.test.js +++ b/packages/lib/urlUtils.test.js @@ -72,12 +72,29 @@ describe('urlUtils', function() { })); it('should convert a file URI to a file path', (async () => { - // This function is a wrapper around the file-uri-to-path module, - // with a fix for the Windows paths. We assume that the wrapped - // function works so we don't test everything, only the cases - // specific to our wrapper. - expect(urlUtils.fileUriToPath('file://c:/not/quite/right')).toBe('c:\\not\\quite\\right'); - expect(urlUtils.fileUriToPath('file:///d:/better')).toBe('d:\\better'); + // Tests imported from https://github.com/TooTallNate/file-uri-to-path/tree/master/test + const testCases = { + 'file://host/path': '//host/path', + 'file://localhost/etc/fstab': '/etc/fstab', + 'file:///etc/fstab': '/etc/fstab', + 'file:///c:/WINDOWS/clock.avi': 'c:/WINDOWS/clock.avi', + 'file://localhost/c|/WINDOWS/clock.avi': 'c:/WINDOWS/clock.avi', + 'file:///c|/WINDOWS/clock.avi': 'c:/WINDOWS/clock.avi', + 'file://localhost/c:/WINDOWS/clock.avi': 'c:/WINDOWS/clock.avi', + 'file://hostname/path/to/the%20file.txt': '//hostname/path/to/the file.txt', + 'file:///c:/path/to/the%20file.txt': 'c:/path/to/the file.txt', + 'file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc': 'C:/Documents and Settings/davris/FileSchemeURIs.doc', + 'file:///C:/caf%C3%A9/%C3%A5r/d%C3%BCnn/%E7%89%9B%E9%93%83/Ph%E1%BB%9F/%F0%9F%98%B5.exe': 'C:/café/år/dünn/牛铃/Phở/😵.exe', + }; + + for (const [input, expected] of Object.entries(testCases)) { + const actual = urlUtils.fileUriToPath(input); + expect(actual).toBe(expected); + } + + expect(urlUtils.fileUriToPath('file://c:/not/quite/right')).toBe('c:/not/quite/right'); + expect(urlUtils.fileUriToPath('file:///d:/better')).toBe('d:/better'); + expect(urlUtils.fileUriToPath('file:///c:/AUTOEXEC.BAT', 'win32')).toBe('c:\\AUTOEXEC.BAT'); })); });