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

Commit 11cb4ca

Browse files
victorbdaviddias
authored andcommitted
feat(files): get interface-ipfs-core files tests pass through http-api
using ipfs-api
1 parent e29f429 commit 11cb4ca

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

src/http-api/resources/files.js

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const bs58 = require('bs58')
44
const ndjson = require('ndjson')
55
const multipart = require('ipfs-multipart')
66
const debug = require('debug')
7+
const tar = require('tar-stream')
78
const log = debug('http-api:files')
89
log.error = debug('http-api:files:error')
10+
const async = require('async')
911

1012
exports = module.exports
1113

@@ -44,8 +46,55 @@ exports.cat = {
4446
Code: 0
4547
}).code(500)
4648
}
49+
return reply(stream).header('X-Stream-Output', '1')
50+
})
51+
}
52+
}
53+
54+
exports.get = {
55+
// uses common parseKey method that returns a `key`
56+
parseArgs: exports.parseKey,
57+
58+
// main route handler which is called after the above `parseArgs`, but only if the args were valid
59+
handler: (request, reply) => {
60+
const key = request.pre.args.key
61+
62+
request.server.app.ipfs.files.get(key, (err, stream) => {
63+
if (err) {
64+
log.error(err)
65+
return reply({
66+
Message: 'Failed to get file: ' + err,
67+
Code: 0
68+
}).code(500)
69+
}
70+
var pack = tar.pack()
71+
const files = []
4772
stream.on('data', (data) => {
48-
return reply(data)
73+
files.push(data)
74+
})
75+
const processFile = (file) => {
76+
return (callback) => {
77+
if (!file.content) { // is directory
78+
pack.entry({name: file.path, type: 'directory'})
79+
callback()
80+
} else { // is file
81+
const fileContents = []
82+
file.content.on('data', (data) => {
83+
fileContents.push(data)
84+
})
85+
file.content.on('end', () => {
86+
pack.entry({name: file.path}, Buffer.concat(fileContents))
87+
callback()
88+
})
89+
}
90+
}
91+
}
92+
stream.on('end', () => {
93+
const callbacks = files.map(processFile)
94+
async.series(callbacks, () => {
95+
pack.finalize()
96+
reply(pack).header('X-Stream-Output', '1')
97+
})
4998
})
5099
})
51100
}
@@ -75,9 +124,10 @@ exports.add = {
75124
}
76125

77126
fileAdder.on('data', (file) => {
127+
const filePath = file.path ? file.path : file.hash
78128
serialize.write({
79-
Name: file.path,
80-
Hash: bs58.encode(file.node.multihash()).toString()
129+
Name: filePath,
130+
Hash: file.hash
81131
})
82132
filesAdded++
83133
})
@@ -104,6 +154,12 @@ exports.add = {
104154
filesParsed = true
105155
fileAdder.write(filePair)
106156
})
157+
parser.on('directory', (directory) => {
158+
fileAdder.write({
159+
path: directory,
160+
content: ''
161+
})
162+
})
107163

108164
parser.on('end', () => {
109165
if (!filesParsed) {

src/http-api/routes/files.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = (server) => {
66
const api = server.select('API')
77

88
api.route({
9+
// TODO fix method
910
method: '*',
1011
path: '/api/v0/cat',
1112
config: {
@@ -17,6 +18,19 @@ module.exports = (server) => {
1718
})
1819

1920
api.route({
21+
// TODO fix method
22+
method: '*',
23+
path: '/api/v0/get',
24+
config: {
25+
pre: [
26+
{ method: resources.files.get.parseArgs, assign: 'args' }
27+
],
28+
handler: resources.files.get.handler
29+
}
30+
})
31+
32+
api.route({
33+
// TODO fix method
2034
method: '*',
2135
path: '/api/v0/add',
2236
config: {

test/http-api/interface-ipfs-core-over-ipfs-api/test-files.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
'use strict'
44

5-
/*
65
const test = require('interface-ipfs-core')
76
const FactoryClient = require('./../../utils/factory-http')
87

@@ -17,8 +16,5 @@ const common = {
1716
fc.dismantle(callback)
1817
}
1918
}
20-
*/
2119

22-
// TODO
23-
// needs: https://github.com/ipfs/js-ipfs/pull/323
24-
// test.files(common)
20+
test.files(common)

0 commit comments

Comments
 (0)