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 a19a5ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"start": "run-script-os",
"start:win32": "@powershell -Command $env:REACT_APP_GIT_REV=(git rev-parse --short HEAD); react-scripts start",
"start:darwin:linux": "cross-env REACT_APP_GIT_REV=`git rev-parse --short HEAD` react-scripts start",
"lint": "eslint src/ test/",
"lint": "eslint src/ test/ && tsc --noEmit",
"prebuild": "lol public/locales > src/lib/languages.json",
"build": "run-script-os",
"build:win32": "@powershell -Command $env:REACT_APP_GIT_REV=(git rev-parse --short HEAD); react-scripts build",
Expand Down
28 changes: 18 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 {Promise<number>}
*/
const cumulativeSize = async (ipfs, cidOrPath) => {
const { cumulativeSize } = await stat(ipfs, cidOrPath)
return cumulativeSize || 0
}

/**
* @param {string} path
* @returns {string}
Expand All @@ -68,6 +78,7 @@ export const realMfsPath = (path) => {
* @property {string} path
* @property {'file'|'directory'|'unknown'} type
* @property {CID} cid
* @property {number} cumulativeSize
* @property {number} size
*
* @param {IPFSService} ipfs
Expand All @@ -81,6 +92,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 All @@ -93,6 +105,7 @@ const stat = async (ipfs, cidOrPath) => {
path: hashOrPath,
cid: new CID(cid),
type: 'unknown',
cumulativeSize: 0,
size: 0
}
}
Expand Down Expand Up @@ -512,13 +525,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 +540,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 +553,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 +623,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 a19a5ef

Please sign in to comment.