forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (150 loc) · 3.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict'
/* eslint-env browser */
const IPFS = require('ipfs')
const ipfs = new IPFS({
repo: `ipfs-${Math.random()}`
})
const {
dragDrop,
log,
bufferToArrayBuffer
} = require('./utils')
const {
updateTree
} = require('./filetree')
const {
mvForm,
mkdirForm,
rmForm,
cpForm,
statForm,
readForm,
hideForms
} = require('./forms')
const mime = require('mime-sniffer')
hideForms()
log('IPFS: Initialising')
ipfs.on('ready', () => {
// Allow adding files to IPFS via drag and drop
dragDrop(async (files) => {
/* eslint-disable-next-line no-alert */
const destinationDirectory = prompt(`Dropped ${files.length} file${files.length > 1 ? 's' : ''}, please enter a directory to store them in`, '/')
if (!destinationDirectory || !`${destinationDirectory}`.trim()) {
return
}
await Promise.all(
files.map(file => {
const path = `${destinationDirectory}/${file.name}`.replace(/\/\/+/g, '/')
log(`ipfs.files.write('${path}', <File>, { create: true, parents: true })`)
return ipfs.files.write(path, file, {
create: true,
parents: true
})
.catch(error => log(error))
})
)
updateTree(ipfs)
})
mkdirForm(async (path, parents, format, hashAlg, flush) => {
log(`ipfs.files.mkdir('${path}', ${JSON.stringify({
parents,
format,
hashAlg,
flush
}, null, 2)})`)
await ipfs.files.mkdir(path, {
parents,
format,
hashAlg,
flush
})
.catch(error => log(error))
updateTree(ipfs)
})
mvForm(async (paths, destination, parents, format, hashAlg, flush) => {
log(`ipfs.files.mv(${paths.map(path => `'${path}'`).join(', ')}, ${JSON.stringify({
parents,
format,
hashAlg,
flush
}, null, 2)})`)
await ipfs.files.mv.apply(null, paths.concat(destination, {
parents,
format,
hashAlg,
flush
}))
.catch(error => log(error))
updateTree(ipfs)
})
rmForm(async (paths, recursive) => {
log(`ipfs.files.rm(${paths.map(path => `'${path}'`).join(', ')}, ${JSON.stringify({
recursive
}, null, 2)})`)
await ipfs.files.rm.apply(null, paths.concat({
recursive
}))
.catch(error => log(error))
updateTree(ipfs)
})
cpForm(async (paths, destination, parents, format, hashAlg, flush) => {
log(`ipfs.files.cp(${paths.map(path => `'${path}'`).join(', ')}, '${destination}', ${JSON.stringify({
parents,
format,
hashAlg,
flush
}, null, 2)})`)
await ipfs.files.cp.apply(null, paths.concat(destination, {
parents,
format,
hashAlg,
flush
}))
.catch(error => log(error))
updateTree(ipfs)
})
statForm(async (path, hash, size, withLocal) => {
log(`ipfs.files.stat('${path}', ${JSON.stringify({
hash,
size,
withLocal
}, null, 2)})`)
await ipfs.files.stat(path, {
hash,
size,
withLocal
})
.then((stats) => log(stats))
.catch(error => log(error))
})
readForm(async (path, offset, length) => {
log(`ipfs.files.read('${path}', ${JSON.stringify({
offset,
length
}, null, 2)})`)
await ipfs.files.read(path, {
offset,
length
})
.then((buffer) => {
mime.lookup(buffer, (error, result) => {
// will cause file to be downloaded if we don't know what it is
let mimeType = 'application/octet-stream'
if (!error) {
mimeType = result.mime
}
const data = bufferToArrayBuffer(buffer)
const file = new Blob([data], {
type: mimeType
})
const fileURL = URL.createObjectURL(file)
window.open(fileURL)
})
})
.catch(error => log(error))
})
log('IPFS: Ready')
log('IPFS: Drop some files into this window to get started')
log('')
updateTree(ipfs)
})