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

Commit 9058118

Browse files
feat(object): add template option to object.new
1 parent 1af30c1 commit 9058118

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

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/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)