Skip to content

Commit b0c18d7

Browse files
committed
refactor: simplify size calculations
Using pre-existing stat helper makes webui immune to unexpected exceptions. Pin size calculation was removed, because it was already hidden in GUI of Settings screen.
1 parent 1a38fd4 commit b0c18d7

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/bundles/files/actions.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ const fileFromStats = ({ cumulativeSize, type, size, cid, name, path, pinned, is
5050
isParent: isParent
5151
})
5252

53+
/**
54+
* @param {IPFSService} ipfs
55+
* @param {string|CID} cidOrPath
56+
* @returns {number}
57+
*/
58+
const cumulativeSize = async (ipfs, cidOrPath) => {
59+
const { cumulativeSize } = await stat(ipfs, cidOrPath)
60+
return cumulativeSize || 0
61+
}
62+
5363
/**
5464
* @param {string} path
5565
* @returns {string}
@@ -81,6 +91,7 @@ const stat = async (ipfs, cidOrPath) => {
8191
: `/ipfs/${hashOrPath}`
8292

8393
try {
94+
// TODO: memoize/cache result per CID
8495
const stats = await ipfs.files.stat(path)
8596
return { path, ...stats }
8697
} catch (e) {
@@ -512,13 +523,10 @@ const actions = () => ({
512523
* updated.
513524
*/
514525
doPinsSizeGet: () => perform(ACTIONS.PINS_SIZE_GET, async (ipfs) => {
515-
const allPinsCids = await ipfs.pin.ls({ type: 'recursive' })
516-
517-
let pinsSize = 0
526+
const pinsSize = -1 // TODO: right now calculating size of all pins is too expensive (requires ipfs.files.stat per CID)
518527
let numberOfPins = 0
519528

520-
for await (const { cid } of allPinsCids) {
521-
pinsSize += (await ipfs.files.stat(`/ipfs/${cid.toString()}`)).cumulativeSize
529+
for await (const _ of ipfs.pin.ls({ type: 'recursive' })) { // eslint-disable-line no-unused-vars
522530
numberOfPins++
523531
}
524532

@@ -530,8 +538,7 @@ const actions = () => ({
530538
* updated.
531539
*/
532540
doFilesSizeGet: () => perform(ACTIONS.SIZE_GET, async (ipfs) => {
533-
const stat = await ipfs.files.stat('/')
534-
return { size: stat.cumulativeSize }
541+
return cumulativeSize(ipfs, '/')
535542
}),
536543

537544
/**
@@ -544,8 +551,7 @@ const actions = () => ({
544551
*/
545552
async (store) => {
546553
const ipfs = store.getIpfs()
547-
const stat = await ipfs.files.stat(`/ipfs/${cid}`)
548-
return stat.cumulativeSize
554+
return cumulativeSize(ipfs, cid)
549555
}
550556
})
551557

@@ -615,7 +621,7 @@ const dirStats = async (ipfs, cid, { path, isRoot, sorting }) => {
615621
}
616622

617623
parent = fileFromStats({
618-
...await ipfs.files.stat(parentInfo.realPath),
624+
...await stat(ipfs, parentInfo.realPath),
619625
path: parentInfo.path,
620626
name: '..',
621627
isParent: true

0 commit comments

Comments
 (0)