-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The fix herewas to normalise and fix how we calculated: - the paths within a dropped folder (FF was getting doubled wrapped in the root dir) - the number of dirs that would be created for a given tree, so we can correctly calculate if we got the right number of items back in the call to ipfs.add ...and removing the unused wrapping dir when adding. License: MIT Signed-off-by: Oli Evans <oli@tableflip.io>
- Loading branch information
Showing
5 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { dirname } from 'path' | ||
|
||
/** | ||
* countDirs: find all the dirs that will be created from a list of paths. | ||
* | ||
* files is an array of file objects as passed to ipfs.added | ||
* The root dir is counted, and All entries are assumed to be file paths, | ||
* `/foo` is assumed to be a file `foo` with no extention in the root dir, | ||
* which would be counted as 1 unigue dir by this function. | ||
* | ||
* ```js | ||
* files = [ | ||
* { path: '/foo/bar/foo.txt', ... } | ||
* { path: '/foo/bar/odd', ... } | ||
* ] | ||
* countDirs(files) === 3 | ||
* // ['/', '/foo', '/foo/bar'] | ||
* ``` | ||
* | ||
* We need to calculat how many directories are in the tree. | ||
* | ||
* See: https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#add | ||
*/ | ||
function countDirs (files) { | ||
if (!files || !files.length) return 0 | ||
const paths = files.map(f => f.path) | ||
.filter(p => !!p) | ||
|
||
// [ /foo/bar, /foo/other, /foo/zoom, /aaa/other ] | ||
const directories = new Set() | ||
paths.forEach(path => findUniqueDirectories(path, directories)) | ||
return directories.size | ||
} | ||
|
||
function findUniqueDirectories (path, res = new Set()) { | ||
if (!path) return res | ||
const name = dirname(path) | ||
if (name === '.') return res | ||
res.add(name) | ||
if (name === '/') return res | ||
return findUniqueDirectories(name, res) | ||
} | ||
|
||
export default countDirs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* global it, expect */ | ||
import countDirs from './count-dirs' | ||
|
||
it('should return 1 for the root dir', () => { | ||
expect(countDirs([{ path: '/' }])).toBe(1) | ||
}) | ||
|
||
it('should return 2 for the root dir with a sub dir', () => { | ||
expect(countDirs([{ path: '/foo/x' }])).toBe(2) | ||
}) | ||
|
||
it('should return 1 for the root dir with an extentionless file', () => { | ||
expect(countDirs([{ path: '/foo' }])).toBe(1) | ||
}) | ||
|
||
it('should return 0 for if files is empty', () => { | ||
expect(countDirs([])).toBe(0) | ||
}) | ||
|
||
it('should return 0 for if files is missing', () => { | ||
expect(countDirs()).toBe(0) | ||
}) | ||
|
||
it('should return 0 for a file name', () => { | ||
expect(countDirs([{ path: 'Master layout FINAL v18(2).pdf' }])).toBe(0) | ||
}) | ||
|
||
it('should deal with relative paths', () => { | ||
expect(countDirs([{ path: 'home/www/index.html' }])).toBe(2) | ||
}) | ||
|
||
it('should count the unique dirs in a list of file objects', () => { | ||
let files = [ | ||
{ path: '/foo/bar/foo.txt' }, | ||
{ path: '/foo/bar/odd.txt' }, | ||
{ path: '/foo/other/odd.txt' }, | ||
{ path: '/foo/zoom/x.txt' }, | ||
{ path: '/aaa/other/y.txt' }, | ||
{ path: '/aaa/bar/y.txt' } | ||
] | ||
|
||
expect(countDirs(files)).toBe(8) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters