Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

ipfs add url wrap doesn't work #140

Closed
jbenet opened this issue Nov 23, 2015 · 8 comments
Closed

ipfs add url wrap doesn't work #140

jbenet opened this issue Nov 23, 2015 · 8 comments

Comments

@jbenet
Copy link
Contributor

jbenet commented Nov 23, 2015

Expected:

ipfs.add(url, {wrap: true}, cb)

would be equivalent to cli's ipfs add -w, preserving the last pathname.

@dignifiedquire
Copy link
Contributor

The main thing to do here is extracting the filename from a given url and passing it as a parameter.

@fazo96
Copy link

fazo96 commented Nov 25, 2015

@dignifiedquire something like this?

var s = url.split('#')[0].split('/')
filename = s[name.length - 1].trim()
if(filename.length === 0) filename = 'index.html'

@dignifiedquire
Copy link
Contributor

@fazo96 yes, and then pass it like this {content: res, path: filename} to the send method (also need to add a test to check that this actually works)

@dignifiedquire
Copy link
Contributor

Or you could look into using https://nodejs.org/docs/latest/api/url.html

@Mithgol
Copy link
Contributor

Mithgol commented Nov 29, 2015

yourFilename = require('path').basename(
   require('url').parse(yourURL).pathname
)

@daviddias
Copy link
Contributor

We have https://github.com/ipfs/js-ipfs-api#add-a-file-from-a-url-to-ipfs and tests https://github.com/ipfs/js-ipfs-api/blob/master/test/ipfs-api/util.spec.js#L73-L86, I'll call this done unless someone is still hitting a issue, let me know and we will reopen and check again. Bonus if you provide a test case :D

@Mithgol
Copy link
Contributor

Mithgol commented Dec 3, 2016

Wait, does the API has anything equivalent to ipfs add -w that actually wraps? (Except ipfs.util.addFromFs of the parent directory, I mean.)

@hacdias
Copy link
Contributor

hacdias commented Apr 25, 2018

@diasdavid is this issue still valid? As far as I know, the wrap option is called wrapWithDirectory and it actually works:

const ipfsAPI = require('.')
const ipfs = ipfsAPI('/ip4/127.0.0.1/tcp/5001')

ipfs.util.addFromURL('https://hacdias.com/downloads/aprenda_a_programar.pdf', {wrapWithDirectory: true}, (err, result) => {
  console.log(result)
})

Prints:

[ { path: 'QmWisvrzmUhFr8Ab8WFVq7iVwskqkmesjB42JBd1AA1ZBX',
    hash: 'QmWisvrzmUhFr8Ab8WFVq7iVwskqkmesjB42JBd1AA1ZBX',
    size: 943569 },
  { path: '',
    hash: 'QmQXoUuuPPnBaVuwEKf3jhZvfFVbWLwjeBSiip58oW9QsB',
    size: 943663 } ]

Edit: noticed that it lacks the filename on path field. Will fix this 😄

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants