Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jul 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 62 additions & 51 deletions app/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import gateways from './gateways.json'
import { trackEvent } from './stats'

import Settings from 'electron-settings'
import { reportAndReject } from './lib/report/util'

export const ERROR_IPFS_UNAVAILABLE = 'IPFS NOT AVAILABLE'
export const ERROR_IPFS_TIMEOUT = 'TIMEOUT'
Expand Down Expand Up @@ -82,6 +83,7 @@ export function wrapFiles (links) {

return wrapper
})
.catch(reportAndReject)
}

/**
Expand Down Expand Up @@ -156,6 +158,7 @@ export function addFilesFromFSPath (filePaths, _queryGateways = queryGateways) {
return Promise.resolve(wrapper)
})
})
.catch(reportAndReject)
}

/**
Expand All @@ -180,6 +183,7 @@ export function unpinObject (hash) {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)
const options = { recursive: true }
return IPFS_CLIENT.pin.rm(hash, options)
.catch(reportAndReject)
}

/**
Expand All @@ -190,6 +194,7 @@ export function pinObject (hash) {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)

return IPFS_CLIENT.pin.add(hash)
.catch(reportAndReject)
}

/**
Expand All @@ -205,6 +210,7 @@ export function getRepoInfo () {
stats.RepoSize = byteSize(stats.repoSize)
return Promise.resolve(stats)
})
.catch(reportAndReject)
}

/**
Expand All @@ -215,6 +221,7 @@ export function getPeersInfo () {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)

return IPFS_CLIENT.swarm.peers()
.catch(reportAndReject)
}

/**
Expand All @@ -224,6 +231,7 @@ export function getPeer () {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)

return IPFS_CLIENT.id()
.catch(reportAndReject)
}

/**
Expand All @@ -234,6 +242,7 @@ export function getObjectList () {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)

return IPFS_CLIENT.pin.ls()
.catch(reportAndReject)
}

/**
Expand All @@ -248,48 +257,45 @@ export function isObjectPinned (hash) {
// find returns the object, we need to cast it to boolean
return Promise.resolve(!!pins.find(pin => pin.hash === hash))
})
.catch(reportAndReject)
}

/**
* Provides using a Promise the stat of an IPFS object. Note: All the Size
* values are a byteSize object (ex: {value, unit}) to make it human readable
*/
export function getObjectStat (objectMultiHash) {
return new Promise((resolve, reject) => {
if (!IPFS_CLIENT) return reject(ERROR_IPFS_UNAVAILABLE)
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)

return IPFS_CLIENT.object.stat(objectMultiHash)
.then((stat) => {
stat.BlockSize = byteSize(stat.BlockSize)
stat.LinksSize = byteSize(stat.LinksSize)
stat.DataSize = byteSize(stat.DataSize)
stat.CumulativeSize = byteSize(stat.CumulativeSize)
return resolve(stat)
})
.catch(reject)
})
return IPFS_CLIENT.object.stat(objectMultiHash)
.then((stat) => {
stat.BlockSize = byteSize(stat.BlockSize)
stat.LinksSize = byteSize(stat.LinksSize)
stat.DataSize = byteSize(stat.DataSize)
stat.CumulativeSize = byteSize(stat.CumulativeSize)
return Promise.resolve(stat)
})
.catch(reportAndReject)
}

/**
* Provides using a Promise the serialized dag of an IPFS object.
*/
export function getObjectDag (objectMultiHash) {
return new Promise((resolve, reject) => {
if (!IPFS_CLIENT) return reject(ERROR_IPFS_UNAVAILABLE)

return IPFS_CLIENT.object.get(objectMultiHash)
.then((dag) => {
dag = dag.toJSON()
dag.size = byteSize(dag.size)
dag.links = dag.links.map(link => {
link.size = byteSize(link.size)
return link
})
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)

return resolve(dag)
return IPFS_CLIENT.object.get(objectMultiHash)
.then((dag) => {
dag = dag.toJSON()
dag.size = byteSize(dag.size)
dag.links = dag.links.map(link => {
link.size = byteSize(link.size)
return link
})
.catch(reject)
})

return Promise.resolve(dag)
})
.catch(reportAndReject)
}

/**
Expand All @@ -305,33 +311,32 @@ export function isDagDirectory (dag) {
* details, ex: Sizes, Links, Hash, Data. Used by the Interface to render the table
*/
export function getStorageList (pins) {
return new Promise((resolve, reject) => {
// Filter out the indirect objects. Required to reduce API Calls
pins = pins.filter(pin => pin.type !== 'indirect')

// Get a list of promises that will return the pin object with the
// stat and dag injected
const promises = pins.map(pin => {
// Use the promises to perform multiple injections, so always
// resolve with the pin object
return getObjectStat(pin.hash)
.then(stat => {
pin.stat = pin.stat || stat

return getObjectDag(pin.hash)
})
.then(dag => {
pin.dag = dag
pin.isDirectory = isDagDirectory(dag)

return Promise.resolve(pin)
})
})
// Filter out the indirect objects. Required to reduce API Calls
pins = pins.filter(pin => pin.type !== 'indirect')

// Get a list of promises that will return the pin object with the
// stat and dag injected
const promises = pins.map(pin => {
// Use the promises to perform multiple injections, so always
// resolve with the pin object
return getObjectStat(pin.hash)
.then(stat => {
pin.stat = pin.stat || stat

return getObjectDag(pin.hash)
})
.then(dag => {
pin.dag = dag
pin.isDirectory = isDagDirectory(dag)

// Return a promise that will complete when all the data will be
// available. When done, it will run the main promise success()
return Promise.all(promises).then(resolve, reject)
return Promise.resolve(pin)
})
})

// Return a promise that will complete when all the data will be
// available. When done, it will run the main promise success()
return Promise.all(promises)
.catch(reportAndReject)
}

/**
Expand All @@ -341,6 +346,7 @@ export function getStorageList (pins) {
export function getPeersWithObjectbyHash (hash) {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)
return IPFS_CLIENT.dht.findprovs(hash)
.catch(reportAndReject)
}

/**
Expand All @@ -351,6 +357,7 @@ export function importObjectByHash (hash) {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)
const options = { recursive: true }
return IPFS_CLIENT.pin.add(hash, options)
.catch(reportAndReject)
}

/**
Expand Down Expand Up @@ -399,6 +406,7 @@ export function saveFileToPath (hash, dest) {
export function runGarbageCollector () {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)
return IPFS_CLIENT.repo.gc()
.catch(reportAndReject)
}

/**
Expand All @@ -408,6 +416,7 @@ export function resolveName (name) {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)

return IPFS_CLIENT.name.resolve(name)
.catch(reportAndReject)
}

/**
Expand All @@ -423,6 +432,7 @@ export function publishToIPNS (hash) {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)

return IPFS_CLIENT.name.publish(hash)
.catch(reportAndReject)
}

/**
Expand All @@ -433,6 +443,7 @@ export function connectTo (strMultiddr) {
if (!IPFS_CLIENT) return Promise.reject(ERROR_IPFS_UNAVAILABLE)
const addr = multiaddr(strMultiddr)
return IPFS_CLIENT.swarm.connect(addr)
.catch(reportAndReject)
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Settings from 'electron-settings'
import './lib/report/node'
import rootDir from 'app-root-dir'
import setupTrayIcon from './setup-tray-icon'
import { report } from './lib/report/util'

import {
startIPFSDaemon,
Expand Down Expand Up @@ -274,6 +275,7 @@ function startOrion () {
} else {
message = JSON.stringify(err)
}
report(message)
dialog.showMessageBox({ type: 'warning', message })
app.quit()
})
Expand Down
14 changes: 14 additions & 0 deletions app/lib/report/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { captureMessage, captureException } from '@sentry/electron'

export function reportAndReject (err) {
report(err)
return Promise.reject(err)
}

export function report (err) {
if (typeof err === 'string') {
captureMessage(err)
} else {
captureException(err)
}
}
3 changes: 2 additions & 1 deletion app/windows/Storage/Components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ class Header extends React.Component {
this.setState({ isRemoving: true })
proptAndRemoveObjects(this.props.storageStore.selected)
.then(() => {
// we don't clear the items, only the selected ones
// we wait for the storagelist to update itself to avoid a short empty table
this.props.storageStore.selected.clear()
this.props.storageStore.elements.clear()
this.setState({ isRemoving: false })
})
.catch(err => {
Expand Down
3 changes: 3 additions & 0 deletions app/windows/Storage/Components/StorageElement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class StorageElement extends React.Component {
label: 'Remove',
click: (item) => {
proptAndRemoveObjects([this.props.element])
.catch(err => {
remote.dialog.showErrorBox('Removing the file(s) has failed', err.message)
})
}
},
{
Expand Down