Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import IPFS from 'ipfs'
import ipfsClient from 'ipfs-http-client'
import { Buffer } from 'buffer'
import { Matcher } from '@areknawo/rex'
import { getImageType } from './images'
const ipfsMatcher = new Matcher().begin().find('ipfs/')
export const ipfs = new IPFS()
const ipfsHttp = ipfsClient({ host: 'test-ipfs.status.im', port: '2053', protocol: 'https' })
const ipfsHttpTheGraph = ipfsClient({ host: 'api.thegraph.com', 'api-path': '/ipfs/api/v0/', protocol: 'https', port: '443' })
window.ipfsHttp = ipfsHttp
window.ipfsHttpTheGraph = ipfsHttpTheGraph
window.jsIPFS = ipfs
ipfs.on('ready', () => {
console.log('Node is ready to use!')
// Add status ipfs node as peer
ipfs.bootstrap.add('/ip4/35.188.209.83/tcp/4001/ipfs/QmZqW6QHdDVyHMvYktpA1PAuKgvqhwumbuFqFB5URedSbA')
})
export const isIpfs = str => ipfsMatcher.test(str)
export const captureFile = (event, cb, imgCb) => {
event.stopPropagation()
;(async (argv) => {
let ipfs
try {
if (argv['ipfs-api']) {
// --ipfs-api option
const IPFS = require('ipfs-http-client')
const addr = multiaddr(argv['ipfs-api'])
const nodeAddress = addr.nodeAddress()
ipfs = new IPFS(nodeAddress.address, nodeAddress.port)
} else {
const IPFS = require('ipfs')
ipfs = await IPFS.create({ start: false })
}
} catch (e) {
console.error(e)
throw new Error('Please use npm to install either `ipfs` or `ipfs-http-client`.')
}
// Register new MIME type for .ambient files
mime.extensions['text/ambients'] = ['ambient']
mime.types.ambient = 'text/ambients'
const file = fs.readFileSync(argv.input).toString().trim()
let result
switch (mime.lookup(argv.input)) {
case 'application/javascript':
result = await output(ipfs, file, argv); break
}
// Ensure each segment multihash in metadata obj has an associated file, else error.
await Promise.all(metadataJSON.track_segments.map(async segment => {
const file = await models.File.findOne({ where: {
multihash: segment.multihash,
cnodeUserUUID: req.session.cnodeUserUUID,
trackUUID: null
} })
if (!file) {
return errorResponseBadRequest(`No file found for provided segment multihash: ${segment.multihash}`)
}
}))
// Store + pin metadata multihash to disk + IPFS.
const metadataBuffer = Buffer.from(JSON.stringify(metadataJSON))
const { multihash, fileUUID } = await saveFileFromBuffer(req, metadataBuffer, 'metadata')
return successResponse({ 'metadataMultihash': multihash, 'metadataFileUUID': fileUUID })
}))
app.post('/audius_users/metadata', authMiddleware, syncLockMiddleware, handleResponse(async (req, res) => {
// TODO - input validation
const metadataJSON = req.body.metadata
const metadataBuffer = Buffer.from(JSON.stringify(metadataJSON))
const { multihash, fileUUID } = await saveFileFromBuffer(req, metadataBuffer, 'metadata')
return successResponse({ 'metadataMultihash': multihash, 'metadataFileUUID': fileUUID })
}))
public async extract(_organization: string, _organism: string): Promise {
const ipfs = new IPFSClient({ host: 'localhost', port: '5001', protocol: 'http' })
const organization = await this.organizations.load({ name: _organization })
const organism = await organization.organisms.load({ name: _organism })
const head = await organism.head()
if (head.blockstamp !== 0) {
const fiber = (await this.fibers.current()) as Fiber
await fiber.snapshot(`Automatic snapshot before extraction of ${_organization}:${_organism}`)
await this._clear()
const metadata = await ipfs.dag.get(head.metadata)
await this._download(metadata.value.tree)
}
}
export function connectThroughHTTP(address) {
if (typeof address === 'string') {
return ipfsHttpClient(parseAddressAsURL(address))
}
return ipfsHttpClient(address)
}
constructor(props) {
super(props)
this.ipfs = IPFS({ host: 'ipfs.infura.io', port: '5001', protocol: 'https' })
this.ipld = new IPLD({
blockService: this.ipfs.block,
formats: [IPLDGit],
})
this.state = {
diffViewState: 'split',
diffs: [],
highlightLine: [],
}
}
constructor(helper: Helper) {
const url = new URL(helper.config.ipfs.gateway)
this.helper = helper
this._ipfs = IPFS({ host: url.hostname, port: url.port, protocol: url.protocol.slice(0, url.protocol.length - 1) })
this._ipld = new IPLD({
blockService: this._ipfs.block,
formats: [IPLDGit],
})
}
import ipfsClient from 'ipfs-http-client'
export const ipfs = ipfsClient({
host: 'localhost',
port: '5001',
protocol: 'http',
})
private async _download(cid: string, relativePath = ''): Promise {
const ipfs = new IPFSClient({ host: 'localhost', port: '5001', protocol: 'http' })
const entries = await ipfs.ls(cid)
for (const entry of entries) {
if (entry.type === 'dir') {
await this._download(entry.hash, npath.join(relativePath, entry.name))
await fs.ensureDir(npath.join(this.paths.root, this.paths.root, relativePath, entry.name))
} else {
const buffer = await ipfs.cat(entry.hash)
await fs.ensureFile(npath.join(this.paths.root, relativePath, entry.name))
await fs.writeFile(npath.join(this.paths.root, relativePath, entry.name), buffer)
}
}
}