Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror-folder example now downloads the files correctly. Fixes #116 #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
55 changes: 38 additions & 17 deletions docs/cookbook/diy-dat.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,56 @@ To download the files to the file system, we are going to use [mirror-folder](ht

In practice, you should use [dat-storage](https://github.com/datproject/dat-storage) to do this as it'll be more efficient and keep the metadata on disk.

Setup will be the same as before (make sure you install `mirror-folder`). The first part of the module will look the same.
In addition to the modules you installed earlier, you will also need to install the following ones:

```bash
npm install --save mirror-folder mkdirp
```

Update your _index.js_ file to match the following:

```js
var path = require('path')
var ram = require('random-access-memory')
var hyperdrive = require('hyperdrive')
var discovery = require('hyperdiscovery')
var mirror = require('mirror-folder')
var mkdirp = require('mkdirp')
const path = require('path')
const ram = require('random-access-memory')
const hyperdrive = require('hyperdrive')
const discovery = require('hyperdiscovery')
const mirror = require('mirror-folder')
const mkdirp = require('mkdirp')

var link = process.argv[2] // user inputs the dat link
var key = link.replace('dat://', '') // extract the key
var dir = path.join(process.cwd(), 'dat-download') // make a download folder
const link = process.argv[2]

const key = link.replace('dat://', '')

const dir = path.join(process.cwd(), 'dat-download')
mkdirp.sync(dir)

var archive = hyperdrive(ram, key)
archive.ready(function () {
discovery(archive)
const archive = hyperdrive(ram, key)

var progress = mirror({name: '/', fs: archive}, dir, function (err) {
console.log('done downloading!')
archive.ready(function (err) {
if (err) throw err
const swarm = discovery(archive)
swarm.on('connection', function (peer, type) {
console.log(`Connected to peer (${type.type})`)
})
})

archive.on('content', function () {
console.log('Archive’s content is ready.')
console.log('Starting to mirror…')
const progress = mirror({name: '/', fs: archive}, dir, function (err) {
if (err) throw err
console.log('Done mirroring the archive’s content to disk.')
})
progress.on('put', function (src) {
console.log(src.name, 'downloaded')
console.log(` Mirrored: ${src.name}`)
})
progress.on('skip', function (src) {
console.log(` Skipped: ${src.name}`)
})
})
```

You should be able to run the module and see all our docs files in the `download` folder:
You should be able to run the module and see all our docs files in the `dat-download` folder:

```bash
node index.js dat://<link>
Expand Down