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

Commit

Permalink
fix: ipfs get with raw blocks (#3683)
Browse files Browse the repository at this point in the history
- tweak ipfs-cli get to handle `raw`
- tweak ipfs-core mapFile to handle `raw`
- mapFile doesn't handle all mapping object or identity types to an IPFSEntry, so throw if it finds one. This makes the cli behaviour match go-ipfs for ipfs get <cbor cid>
- add interface-core test for get with rawLeaves: true
- remove cli test for "raw" as core normalises the type to "file" so it was not testing a valid conditon

fixes #3682

License: MIT
Signed-off-by: Oli Evans <oli@tableflip.io>
  • Loading branch information
olizilla committed May 11, 2021
1 parent 1c314a2 commit 28235b0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
13 changes: 13 additions & 0 deletions packages/interface-ipfs-core/src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ module.exports = (common, options) => {
expect(uint8ArrayConcat(await all(output[0].content))).to.eql(input)
})

it('should get a file added as CIDv1 with rawLeaves', async () => {
const input = uint8ArrayFromString(`TEST${Math.random()}`)

const res = await all(importer([{ content: input }], ipfs.block, { cidVersion: 1, rawLeaves: true }))

const cidv1 = res[0].cid
expect(cidv1.version).to.equal(1)

const output = await all(ipfs.get(cidv1))
expect(output[0].type).to.eql('file')
expect(uint8ArrayConcat(await all(output[0].content))).to.eql(input)
})

it('should get a BIG file', async () => {
for await (const file of ipfs.get(fixtures.bigFile.cid)) {
expect(file.path).to.equal(fixtures.bigFile.cid)
Expand Down
31 changes: 20 additions & 11 deletions packages/ipfs-core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,36 +96,45 @@ const resolvePath = async function (ipld, ipfsPath, options = {}) {
* @param {boolean} [options.includeContent]
*/
const mapFile = (file, options = {}) => {
if (file.type !== 'file' && file.type !== 'directory' && file.type !== 'raw') {
// file.type === object | identity not supported yet
throw new Error(`Unknown node type '${file.type}'`)
}

/** @type {import('ipfs-core-types/src/root').IPFSEntry} */
const output = {
cid: file.cid,
path: file.path,
name: file.name,
depth: file.path.split('/').length,
size: 0,
size: file.size,
type: 'file'
}

if (file.type === 'file' || file.type === 'directory') {
if (file.type === 'directory') {
// @ts-ignore - TS type can't be changed from File to Directory
output.type = file.type === 'directory' ? 'dir' : 'file'

if (file.type === 'file') {
output.size = file.unixfs.fileSize()
output.type = 'dir'
}

if (options.includeContent) {
// @ts-expect-error - content is readonly
output.content = file.content()
}
}
if (file.type === 'file') {
output.size = file.unixfs.fileSize()
}

if (file.type === 'file' || file.type === 'directory') {
output.mode = file.unixfs.mode

if (file.unixfs.mtime !== undefined) {
output.mtime = file.unixfs.mtime
}
}

if (options.includeContent) {
if (file.type === 'file' || file.type === 'raw') {
// @ts-expect-error - content is readonly
output.content = file.content()
}
}

return output
}

Expand Down

0 comments on commit 28235b0

Please sign in to comment.