[plugin cli] Fix file:/// paths on Windows

This commit is contained in:
Jonathan Budzenski 2017-01-26 11:02:24 -06:00
parent 2796e7798b
commit f47bc1e802
No known key found for this signature in database
GPG key ID: D28BF9418FA0F292
2 changed files with 27 additions and 2 deletions

View file

@ -6,7 +6,7 @@ import rimraf from 'rimraf';
import mkdirp from 'mkdirp';
import Logger from '../../lib/logger';
import { UnsupportedProtocolError } from '../../lib/errors';
import { download, _downloadSingle } from '../download';
import { download, _downloadSingle, _getFilePath } from '../download';
import { join } from 'path';
describe('kibana cli', function () {
@ -133,6 +133,22 @@ describe('kibana cli', function () {
});
describe('_getFilePath', function () {
it('should decode paths', function () {
expect(_getFilePath('Test%20folder/file.zip')).to.equal('Test folder/file.zip');
});
it('should remove the leading slash from windows paths', function () {
const platform = Object.getOwnPropertyDescriptor(process, 'platform');
Object.defineProperty(process, 'platform', { value: 'win32' });
expect(_getFilePath('/C:/foo/bar')).to.equal('C:/foo/bar');
Object.defineProperty(process, 'platform', platform);
});
});
describe('download', function () {
it('should loop through bad urls until it finds a good one.', function () {
const filePath = join(__dirname, 'replies/test_plugin.zip');

View file

@ -3,12 +3,21 @@ import downloadLocalFile from './downloaders/file';
import { UnsupportedProtocolError } from '../lib/errors';
import { parse } from 'url';
export function _getFilePath(filePath) {
const decodedPath = decodeURI(filePath);
const isWindows = /^win/.test(process.platform);
if (isWindows) {
return decodedPath.replace(/^\//, '');
}
return decodedPath;
}
export function _downloadSingle(settings, logger, sourceUrl) {
const urlInfo = parse(sourceUrl);
let downloadPromise;
if (/^file/.test(urlInfo.protocol)) {
downloadPromise = downloadLocalFile(logger, decodeURI(urlInfo.path), settings.tempArchiveFile);
downloadPromise = downloadLocalFile(logger, _getFilePath(urlInfo.path), settings.tempArchiveFile);
} else if (/^https?/.test(urlInfo.protocol)) {
downloadPromise = downloadHttpFile(logger, sourceUrl, settings.tempArchiveFile, settings.timeout);
} else {