Skip to content
Permalink
Browse files

Fix: Support for files with spaces in the path

Fix #3401
Close #3409
  • Loading branch information
sarvaje authored and molant committed Dec 2, 2019
1 parent 10e4024 commit 0b46a4ff0c58730336c8a136e798b33dbbbe51b1
Showing with 8 additions and 9 deletions.
  1. +2 −2 packages/connector-local/src/connector.ts
  2. +6 −7 packages/utils-network/src/as-uri.ts
@@ -353,7 +353,7 @@ export default class LocalConnector implements IConnector {
* and then get the path string.
*/
const uri = getAsUri(target);
const filePath: string = uri ? asPathString(uri) : '';
const filePath: string = uri ? asPathString(uri).replace('%20', ' ') : '';
const rawContent: Buffer = options && options.content ? Buffer.from(options.content) : await readFileAsBuffer(filePath);
const contentType = getContentTypeData(null as any, filePath, null, rawContent);
let content = '';
@@ -395,7 +395,7 @@ export default class LocalConnector implements IConnector {

this.engine.emitAsync('scan::start', initialEvent);

const pathString = asPathString(target);
const pathString = asPathString(target).replace('%20', ' ');
let files: string[];

if (isFile(pathString)) {
@@ -17,11 +17,10 @@ const debug: debug.IDebugger = d(__filename);
* * null if not valid
*/
export const getAsUri = (source: string): URL | null => {
const entry: string = source.trim();
let target: URL | null;

try {
target = new URL(entry);
target = new URL(source);
} catch (err) {
/* istanbul ignore next */
{ // eslint-disable-line no-lone-blocks
@@ -46,29 +45,29 @@ export const getAsUri = (source: string): URL | null => {
* If it's not a URI
* If it does exist and it's a regular file.
*/
if (isFile(entry) || isDirectory(entry)) {
target = new URL(fileUrl(entry));
if (isFile(source) || isDirectory(source)) {
target = new URL(fileUrl(source));
debug(`Adding valid target: ${url.format(target)}`);

return target;
}

target = new URL(`http://${entry}`);
target = new URL(`http://${source}`);

/*
* And it doesn't exist locally, and is a valid URL:
* Except for the case of the well known and used `localhost`,
* for all other cases the `hostname` needs to contain at least
* a `.`. Private domains should have `http(s)://` in front.
*/
if (!pathExists(entry) && (target.hostname === 'localhost' || target.hostname.includes('.'))) {
if (!pathExists(source) && (target.hostname === 'localhost' || target.hostname.includes('.'))) {
debug(`Adding modified target: ${url.format(target)}`);

return target;
}

// If it's not a regular file or looks like a URL, ignore it.
logger.error(`Ignoring '${entry}' as it's not an existing file nor a valid URL`);
logger.error(`Ignoring '${source}' as it's not an existing file nor a valid URL`);

return null;
};

0 comments on commit 0b46a4f

Please sign in to comment.
You can’t perform that action at this time.