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

Commit 28235b0

Browse files
authored
fix: ipfs get with raw blocks (#3683)
- 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 <[email protected]>
1 parent 1c314a2 commit 28235b0

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ module.exports = (common, options) => {
8686
expect(uint8ArrayConcat(await all(output[0].content))).to.eql(input)
8787
})
8888

89+
it('should get a file added as CIDv1 with rawLeaves', async () => {
90+
const input = uint8ArrayFromString(`TEST${Math.random()}`)
91+
92+
const res = await all(importer([{ content: input }], ipfs.block, { cidVersion: 1, rawLeaves: true }))
93+
94+
const cidv1 = res[0].cid
95+
expect(cidv1.version).to.equal(1)
96+
97+
const output = await all(ipfs.get(cidv1))
98+
expect(output[0].type).to.eql('file')
99+
expect(uint8ArrayConcat(await all(output[0].content))).to.eql(input)
100+
})
101+
89102
it('should get a BIG file', async () => {
90103
for await (const file of ipfs.get(fixtures.bigFile.cid)) {
91104
expect(file.path).to.equal(fixtures.bigFile.cid)

packages/ipfs-core/src/utils.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -96,36 +96,45 @@ const resolvePath = async function (ipld, ipfsPath, options = {}) {
9696
* @param {boolean} [options.includeContent]
9797
*/
9898
const mapFile = (file, options = {}) => {
99+
if (file.type !== 'file' && file.type !== 'directory' && file.type !== 'raw') {
100+
// file.type === object | identity not supported yet
101+
throw new Error(`Unknown node type '${file.type}'`)
102+
}
103+
99104
/** @type {import('ipfs-core-types/src/root').IPFSEntry} */
100105
const output = {
101106
cid: file.cid,
102107
path: file.path,
103108
name: file.name,
104109
depth: file.path.split('/').length,
105-
size: 0,
110+
size: file.size,
106111
type: 'file'
107112
}
108113

109-
if (file.type === 'file' || file.type === 'directory') {
114+
if (file.type === 'directory') {
110115
// @ts-ignore - TS type can't be changed from File to Directory
111-
output.type = file.type === 'directory' ? 'dir' : 'file'
112-
113-
if (file.type === 'file') {
114-
output.size = file.unixfs.fileSize()
116+
output.type = 'dir'
117+
}
115118

116-
if (options.includeContent) {
117-
// @ts-expect-error - content is readonly
118-
output.content = file.content()
119-
}
120-
}
119+
if (file.type === 'file') {
120+
output.size = file.unixfs.fileSize()
121+
}
121122

123+
if (file.type === 'file' || file.type === 'directory') {
122124
output.mode = file.unixfs.mode
123125

124126
if (file.unixfs.mtime !== undefined) {
125127
output.mtime = file.unixfs.mtime
126128
}
127129
}
128130

131+
if (options.includeContent) {
132+
if (file.type === 'file' || file.type === 'raw') {
133+
// @ts-expect-error - content is readonly
134+
output.content = file.content()
135+
}
136+
}
137+
129138
return output
130139
}
131140

0 commit comments

Comments
 (0)