-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This eliminates the need for running an ipfs peer, so no need to listen on any port, forward ports, do NAT traversal or any of that. It also means we can now upload from Travis CI! refs #11
- Loading branch information
1 parent
2d4e2c9
commit 14e7453
Showing
5 changed files
with
116 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const _ = require('lodash') | ||
const fp = require('lodash/fp') | ||
const stringify = require('json-stringify-safe') | ||
const prettier = require('prettier') | ||
const jsonifyError = require('jsonify-error') | ||
const neatFrame = require('neat-frame') | ||
const { stripIndent } = require('common-tags') | ||
|
||
// # Pure functions | ||
|
||
function formatError(e) { | ||
const prettierJson = obj => | ||
prettier.format(stringify(obj), { | ||
parser: 'json', | ||
printWidth: 72, | ||
tabWidth: 2, | ||
}) | ||
const beautifyStr = fp.pipe( | ||
stripIndent, | ||
str => neatFrame(str, { trim: false }) | ||
) | ||
if (_.isError(e)) { | ||
eStr = prettierJson(jsonifyError(e)) | ||
} else if (_.isString(e)) { | ||
eStr = e | ||
} else if (_.isObjectLike(e)) { | ||
eStr = prettierJson(e) | ||
} | ||
const beautifulErrorString = '\n' + beautifyStr(eStr) | ||
return beautifulErrorString | ||
} | ||
|
||
// Effectful functions | ||
|
||
function logError(e) { | ||
const errorString = formatError(e) | ||
console.error(errorString) | ||
return errorString | ||
} | ||
|
||
module.exports = { formatError, logError } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const axios = require('axios') | ||
const fs = require('fs') | ||
const FormData = require('form-data') | ||
const recursive = require('recursive-fs') | ||
const ora = require('ora') | ||
const { logError } = require('./logging') | ||
|
||
const chalk = require('chalk') | ||
const white = chalk.whiteBright | ||
|
||
module.exports.setupPinata = ({ apiKey, secretApiKey }) => { | ||
const url = 'https://api.pinata.cloud/pinning/pinFileToIPFS' | ||
|
||
// we gather the files from a local directory in this example, but a valid | ||
// readStream is all that's needed for each file in the directory. | ||
return async (publicDirPath, pinataMetadata = {}) => { | ||
const spinner = ora() | ||
spinner.start( | ||
`📠 Uploading and pinning via https to ${white('pinata.cloud')}…` | ||
) | ||
|
||
try { | ||
const response = await new Promise(resolve => { | ||
recursive.readdirr(publicDirPath, (_err, _dirs, files) => { | ||
let data = new FormData() | ||
files.forEach(file => { | ||
data.append('file', fs.createReadStream(file), { | ||
// for each file stream, we need to include the correct | ||
// relative file path | ||
filepath: file, | ||
}) | ||
}) | ||
|
||
const metadata = JSON.stringify(pinataMetadata) | ||
data.append('pinataMetadata', metadata) | ||
|
||
axios | ||
.post(url, data, { | ||
// Infinity is needed to prevent axios from erroring out with | ||
// large directories | ||
maxContentLength: 'Infinity', | ||
headers: { | ||
'Content-Type': `multipart/form-data; boundary=${data._boundary}`, | ||
pinata_api_key: apiKey, | ||
pinata_secret_api_key: secretApiKey, | ||
}, | ||
}) | ||
.then(resolve) | ||
}) | ||
}) | ||
|
||
spinner.succeed("📌 It's pinned to Pinata now with hash:") | ||
const hash = response.data.IpfsHash | ||
spinner.info(`🔗 ${hash}`) | ||
return hash | ||
} catch (e) { | ||
spinner.fail("💔 Uploading to Pinata didn't work.") | ||
logError(e) | ||
return undefined | ||
} | ||
} | ||
} |