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

Commit 49c7611

Browse files
Merge pull request #661 from ipfs/feat/object-template
Feat/object template
2 parents 1af30c1 + 1636c2d commit 49c7611

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"form-data": "^2.1.2",
6161
"fs-pull-blob-store": "^0.4.1",
6262
"gulp": "^3.9.1",
63-
"interface-ipfs-core": "^0.22.0",
63+
"interface-ipfs-core": "^0.22.1",
6464
"left-pad": "^1.1.3",
6565
"lodash": "^4.17.2",
6666
"ncp": "^2.0.0",
@@ -80,7 +80,7 @@
8080
"hapi": "^16.0.1",
8181
"hapi-set-header": "^1.0.2",
8282
"idb-pull-blob-store": "^0.5.1",
83-
"ipfs-api": "^12.0.3",
83+
"ipfs-api": "^12.1.0",
8484
"ipfs-bitswap": "^0.8.2",
8585
"ipfs-block": "^0.5.1",
8686
"ipfs-block-service": "^0.7.1",
@@ -150,4 +150,4 @@
150150
"nginnever <[email protected]>",
151151
"npmcdn-to-unpkg-bot <[email protected]>"
152152
]
153-
}
153+
}

src/cli/commands/object/new.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const log = debug('cli:object')
77
log.error = debug('cli:object:error')
88

99
module.exports = {
10-
command: 'new',
10+
command: 'new [<template>]',
1111

1212
describe: 'Create new ipfs objects',
1313

@@ -16,7 +16,7 @@ module.exports = {
1616
handler (argv) {
1717
waterfall([
1818
(cb) => utils.getIPFS(cb),
19-
(ipfs, cb) => ipfs.object.new(cb)
19+
(ipfs, cb) => ipfs.object.new(argv.template, cb)
2020
], (err, node) => {
2121
if (err) {
2222
throw err

src/core/components/init.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const peerId = require('peer-id')
44
const waterfall = require('async/waterfall')
5+
const parallel = require('async/parallel')
56

67
const addDefaultAssets = require('./init-assets')
78

@@ -51,11 +52,28 @@ module.exports = function init (self) {
5152
},
5253
(cb) => self._repo.config.set(config, cb),
5354
(cb) => {
54-
if (typeof addDefaultAssets === 'function' && !opts.emptyRepo) {
55-
return addDefaultAssets(self, opts.log, cb)
55+
if (opts.emptyRepo) {
56+
return cb(null, true)
5657
}
5758

58-
cb(null, true)
59+
const tasks = [
60+
// add empty unixfs dir object (go-ipfs assumes this exists)
61+
(cb) => self.object.new('unixfs-dir', cb)
62+
]
63+
64+
if (typeof addDefaultAssets === 'function') {
65+
tasks.push(
66+
(cb) => addDefaultAssets(self, opts.log, cb)
67+
)
68+
}
69+
70+
parallel(tasks, (err) => {
71+
if (err) {
72+
return cb(err)
73+
}
74+
75+
cb(null, true)
76+
})
5977
}
6078
], callback)
6179
}

src/core/components/object.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const DAGNode = dagPB.DAGNode
77
const DAGLink = dagPB.DAGLink
88
const CID = require('cids')
99
const mh = require('multihashes')
10+
const Unixfs = require('ipfs-unixfs')
11+
const assert = require('assert')
1012

1113
function normalizeMultihash (multihash, enc) {
1214
if (typeof multihash === 'string') {
@@ -91,8 +93,22 @@ module.exports = function object (self) {
9193
}
9294

9395
return {
94-
new: promisify((callback) => {
95-
DAGNode.create(new Buffer(0), (err, node) => {
96+
new: promisify((template, callback) => {
97+
if (typeof template === 'function') {
98+
callback = template
99+
template = undefined
100+
}
101+
102+
let data
103+
104+
if (template) {
105+
assert(template === 'unixfs-dir', 'unkown template')
106+
data = (new Unixfs('directory')).marshal()
107+
} else {
108+
data = new Buffer(0)
109+
}
110+
111+
DAGNode.create(data, (err, node) => {
96112
if (err) {
97113
return callback(err)
98114
}

src/http-api/resources/object.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ exports.parseKey = (request, reply) => {
3434

3535
exports.new = (request, reply) => {
3636
const ipfs = request.server.app.ipfs
37+
const template = request.query.arg
3738

38-
ipfs.object.new((err, node) => {
39+
ipfs.object.new(template, (err, node) => {
3940
if (err) {
4041
log.error(err)
4142
return reply({

test/cli/test-object.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ describe('object', () => {
1717
})
1818
})
1919

20+
it('new unixfs-dir', () => {
21+
return ipfs('object new unixfs-dir').then((out) => {
22+
expect(out).to.be.eql(
23+
'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
24+
)
25+
})
26+
})
27+
2028
it('get', () => {
2129
return ipfs('object get QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n').then((out) => {
2230
const result = JSON.parse(out)

0 commit comments

Comments
 (0)