Skip to content

Commit 531f819

Browse files
committed
support ipfsPath function and set the .ipfs property on files.
1 parent 87b046c commit 531f819

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

README.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,41 @@ Both [ipfs](https://www.npmjs.com/package/ipfs) and [dropzone](https://www.npmjs
88
const IPFSDropzone = require('ipfs-dropzone')
99

1010
let dz = IPFSDropzone('#files', {
11+
/*
12+
the name of the repo which will store the IPFS blocks in the browser.
13+
this name will be used by IPFS to create the IndexedDB databases.
14+
15+
defaults to "ipfs-dropzone"
16+
*/
1117
ipfsRepoName: 'filemap.xyz',
12-
addRemoveLinks: true,
13-
accept: (file, done) => {
14-
if (file.size > 50000000) {
15-
done('Files must be smaller than 50MB.')
16-
} else {
17-
done()
18-
}
19-
}
18+
19+
/*
20+
ipfsPath is a function that takes the dropzone file object
21+
and returns a string that will be used as the path when calling
22+
https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#add
23+
24+
defaults to (file) => file.name
25+
*/
26+
ipfsPath: file => file.name
27+
28+
// all other options you'll normally pass to dropzone go here.
2029
})
2130

2231
dz.on('success', file => {
23-
console.log('file published to ipfs: ' + file.hash)
32+
/*
33+
everywhere the normal dropzone file object will now have a
34+
property called "ipfs" which contains the result to the call to
35+
https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#add
36+
*/
37+
38+
console.log('file published to ipfs: ' + file.ipfs[0].hash)
2439

40+
/*
41+
your dropzone object will also have the property "node", which
42+
resolves to the IPFS node the dropzone object is using.
43+
it is a Promise because the IPFS node is only initialized at the
44+
moment of the first file upload.
45+
*/
2546
dz.node.then(ipfs => {
2647
ipfs.files.cat(file.hash, (err, file) => {
2748
console.log(file.toString('utf-8'))
@@ -34,12 +55,12 @@ dz.on('success', file => {
3455

3556
* Most normal Dropzone features and events should work, but I'm not sure.
3657
* There's no way to cancel the process of adding a file.
37-
* A IPFS repo will automatically be created in the browser (in a IndexedDB) by js-ipfs, this repo will be named `"dz-ipfs"` if `ipfsRepoName` is not supplied.
3858
* I don't know how to publish this package in a way all JS transpilers and bundlers out there can understand. Please help me.
59+
* Please read the source if something is unclear. The source is really small.
3960

4061
## Who's using this?
4162

42-
https://filemap.xyz
63+
* https://filemap.xyz
4364

4465
## What is the LICENSE?
4566

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ class IPFSDropzone extends Dropzone {
88
super(el, options)
99

1010
this.node = null
11-
this.ipfsRepoName = options.ipfsRepoName || 'dz-ipfs'
11+
this.ipfsRepoName = options.ipfsRepoName || 'ipfs-dropzone'
12+
this.ipfsPath = typeof options.ipfsPath === 'function'
13+
? options.ipfsPath
14+
: file => file.name
1215
}
1316

1417
uploadFiles (files) {
@@ -44,17 +47,18 @@ class IPFSDropzone extends Dropzone {
4447
let buf = buffers[i]
4548
let file = files[i]
4649

47-
node.files.add(buf, {
50+
node.files.add({
51+
path: this.options.ipfsPath(file),
52+
content: buf
53+
}, {
4854
progress: loaded => {
4955
this._updateFilesUploadProgress([file], null, {
5056
loaded: loaded,
5157
total: file.size
5258
})
5359
}
5460
}, (err, res) => {
55-
let ipfsFile = res[0]
56-
file.hash = ipfsFile.hash
57-
file.path = ipfsFile.path
61+
file.ipfs = res
5862

5963
if (err) {
6064
this._errorProcessing([file], `ipfs add error: ${err}`)

0 commit comments

Comments
 (0)