Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

fix(files.add): handle weird directory names #646

Merged
merged 1 commit into from
Dec 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"detect-node": "^2.0.3",
"flatmap": "0.0.3",
"glob": "^7.1.2",
"glob-escape": "0.0.2",
"ipfs-block": "~0.6.1",
"ipfs-unixfs": "~0.1.14",
"ipld-dag-pb": "~0.11.3",
Expand Down
39 changes: 16 additions & 23 deletions src/utils/prepare-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

const isNode = require('detect-node')
const flatmap = require('flatmap')
const escape = require('glob-escape')

function strip (name, base) {
const smallBase = base
.split('/')
.slice(0, -1)
.join('/') + '/'
return name.replace(smallBase, '')
}

function loadPaths (opts, file) {
const path = require('path')
Expand All @@ -29,41 +20,43 @@ function loadPaths (opts, file) {
if (stats.isDirectory() && opts.recursive) {
// glob requires a POSIX filename
file = file.split(path.sep).join('/')
const globEscapedDir = escape(file) + (file.endsWith('/') ? '' : '/')
const mg = new glob.sync.GlobSync(`${globEscapedDir}` + '**/*', {
const fullDir = file + (file.endsWith('/') ? '' : '/')
let dirName = fullDir.split('/')
dirName = dirName[dirName.length - 2] + '/'
const mg = new glob.sync.GlobSync('**/*', {
cwd: file,
follow: followSymlinks,
dot: opts.hidden,
ignore: (opts.ignore || []).map((ignoreGlob) => {
return globEscapedDir + ignoreGlob
})
ignore: opts.ignore
})

return mg.found
.map((name) => {
const fqn = fullDir + name
// symlinks
if (mg.symlinks[name] === true) {
if (mg.symlinks[fqn] === true) {
return {
path: strip(name, file),
path: dirName + name,
symlink: true,
dir: false,
content: fs.readlinkSync(name)
content: fs.readlinkSync(fqn)
}
}

// files
if (mg.cache[name] === 'FILE') {
if (mg.cache[fqn] === 'FILE') {
return {
path: strip(name, file),
path: dirName + name,
symlink: false,
dir: false,
content: fs.createReadStream(name)
content: fs.createReadStream(fqn)
}
}

// directories
if (mg.cache[name] === 'DIR' || mg.cache[name] instanceof Array) {
if (mg.cache[fqn] === 'DIR' || mg.cache[fqn] instanceof Array) {
return {
path: strip(name, file),
path: dirName + name,
symlink: false,
dir: true
}
Expand All @@ -86,7 +79,7 @@ function prepareFile (file, opts) {
return flatmap(files, (file) => {
if (typeof file === 'string') {
if (!isNode) {
throw new Error('Can not add paths in node')
throw new Error('Can only add file paths in node')
}

return loadPaths(opts, file)
Expand Down