Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit ffff919

Browse files
committed
feat: pass file name to add/addAll progress handler
Since ipfs/js-ipfs-unixfs#87 landed we can now pass the file name to the progress handler for adding files: ```js await ipfs.addAll(..., { progress: (bytes, fileName) => { //... } }) ``` This should make showing progress a bit more usable.
1 parent 308baa4 commit ffff919

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

docs/core-api/FILES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ An optional object which may have the following keys:
252252
| hashAlg | `String` | `'sha2-256'` | multihash hashing algorithm to use |
253253
| onlyHash | `boolean` | `false` | If true, will not add blocks to the blockstore |
254254
| pin | `boolean` | `true` | pin this object when adding |
255-
| progress | function | `undefined` | a function that will be called with the byte length of chunks as a file is added to ipfs |
255+
| progress | function | `undefined` | a function that will be called with the number of bytes added as a file is added to ipfs and the name of the file being added |
256256
| rawLeaves | `boolean` | `false` | if true, DAG leaves will contain raw file data and not be wrapped in a protobuf |
257257
| shardSplitThreshold | `Number` | `1000` | Directories with more than this number of files will be created as HAMT-sharded directories |
258258
| trickle | `boolean` | `false` | if true will use the [trickle DAG](https://godoc.org/github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/go-unixfs/importer/trickle) format for DAG generation |

packages/interface-ipfs-core/src/add-all.js

+20
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ module.exports = (common, options) => {
170170
expect(root.cid.toString()).to.equal(fixtures.directory.cid)
171171
})
172172

173+
it('should receive file name from progress event', async () => {
174+
const receivedNames = []
175+
function handler (p, name) {
176+
receivedNames.push(name)
177+
}
178+
179+
await drain(ipfs.add([{
180+
content: 'hello',
181+
path: 'foo.txt'
182+
}, {
183+
content: 'world',
184+
path: 'bar.txt'
185+
}], {
186+
progress: handler,
187+
wrapWithDirectory: true
188+
}))
189+
190+
expect(receivedNames).to.deep.equal(['foo.txt', 'bar.txt'])
191+
})
192+
173193
it('should add files to a directory non sequentially', async function () {
174194
const content = path => ({
175195
path: `test-dir/${path}`,

packages/interface-ipfs-core/src/add.js

+14
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ module.exports = (common, options) => {
130130
expect(accumProgress).to.equal(fixtures.emptyFile.data.length)
131131
})
132132

133+
it('should receive file name from progress event', async () => {
134+
let receivedName
135+
function handler (p, name) {
136+
receivedName = name
137+
}
138+
139+
await ipfs.add({
140+
content: 'hello',
141+
path: 'foo.txt'
142+
}, { progress: handler })
143+
144+
expect(receivedName).to.equal('foo.txt')
145+
})
146+
133147
it('should add an empty file without progress enabled', async () => {
134148
const file = await ipfs.add(fixtures.emptyFile.data)
135149

packages/ipfs-core/src/components/add-all/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ module.exports = ({ block, gcLock, preload, pin, options: constructorOptions })
4747
let total = 0
4848
const prog = opts.progress
4949

50-
opts.progress = (bytes) => {
50+
opts.progress = (bytes, file) => {
5151
total += bytes
52-
prog(total)
52+
prog(total, file)
5353
}
5454
}
5555

@@ -162,8 +162,8 @@ function pinFile (pin, opts) {
162162
* @property {boolean} [onlyHash=false] - If true, will not add blocks to the
163163
* blockstore.
164164
* @property {boolean} [pin=true] - Pin this object when adding.
165-
* @property {(bytes:number) => void} [progress] - A function that will be
166-
* called with the byte length of chunks as a file is added to ipfs.
165+
* @property {(bytes:number, fileName:string) => void} [progress] - A function that will be
166+
* called with the number of bytes added as a file is added to ipfs and the name of the file being added.
167167
* @property {boolean} [rawLeaves=false] - If true, DAG leaves will contain raw
168168
* file data and not be wrapped in a protobuf.
169169
* @property {number} [shardSplitThreshold=1000] - Directories with more than this

packages/ipfs-http-client/src/add-all.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = configure((api) => {
3838
if (file.hash !== undefined) {
3939
yield toCoreInterface(file)
4040
} else if (progressFn) {
41-
progressFn(file.bytes || 0)
41+
progressFn(file.bytes || 0, file.name)
4242
}
4343
}
4444
}

0 commit comments

Comments
 (0)