Fine-tune file protocol filter
This commit is contained in:
parent
06b0544bbe
commit
eec61f5289
3 changed files with 38 additions and 16 deletions
|
@ -1,23 +1,29 @@
|
|||
const path = require('path');
|
||||
|
||||
const FILE_SCHEME = /^file:\/\//;
|
||||
const WINDOWS_PREFIX = /^\/[A-Z]:/;
|
||||
function _urlToPath(targetUrl) {
|
||||
let withoutScheme = targetUrl.replace(FILE_SCHEME, '');
|
||||
if (WINDOWS_PREFIX.test(withoutScheme)) {
|
||||
withoutScheme = withoutScheme.slice(1);
|
||||
function _eliminateAllAfterCharacter(string, character) {
|
||||
const index = string.indexOf(character);
|
||||
if (index < 0) {
|
||||
return string;
|
||||
}
|
||||
|
||||
const withoutQuerystring = withoutScheme.replace(/\?.*$/, '');
|
||||
const withoutHash = withoutQuerystring.replace(/#.*$/, '');
|
||||
|
||||
return decodeURIComponent(withoutHash);
|
||||
return string.slice(0, index);
|
||||
}
|
||||
|
||||
function _createFileHandler({ userDataPath, installPath }) {
|
||||
function _urlToPath(targetUrl, options = {}) {
|
||||
const { isWindows } = options;
|
||||
|
||||
const decoded = decodeURIComponent(targetUrl);
|
||||
const withoutScheme = decoded.slice(isWindows ? 8 : 7);
|
||||
const withoutQuerystring = _eliminateAllAfterCharacter(withoutScheme, '?');
|
||||
const withoutHash = _eliminateAllAfterCharacter(withoutQuerystring, '#');
|
||||
|
||||
return withoutHash;
|
||||
}
|
||||
|
||||
function _createFileHandler({ userDataPath, installPath, isWindows }) {
|
||||
return (request, callback) => {
|
||||
// normalize() is primarily useful here for switching / to \ on windows
|
||||
const target = path.normalize(_urlToPath(request.url));
|
||||
const target = path.normalize(_urlToPath(request.url, { isWindows }));
|
||||
|
||||
if (!path.isAbsolute(target)) {
|
||||
return callback();
|
||||
|
@ -34,10 +40,15 @@ function _createFileHandler({ userDataPath, installPath }) {
|
|||
};
|
||||
}
|
||||
|
||||
function installFileHandler({ protocol, userDataPath, installPath }) {
|
||||
function installFileHandler({
|
||||
protocol,
|
||||
userDataPath,
|
||||
installPath,
|
||||
isWindows,
|
||||
}) {
|
||||
protocol.interceptFileProtocol(
|
||||
'file',
|
||||
_createFileHandler({ userDataPath, installPath })
|
||||
_createFileHandler({ userDataPath, installPath, isWindows })
|
||||
);
|
||||
}
|
||||
|
||||
|
|
1
main.js
1
main.js
|
@ -452,6 +452,7 @@ app.on('ready', () => {
|
|||
protocol: electronProtocol,
|
||||
userDataPath,
|
||||
installPath,
|
||||
isWindows: process.platform === 'win32',
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -34,12 +34,13 @@ describe('Protocol Filter', () => {
|
|||
expect(actual).to.equal(expected);
|
||||
});
|
||||
|
||||
it('returns proper file path for windows style file URI', () => {
|
||||
it('returns proper file path for file URI on windows', () => {
|
||||
const path =
|
||||
'file:///C:/Users/Someone/dev/desktop/background.html?name=Signal&locale=en&version=2.4.0';
|
||||
const expected = 'C:/Users/Someone/dev/desktop/background.html';
|
||||
const isWindows = true;
|
||||
|
||||
const actual = _urlToPath(path, { isWindows: true });
|
||||
const actual = _urlToPath(path, { isWindows });
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
|
||||
|
@ -72,6 +73,15 @@ describe('Protocol Filter', () => {
|
|||
expect(actual).to.equal(expected);
|
||||
});
|
||||
|
||||
it('handles SMB share path on windows', () => {
|
||||
const path = 'file://relative/path';
|
||||
const expected = 'elative/path';
|
||||
const isWindows = true;
|
||||
|
||||
const actual = _urlToPath(path, { isWindows });
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
|
||||
it('hands back a path with .. in it', () => {
|
||||
const path = 'file://../../..';
|
||||
const expected = '../../..';
|
||||
|
|
Loading…
Reference in a new issue