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

Feature: Wrap with directory #1329

Merged
merged 3 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const utils = require('../../utils')
const print = require('../../utils').print
const createProgressBar = require('../../utils').createProgressBar

const WRAPPER = 'wrapper/'

function checkPath (inPath, recursive) {
// This function is to check for the following possible inputs
// 1) "." add the cwd but throw error for no recursion flag
Expand Down Expand Up @@ -59,7 +57,6 @@ function getTotalBytes (path, recursive, cb) {

function addPipeline (index, addStream, list, argv) {
const {
wrapWithDirectory,
quiet,
quieter,
silent
Expand All @@ -79,17 +76,9 @@ function addPipeline (index, addStream, list, argv) {
pull.filter((file) => !file.isDirectory),
pull.map((file) => ({
path: file.path.substring(index, file.path.length),
originalPath: file.path
})),
pull.map((file) => ({
path: wrapWithDirectory ? WRAPPER + file.path : file.path,
content: fs.createReadStream(file.originalPath)
content: fs.createReadStream(file.path)
})),
addStream,
pull.map((file) => ({
hash: file.hash,
path: wrapWithDirectory ? file.path.substring(WRAPPER.length) : file.path
})),
pull.collect((err, added) => {
if (err) {
throw err
Expand Down Expand Up @@ -198,7 +187,8 @@ module.exports = {
cidVersion: argv.cidVersion,
rawLeaves: argv.rawLeaves,
onlyHash: argv.onlyHash,
hashAlg: argv.hash
hashAlg: argv.hash,
wrapWithDirectory: argv.wrapWithDirectory
}

// Temporary restriction on raw-leaves:
Expand Down
16 changes: 13 additions & 3 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const OtherBuffer = require('buffer').Buffer
const CID = require('cids')
const toB58String = require('multihashes').toB58String

const WRAPPER = 'wrapper/'

function noop () {}

function prepareFile (self, opts, file, callback) {
Expand All @@ -34,15 +36,15 @@ function prepareFile (self, opts, file, callback) {
const b58Hash = cid.toBaseEncodedString()

cb(null, {
path: file.path || b58Hash,
path: opts.wrapWithDirectory ? file.path.substring(WRAPPER.length) : (file.path || b58Hash),
hash: b58Hash,
size: node.size
})
}
], callback)
}

function normalizeContent (content) {
function normalizeContent (opts, content) {
if (!Array.isArray(content)) {
content = [content]
}
Expand All @@ -68,6 +70,14 @@ function normalizeContent (content) {
}
}

if (opts.wrapWithDirectory && !data.path) {
throw new Error('Must provide a path when wrapping with a directory')
}

if (opts.wrapWithDirectory) {
data.path = WRAPPER + data.path
}

return data
})
}
Expand Down Expand Up @@ -123,7 +133,7 @@ module.exports = function files (self) {

opts.progress = progress
return pull(
pull.map(normalizeContent),
pull.map(normalizeContent.bind(null, opts)),
pull.flatten(),
importer(self._ipld, opts),
pull.asyncMap(prepareFile.bind(null, self, opts))
Expand Down
8 changes: 5 additions & 3 deletions src/http/api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ exports.add = {
then: Joi.boolean().valid(false).required(),
otherwise: Joi.boolean().valid(false)
}),
'only-hash': Joi.boolean()
'only-hash': Joi.boolean(),
'wrap-with-directory': Joi.boolean()
})
// TODO: Necessary until validate "recursive", "stream-channels" etc.
.options({ allowUnknown: true })
Expand Down Expand Up @@ -208,7 +209,8 @@ exports.add = {
rawLeaves: request.query['raw-leaves'],
progress: request.query.progress ? progressHandler : null,
onlyHash: request.query['only-hash'],
hashAlg: request.query['hash']
hashAlg: request.query['hash'],
wrapWithDirectory: request.query['wrap-with-directory']
}

const aborter = abortable()
Expand Down Expand Up @@ -246,7 +248,7 @@ exports.add = {
ipfs.files.addPullStream(options),
pull.map((file) => {
return {
Name: file.path ? file.path : file.hash,
Name: file.path, // addPullStream already turned this into a hash if it wanted to
Hash: file.hash,
Size: file.size
}
Expand Down