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

Read file error on Windows #1000

Closed
EasonWang01 opened this issue Sep 6, 2017 · 9 comments
Closed

Read file error on Windows #1000

EasonWang01 opened this issue Sep 6, 2017 · 9 comments
Labels
kind/support A question or request for support

Comments

@EasonWang01
Copy link

EasonWang01 commented Sep 6, 2017

  • Version: 0.4.10
  • Platform: OSX
const IPFS = require('ipfs')
const stream = require('stream');
const path = require('path');

const node = new IPFS()
node.on('ready', (cb) => {
  node.files.add({
    path: 'hello.txt',
    content: require('fs').readFileSync(path.join(__dirname, './ipfs.js'))
  }, (err, result) => {
    if (err) { return cb(err) }

    console.log('\nAdded file:', result[0].path, result[0].hash);
  })
})

then https://gateway.ipfs.io/ipfs/....hash
the browser will keep loading and not showing the file.

2.Is there an example that using Readstream to add file to ipfs?

something like

const IPFS = require('ipfs')
const stream = require('stream');
const path = require('path');

const node = new IPFS()
node.on('ready', (cb) => {
  node.files.add({
    content: fs.createReadStream('sample.txt', { start: 90, end: 99 })
  }, (err, result) => {
    if (err) { return cb(err) }

    console.log('\nAdded file:', result[0].path, result[0].hash)
    // process.exit()
  })
})

Because in the test example https://github.com/ipfs/interface-ipfs-core/blob/master/src/files.js

ipfs.files.createAddStream....

then load file using 

loadFixture(__dirname, '../test/fixtures/test-folder/pp.txt', 'interface-ipfs-core'),

but the loadFixture function still using

require('fs').readFileSync(path.join(dirname, file))

https://github.com/ipfs/aegir/blob/master/src/fixtures.js

@daviddias
Copy link
Member

daviddias commented Sep 8, 2017

Hi @EasonWang01! I was unable to replicate the issue, could you try again?

As for the question on the Readable Stream example, you just need to pass a Readable Stream to content, see https://github.com/ipfs/interface-ipfs-core/tree/master/API/files#add.

Let me know if that works :)

@daviddias daviddias added the kind/support A question or request for support label Sep 8, 2017
@EasonWang01
Copy link
Author

Thanks for reply.
Sending a Read stream like this will get some error.

const IPFS = require('ipfs')
const stream = require('stream');
const path = require('path');

const node = new IPFS()
node.on('ready', (cb) => {
  node.files.add({
    content: fs.createReadStream('sample.txt', { start: 90, end: 99 })
  }, (err, result) => {
    if (err) { return cb(err) }

    console.log('\nAdded file:', result[0].path, result[0].hash)
    // process.exit()
  })
})

@daviddias
Copy link
Member

You need to add a path: ''

{
  path: ''
  content: fs.createReadStream('sample.txt', { start: 90, end: 99 })
}

Also, it seems you forgot to require fs and ready doesn't provide any callback. A correct way of doing that should be like:

const IPFS = require('ipfs')
const fs = require('fs');

const node = new IPFS()
node.on('ready', () => {
  node.files.add({
    path: ''
    content: fs.createReadStream('sample.txt', { start: 90, end: 99 })
  }, (err, result) => {
    if (err) { throw err }

    console.log('\nAdded file:', result[0].path, result[0].hash)
    // process.exit()
  })
})

@EasonWang01
Copy link
Author

Thanks,but it still got this error

      throw new Error(`Invalid key: ${this.toString()}`)
      ^

Error: Invalid key: /CIQPX75ZVS7OPRFWDXJVWID53Z3P6NP5N5MB6J44ACZEFEYSAGOJKVQ
    at new Key (C:\Users\yi.w\Desktop\node_modules\interface-datastore\src\key.js:45:13)
    at keyFromBuffer (C:\Users\yi.w\Desktop\node_modules\ipfs-repo\src\blockstore.js:20:10)
    at cidToDsKey (C:\Users\yi.w\Desktop\node_modules\ipfs-repo\src\blockstore.js:30:10)
    at Object.has (C:\Users\yi.w\Desktop\node_modules\ipfs-repo\src\blockstore.js:129:17)
    at waterfall (C:\Users\yi.w\Desktop\node_modules\ipfs-bitswap\src\index.js:286:31)
    at nextTask (C:\Users\yi.w\Desktop\node_modules\async\waterfall.js:16:14)
    at exports.default (C:\Users\yi.w\Desktop\node_modules\async\waterfall.js:26:5)
    at Bitswap.put (C:\Users\yi.w\Desktop\node_modules\ipfs-bitswap\src\index.js:285:5)
    at BlockService.put (C:\Users\yi.w\Desktop\node_modules\ipfs-block-service\src\index.js:61:28)
    at waterfall (C:\Users\yi.w\Desktop\node_modules\ipld-resolver\src\index.js:380:28)

@daviddias
Copy link
Member

daviddias commented Sep 8, 2017

Ah! You are using Windows. We don't fully support Windows yet, see #861 for the full thread.

Closing this one as a duplicate. Apologies for the hurdles, help us achieve Windows support!

@daviddias daviddias changed the title Read file error Read file error on Windows Sep 8, 2017
@EasonWang01
Copy link
Author

Thanks,will using OSX when come back home.

@EasonWang01
Copy link
Author

EasonWang01 commented Sep 12, 2017

Hi,it running without problem,but the link got no content.Thanks

Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmbPVEbSsLUhXRN9vJQh2a4PvMDMs


const IPFS = require('ipfs')
const fs = require('fs');

const node = new IPFS()
node.on('ready', () => {
  node.files.add({
    path: '',
    content: fs.createReadStream('./sample.txt', { start: 90, end: 99 })
  }, (err, result) => {
    if (err) { throw err }

    console.log('\nAdded file:', result[0].path, result[0].hash)
    // process.exit()
  })
})
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmbPVEbSsLUhXRN9vJQh2a4PvMDMsiFQvFF9kRi9V1rva7
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/QmbPVEbSsLUhXRN9vJQh2a4PvMDMsiFQvFF9kRi9V1rva7
Swarm listening on /ip4/192.168.1.6/tcp/4002/ipfs/QmbPVEbSsLUhXRN9vJQh2a4PvMDMsiFQvFF9kRi9V1rva7

Added file: QmfJMCvenrj4SKKRc48DYPxwVdS44qCUCqqtbqhJuSTWXP QmfJMCvenrj4SKKRc48DYPxwVdS44qCUCqqtbqhJuSTWXP

https://gateway.ipfs.io/ipfs/QmfJMCvenrj4SKKRc48DYPxwVdS44qCUCqqtbqhJuSTWXP
or
ipfs get QmfJMCvenrj4SKKRc48DYPxwVdS44qCUCqqtbqhJuSTWXP

the file content is empty

@daviddias
Copy link
Member

I believe that is because you are adding 9 bytes which look like not to exist in the original file:

image

@EasonWang01
Copy link
Author

thanks!

MicrowaveDev pushed a commit to galtproject/js-ipfs that referenced this issue May 22, 2020
Better error reporting by detecting `Content-Type` of the response and not attempting to parse JSON for non-`application/json` responses.

resolves ipfs#912
resolves ipfs#1000
closes ipfs#1001

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/support A question or request for support
Projects
None yet
Development

No branches or pull requests

2 participants