Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
handleAddClick: function () {
// First check in db to see if name and hash are unique
var file = {};
file.name = this.state.name;
file.description = this.state.description;
file._id = this.state.hash;
file.type = FILE;
file.category = this.state.category;
console.log(file);
if (!file.name || !file.description) {
this.setState({
message: 'Files must have a name and a description'
});
return;
}
if (!multihash(file._id)) {
this.setState({
message: 'Not a valid multihash'
});
return;
}
searchNameOrHash(file.name, file._id)
.then(result => {
console.log('search result', result.length > 0);
if (result.length === 0) {
this.props.postFile(file, this.props.router);
} else {
this.setState({
message: 'A file with this name or hash already exists on the server'
});
}
});
async function handleSharePush(dispatch, getState, payload) {
const { from, hash } = payload
const state: Store = getState()
const contactList: ContactList = state.contactList
const contact = contactList.findContactInDirectory(from)
if(!contact) {
console.log('Got a share notification from unknow contact ' + from)
return
}
if(!isIpfs.multihash(hash)) {
throw 'invalid hash'
}
// Send ACK
const profile = getState().profile
const data = protocol.shareAck(profile, hash)
dispatch(pubsub.send(contact.sharesPubsubTopic, data))
const shareList: ShareList = state.shareList
const storedShare: ?Share = shareList.findByHash(hash)
if(storedShare) {
console.log('Got a share notification that we already knew: ' + storedShare.title)
return
}
function access (parts, obj, cb) {
const isRoot = obj === null && (isIPFS.multihash(parts[0]) || isIPFS.ipfsPath('/' + parts.join('/')))
const next = parts.shift()
const isLink = obj && Object.keys(obj).length === 1 && obj[LINK_SYMBOL]
const fetchLink = obj && (next ? !includes(Object.keys(obj), next) : true)
if (!obj && !isRoot) {
cb(new Error('No root object provided'))
} else if (isLink && fetchLink) {
// resolve links in objects with an / property
const link = obj[LINK_SYMBOL]
const linkParts = splitLink(link)
let blockLink = ''
if (linkParts[0] === 'ipfs') {
// /ipfs/
blockLink = linkParts[1]
parts = linkParts.slice(2).concat(parts)
const isLink = obj && Object.keys(obj).length === 1 && obj[LINK_SYMBOL]
const fetchLink = obj && (next ? !includes(Object.keys(obj), next) : true)
if (!obj && !isRoot) {
cb(new Error('No root object provided'))
} else if (isLink && fetchLink) {
// resolve links in objects with an / property
const link = obj[LINK_SYMBOL]
const linkParts = splitLink(link)
let blockLink = ''
if (linkParts[0] === 'ipfs') {
// /ipfs/
blockLink = linkParts[1]
parts = linkParts.slice(2).concat(parts)
} else if (isIPFS.multihash(linkParts[0])) {
// /
blockLink = linkParts[0]
parts = linkParts.slice(1).concat(parts)
} else {
return cb(new Error(`Invalid link: "${link}"`))
}
service.get(blockLink, (err, block) => {
if (err) {
return cb(err)
}
if (next) {
// Put back so it's resolved in the next node
parts.unshift(next)
}
async function handleLookup(dispatch, getState, payload) {
const { from, pubkey } = payload
if(!isIpfs.multihash(from)) {
return
}
if(!isIpfs.multihash(pubkey)) {
return
}
console.log('Got a contact lookup query from ' + from + ' for ' + pubkey)
const state: Store = getState()
const profile = state.profile
const contactList = state.contactList
let reply = null
const contact = contactList.findContactInPool(pubkey)
if(contact) {
reply = contact.publishObject()
}
module.exports = function (multihash) {
if (Buffer.isBuffer(multihash)) {
multihash = bs58.encode(multihash)
}
if (CID.isCID(multihash)) {
multihash = multihash.toBaseEncodedString()
}
if (typeof multihash !== 'string') {
throw new Error('unexpected multihash type: ' + typeof multihash)
}
if (!isIPFS.multihash(multihash.split('/')[0])) {
throw new Error('not valid multihash')
}
return multihash
}
async function handleLookup(dispatch, getState, payload) {
const { from, pubkey } = payload
if(!isIpfs.multihash(from)) {
return
}
if(!isIpfs.multihash(pubkey)) {
return
}
console.log('Got a contact lookup query from ' + from + ' for ' + pubkey)
const state: Store = getState()
const profile = state.profile
const contactList = state.contactList
let reply = null
const contact = contactList.findContactInPool(pubkey)
static create(hash: string) {
if(!isIpfs.multihash(hash)) {
throw 'invalid hash'
}
return new this()
.set(writable.hash, hash)
}
function normalizeKey (key) {
let res
const isMhash = isIPFS.multihash(key)
const isPath = isIPFS.path(key)
if (!isMhash && !isPath) {
return null
}
if (isMhash) {
res = key
} else if (isPath) {
res = key.replace('/ipfs/', '')
}
if (typeof res === 'string') {
return mh.fromB58String(res)
}
await Promise.all(shares.map(async (hash: string) => {
if(!isIpfs.multihash(hash)) {
throw 'invalid hash'
}
await handleNewShare(dispatch, contact, hash)
}))