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

Commit

Permalink
fix(files.add): handle weird directory names (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider authored and daviddias committed Dec 17, 2017
1 parent 5c254eb commit 012b86c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
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

0 comments on commit 012b86c

Please sign in to comment.