diff --git a/src/core/components/files.js b/src/core/components/files.js index 47cf2d5abb..d6f6d889a2 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -40,10 +40,16 @@ function prepareFile (self, opts, file, callback) { (node, cb) => { const b58Hash = cid.toBaseEncodedString() + let size = node.size + + if (Buffer.isBuffer(node)) { + size = node.length + } + cb(null, { path: opts.wrapWithDirectory ? file.path.substring(WRAPPER.length) : (file.path || b58Hash), hash: b58Hash, - size: node.size + size }) } ], callback) diff --git a/test/core/files.spec.js b/test/core/files.spec.js index d90bddd805..4952c3ecf9 100644 --- a/test/core/files.spec.js +++ b/test/core/files.spec.js @@ -83,5 +83,30 @@ describe('files', () => { done() }) }) + + it('should add a file with a v1 CID', (done) => { + ipfs.files.add(Buffer.from([0, 1, 2]), { + cidVersion: 1 + }, (err, files) => { + expect(err).to.not.exist() + expect(files.length).to.equal(1) + expect(files[0].hash).to.equal('zb2rhiNedvrkpYhcrgtpmhKk5UPzcgizgSXaQLYXNY745BmYP') + expect(files[0].size).to.equal(3) + done() + }) + }) + + it('should add a file with a v1 CID and not raw leaves', (done) => { + ipfs.files.add(Buffer.from([0, 1, 2]), { + cidVersion: 1, + rawLeaves: false + }, (err, files) => { + expect(err).to.not.exist() + expect(files.length).to.equal(1) + expect(files[0].hash).to.equal('zdj7WcDSFNSsZkdkbpSDGeLsBtHbYKyvPQsaw6PpeeYdGqoAx') + expect(files[0].size).to.equal(11) + done() + }) + }) }) }) diff --git a/test/http-api/files.js b/test/http-api/files.js index 921f251e58..4d98d7d4e4 100644 --- a/test/http-api/files.js +++ b/test/http-api/files.js @@ -26,7 +26,14 @@ describe('.files', () => { }) }) - after((done) => ipfsd.stop(done)) + after(function (done) { + this.timeout(20 * 1000) + if (ipfsd) { + ipfsd.stop(done) + } else { + done() + } + }) describe('.add', function () { it('performs a speculative add, --only-hash', () => {