-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathactions.js
568 lines (494 loc) · 15 KB
/
actions.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/* eslint-disable require-yield */
import { join, dirname, basename } from 'path'
import { getDownloadLink, getShareableLink } from '../../lib/files'
import countDirs from '../../lib/count-dirs'
import all from 'it-all'
import map from 'it-map'
import last from 'it-last'
import CID from 'cids'
import { spawn, send, ensureMFS, Channel, sortFiles, infoFromPath } from './utils'
import { IGNORED_FILES, ACTIONS } from './consts'
/**
* @typedef {import('ipfs').IPFSService} IPFSService
* @typedef {import('../../lib/files').FileStream} FileStream
* @typedef {import('./utils').Info} Info
* @typedef {import('ipfs').Pin} Pin
*/
/**
* @typedef {Object} FileStat
* @property {number} size
* @property {'directory'|'file'|'unknown'} type
* @property {CID} cid
* @property {string} name
* @property {string} path
* @property {boolean} pinned
* @property {boolean|void} isParent
*
* @param {Object} stat
* @param {'dir'|'directory'|'file'|'unknown'} stat.type
* @param {CID} stat.cid
* @param {string} stat.path
* @param {number} [stat.cumulativeSize]
* @param {number} stat.size
* @param {string|void} [stat.name]
* @param {boolean|void} [stat.pinned]
* @param {boolean|void} [stat.isParent]
* @param {string} [prefix]
* @returns {FileStat}
*/
const fileFromStats = ({ cumulativeSize, type, size, cid, name, path, pinned, isParent }, prefix = '/ipfs') => ({
size: cumulativeSize || size || 0,
type: type === 'dir' ? 'directory' : type,
cid,
name: name || path.split('/').pop() || cid.toString(),
path: path || `${prefix}/${cid.toString()}`,
pinned: Boolean(pinned),
isParent: isParent
})
/**
* @param {string} path
* @returns {string}
*/
// TODO: use sth else
const realMfsPath = (path) => {
if (path.startsWith('/files')) {
return path.substr('/files'.length) || '/'
}
return path
}
/**
* @typedef {Object} Stat
* @property {string} path
* @property {'file'|'directory'|'unknown'} type
* @property {CID} cid
* @property {number} size
*
* @param {IPFSService} ipfs
* @param {string|CID} cidOrPath
* @returns {Promise<Stat>}
*/
const stat = async (ipfs, cidOrPath) => {
const hashOrPath = cidOrPath.toString()
const path = hashOrPath.startsWith('/')
? hashOrPath
: `/ipfs/${hashOrPath}`
try {
const stats = await ipfs.files.stat(path)
return { path, ...stats }
} catch (e) {
// Discard error and mark DAG as 'unknown' to unblock listing other pins.
// Clicking on 'unknown' entry will open it in Inspector.
// No information is lost: if there is an error related
// to specified hashOrPath user will read it in Inspector.
const [, , cid] = path.split('/')
return {
path: hashOrPath,
cid: new CID(cid),
type: 'unknown',
size: 0
}
}
}
/**
*
* @param {IPFSService} ipfs
* @returns {AsyncIterable<Pin>}
*/
const getRawPins = async function * (ipfs) {
yield * ipfs.pin.ls({ type: 'recursive' })
yield * ipfs.pin.ls({ type: 'direct' })
}
/**
* @param {IPFSService} ipfs
* @returns {AsyncIterable<CID>}
*/
const getPinCIDs = (ipfs) => map(getRawPins(ipfs), (pin) => pin.cid)
/**
* @param {IPFSService} ipfs
* @returns {AsyncIterable<FileStat>}
*/
const getPins = async function * (ipfs) {
for await (const cid of getPinCIDs(ipfs)) {
const info = await stat(ipfs, cid)
yield fileFromStats({ ...info, pinned: true }, '/pins')
}
}
/**
* @typedef {import('./protocol').Message} Message
* @typedef {import('./protocol').Model} Model
* @typedef {import('./protocol').PageContent} PageContent
* @typedef {import('./utils').Context} Context
*
* @typedef {import('redux-bundler').Actions<ReturnType<typeof actions>>} Actions
*
*/
const actions = () => ({
/**
* Fetches list of pins and updates `state.pins` on succeful completion.
*/
doPinsFetch: () => spawn(ACTIONS.PIN_LIST, async function * (ipfs) {
const cids = await all(getPinCIDs(ipfs))
return { pins: cids }
}),
/**
* Syncs currently selected path file list.
* @returns {function(Context):Promise<void>}
*/
doFilesFetch: () => async ({ store }) => {
const isReady = store.selectIpfsReady()
const isConnected = store.selectIpfsConnected()
const isFetching = store.selectFilesIsFetching()
const info = store.selectFilesPathInfo()
if (isReady && isConnected && !isFetching && info) {
await store.doFetch(info)
}
},
/**
* Fetches conten for the currently selected path. And updates
* `state.pageContent` on succesful completion.
* @param {Info} info
*/
doFetch: ({ path, realPath, isMfs, isPins, isRoot }) => spawn(ACTIONS.FETCH, async function * (ipfs, { store }) {
if (isRoot && !isMfs && !isPins) {
throw new Error('not supposed to be here')
}
if (isRoot && isPins) {
const pins = await all(getPins(ipfs)) // FIX: pins path
return {
path: '/pins',
fetched: Date.now(),
type: 'directory',
content: pins
}
}
const resolvedPath = realPath.startsWith('/ipns')
? await last(ipfs.name.resolve(realPath))
: realPath
const stats = await stat(ipfs, resolvedPath)
const time = Date.now()
switch (stats.type) {
case 'unknown': {
return {
fetched: time,
...stats
}
}
case 'file': {
return {
...fileFromStats({ ...stats, path }),
fetched: time,
type: 'file',
read: () => ipfs.cat(stats.cid),
name: path.split('/').pop(),
size: stats.size,
cid: stats.cid
}
}
case 'directory': {
return await dirStats(ipfs, stats.cid, {
path,
isRoot,
sorting: store.selectFilesSorting()
})
}
default: {
throw Error(`Unsupported file type "${stats.type}"`)
}
}
}),
/**
* Imports given `source` files to the provided `root` path. On completion
* (success or fail) will trigger `doFilesFetch` to update the state.
* @param {FileStream[]} source
* @param {string} root
*/
doFilesWrite: (source, root) => spawn(ACTIONS.WRITE, async function * (ipfs, { store }) {
const files = source
// Skip ignored files
.filter($ => !IGNORED_FILES.includes(basename($.path)))
// Dropped files come as absolute, those added by the file input come
// as relative paths, so normalise all to be relative.
.map($ => $.path[0] === '/' ? { ...$, path: $.path.slice(1) } : $)
const uploadSize = files.reduce((prev, { size }) => prev + size, 0)
// Just estimate download size to be around 10% of upload size.
const downloadSize = uploadSize * 10 / 100
const totalSize = uploadSize + downloadSize
let loaded = 0
const entries = files.map(({ path, size }) => ({ path, size }))
yield { entries, progress: 0 }
const { result, progress } = importFiles(ipfs, files)
for await (const update of progress) {
loaded += update.loaded
yield { entries, progress: loaded / totalSize * 100 }
}
try {
const added = await result
const numberOfFiles = files.length
const numberOfDirs = countDirs(files)
const expectedResponseCount = numberOfFiles + numberOfDirs
if (added.length !== expectedResponseCount) {
// See https://github.com/ipfs/js-ipfs-api/issues/797
throw Object.assign(new Error('API returned a partial response.'), {
code: 'ERR_API_RESPONSE'
})
}
for (const { path, cid } of added) {
// Only go for direct children
if (path.indexOf('/') === -1 && path !== '') {
const src = `/ipfs/${cid}`
const dst = join(realMfsPath(root || '/files'), path)
try {
await ipfs.files.cp(src, dst)
} catch (err) {
throw Object.assign(new Error('Folder already exists.'), {
code: 'ERR_FOLDER_EXISTS'
})
}
}
}
yield { entries, progress: 100 }
} finally {
await store.doFilesFetch()
}
}),
/**
* Deletes `files` with provided paths. On completion (success sor fail) will
* trigger `doFilesFetch` to update the state.
* @param {string[]} files
*/
doFilesDelete: (files) => spawn(ACTIONS.DELETE, async function * (ipfs, { store }) {
ensureMFS(store)
if (files.length > 0) {
const promises = files
.map(file => ipfs.files.rm(realMfsPath(file), {
recursive: true
}))
try {
await Promise.all(promises)
const src = files[0]
const path = src.slice(0, src.lastIndexOf('/'))
await store.doUpdateHash(path)
return undefined
} finally {
await store.doFilesFetch()
}
}
return undefined
}),
/**
* Adds file under the `src` path to the given `root` path. On completion will
* trigger `doFilesFetch` to update the state.
* @param {string} root
* @param {string} src
*/
doFilesAddPath: (root, src) => spawn(ACTIONS.ADD_BY_PATH, async function * (ipfs, { store }) {
ensureMFS(store)
const path = realMfsPath(src)
/** @type {string} */
const name = (path.split('/').pop())
const dst = realMfsPath(join(root, name))
const srcPath = src.startsWith('/') ? src : `/ipfs/${name}`
try {
return ipfs.files.cp(srcPath, dst)
} finally {
await store.doFilesFetch()
}
}),
/**
* Creates a download link for the provided files.
* @param {FileStat[]} files
*/
doFilesDownloadLink: (files) => spawn(ACTIONS.DOWNLOAD_LINK, async function * (ipfs, { store }) {
ensureMFS(store)
const apiUrl = store.selectApiUrl()
const gatewayUrl = store.selectGatewayUrl()
return await getDownloadLink(files, gatewayUrl, apiUrl, ipfs)
}),
/**
* Generates sharable link for the provided files.
* @param {FileStat[]} files
*/
doFilesShareLink: (files) => spawn(ACTIONS.SHARE_LINK, async function * (ipfs, { store }) {
ensureMFS(store)
return await getShareableLink(files, ipfs)
}),
/**
* Moves file from `src` MFS path to a `dst` MFS path. On completion (success
* or fail) triggers `doFilesFetch` to update the state.
* @param {string} src
* @param {string} dst
*/
doFilesMove: (src, dst) => spawn(ACTIONS.MOVE, async function * (ipfs, { store }) {
ensureMFS(store)
try {
await ipfs.files.mv(realMfsPath(src), realMfsPath(dst))
const page = store.selectFiles()
const pagePath = page && page.path
if (src === pagePath) {
await store.doUpdateHash(dst)
}
} finally {
await store.doFilesFetch()
}
}),
/**
* Copies file from `src` MFS path to a `dst` MFS path. On completion (success
* or fail) triggers `doFilesFetch` to update the state.
* @param {string} src
* @param {string} dst
*/
doFilesCopy: (src, dst) => spawn(ACTIONS.COPY, async function * (ipfs, { store }) {
ensureMFS(store)
try {
await ipfs.files.cp(realMfsPath(src), realMfsPath(dst))
} finally {
await store.doFilesFetch()
}
}),
/**
* Creates a directory at given MFS `path`. On completion (success or fail)
* triggers `doFilesFetch` to update the state.
* @param {string} path
*/
doFilesMakeDir: (path) => spawn(ACTIONS.MAKE_DIR, async function * (ipfs, { store }) {
ensureMFS(store)
try {
await ipfs.files.mkdir(realMfsPath(path), {
parents: true
})
} finally {
await store.doFilesFetch()
}
}),
/**
* Pins given `cid`. On completion (success or fail) triggers `doPinsFetch` to
* update the state.
* @param {CID} cid
*/
doFilesPin: (cid) => spawn(ACTIONS.PIN_ADD, async function * (ipfs, { store }) {
try {
return await ipfs.pin.add(cid)
} finally {
await store.doPinsFetch()
}
}),
/**
* Unpins given `cid`. On completion (success or fail) triggers `doPinsFetch`
* to update the state.
* @param {CID} cid
*/
doFilesUnpin: (cid) => spawn(ACTIONS.PIN_REMOVE, async function * (ipfs, { store }) {
try {
return await ipfs.pin.rm(cid)
} finally {
await store.doPinsFetch()
}
}),
/**
* Clears all failed tasks.
*/
doFilesDismissErrors: () => send({ type: ACTIONS.DISMISS_ERRORS }),
/**
* @param {string} path
*/
doFilesNavigateTo: (path) =>
/**
* @param {Context} context
*/
async ({ store }) => {
const link = path.split('/').map(p => encodeURIComponent(p)).join('/')
const files = store.selectFiles()
const url = store.selectFilesPathInfo()
if (files && files.path === link && url) {
await store.doFilesFetch()
} else {
await store.doUpdateHash(link)
}
},
/**
* @param {import('./consts').SORTING} by
* @param {boolean} asc
*/
doFilesUpdateSorting: (by, asc) => send({
type: ACTIONS.UPDATE_SORT,
payload: { by, asc }
}),
/**
* Clears all the tasks (pending, complete and failed).
*/
doFilesClear: () => send({ type: ACTIONS.CLEAR_ALL }),
/**
* Gets size of the MFS. On succesful completion `state.mfsSize` will get
* updated.
*/
doFilesSizeGet: () => spawn(ACTIONS.SIZE_GET, async function * (ipfs) {
const stat = await ipfs.files.stat('/')
return { size: stat.cumulativeSize }
})
})
export default actions
/**
*
* @param {IPFSService} ipfs
* @param {FileStream[]} files
*/
const importFiles = (ipfs, files) => {
/** @type {Channel<{ total:number, loaded: number}>} */
const channel = new Channel()
const result = all(ipfs.addAll(files, {
pin: false,
wrapWithDirectory: false,
onUploadProgress: (event) => channel.send(event),
onDownloadProgress: (event) => channel.send(event)
}))
result.then(() => channel.close(), error => channel.close(error))
return { result, progress: channel }
}
/**
* @param {IPFSService} ipfs
* @param {CID} cid
* @param {Object} options
* @param {string} options.path
* @param {boolean} [options.isRoot]
* @param {import('./utils').Sorting} options.sorting
*/
const dirStats = async (ipfs, cid, { path, isRoot, sorting }) => {
const res = await all(ipfs.ls(cid)) || []
const files = []
const showStats = res.length < 100
for (const f of res) {
const absPath = join(path, f.name)
let file = null
if (showStats && (f.type === 'directory' || f.type === 'dir')) {
file = fileFromStats({ ...await stat(ipfs, f.cid), path: absPath })
} else {
file = fileFromStats({ ...f, path: absPath })
}
files.push(file)
}
let parent = null
if (!isRoot) {
const parentPath = dirname(path)
const parentInfo = infoFromPath(parentPath, false)
if (parentInfo && (parentInfo.isMfs || !parentInfo.isRoot)) {
const realPath = parentInfo.realPath
if (realPath && realPath.startsWith('/ipns')) {
parentInfo.realPath = await last(ipfs.name.resolve(parentInfo.realPath))
}
parent = fileFromStats({
...await ipfs.files.stat(parentInfo.realPath),
path: parentInfo.path,
name: '..',
isParent: true
})
}
}
return {
path: path,
fetched: Date.now(),
type: 'directory',
cid,
upper: parent,
content: sortFiles(files, sorting)
}
}