diff --git a/src/bundles/files/actions.js b/src/bundles/files/actions.js index 1c432c344..2a13857b9 100644 --- a/src/bundles/files/actions.js +++ b/src/bundles/files/actions.js @@ -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} @@ -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) { @@ -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++ } @@ -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, '/') }), /** @@ -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) } }) @@ -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