Skip to content

Commit

Permalink
refactor: simplify size calculations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lidel committed Mar 23, 2021
1 parent 1a38fd4 commit b0c18d7
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/bundles/files/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ const fileFromStats = ({ cumulativeSize, type, size, cid, name, path, pinned, is
isParent: isParent
})

/**
* @param {IPFSService} ipfs
* @param {string|CID} cidOrPath
* @returns {number}
*/
const cumulativeSize = async (ipfs, cidOrPath) => {
const { cumulativeSize } = await stat(ipfs, cidOrPath)
return cumulativeSize || 0
}

/**
* @param {string} path
* @returns {string}
Expand Down Expand Up @@ -81,6 +91,7 @@ const stat = async (ipfs, cidOrPath) => {
: `/ipfs/${hashOrPath}`

try {
// TODO: memoize/cache result per CID
const stats = await ipfs.files.stat(path)
return { path, ...stats }
} catch (e) {
Expand Down Expand Up @@ -512,13 +523,10 @@ const actions = () => ({
* updated.
*/
doPinsSizeGet: () => perform(ACTIONS.PINS_SIZE_GET, async (ipfs) => {
const allPinsCids = await ipfs.pin.ls({ type: 'recursive' })

let pinsSize = 0
const pinsSize = -1 // TODO: right now calculating size of all pins is too expensive (requires ipfs.files.stat per CID)
let numberOfPins = 0

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

Expand All @@ -530,8 +538,7 @@ const actions = () => ({
* updated.
*/
doFilesSizeGet: () => perform(ACTIONS.SIZE_GET, async (ipfs) => {
const stat = await ipfs.files.stat('/')
return { size: stat.cumulativeSize }
return cumulativeSize(ipfs, '/')
}),

/**
Expand All @@ -544,8 +551,7 @@ const actions = () => ({
*/
async (store) => {
const ipfs = store.getIpfs()
const stat = await ipfs.files.stat(`/ipfs/${cid}`)
return stat.cumulativeSize
return cumulativeSize(ipfs, cid)
}
})

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

parent = fileFromStats({
...await ipfs.files.stat(parentInfo.realPath),
...await stat(ipfs, parentInfo.realPath),
path: parentInfo.path,
name: '..',
isParent: true
Expand Down

0 comments on commit b0c18d7

Please sign in to comment.