From e512ed4471b95fe39c38f9451c8e6d40ef8f7807 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Mon, 9 Sep 2019 22:23:29 +0200 Subject: [PATCH] IPFS publishing fixes, allow dropping multiple files --- client/.env.local.example | 1 + client/src/config.ts | 3 +++ client/src/hooks/use-ipfs-api.tsx | 2 +- .../src/routes/Publish/Files/Ipfs/index.tsx | 19 ++++++++++++------- client/src/routes/Publish/Files/index.tsx | 2 ++ 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/client/.env.local.example b/client/.env.local.example index 27aa7b2..afc8d91 100644 --- a/client/.env.local.example +++ b/client/.env.local.example @@ -59,3 +59,4 @@ REACT_APP_REPORT_EMAIL="test@example.com" # REACT_APP_SHOW_CHANNELS=true # REACT_APP_ALLOW_PRICING=true # REACT_APP_SHOW_REQUEST_TOKENS_BUTTON=true +# REACT_APP_IPFS_GATEWAY_URI="https://gateway.ipfs.io" diff --git a/client/src/config.ts b/client/src/config.ts index c3b7875..9439cab 100644 --- a/client/src/config.ts +++ b/client/src/config.ts @@ -35,3 +35,6 @@ export const allowPricing = process.env.REACT_APP_ALLOW_PRICING === 'true' || false export const showRequestTokens = process.env.REACT_APP_SHOW_REQUEST_TOKENS_BUTTON === 'true' || false +// https://ipfs.github.io/public-gateway-checker/ +export const ipfsGatewayUri = + process.env.REACT_APP_IPFS_GATEWAY_URI || 'https://gateway.ipfs.io' diff --git a/client/src/hooks/use-ipfs-api.tsx b/client/src/hooks/use-ipfs-api.tsx index 52f8ad9..40fb24f 100644 --- a/client/src/hooks/use-ipfs-api.tsx +++ b/client/src/hooks/use-ipfs-api.tsx @@ -24,7 +24,7 @@ export default function useIpfsApi(config: IpfsConfig) { ipfsMessage = 'Checking IPFS gateway...' try { - const message = 'Connected to IPFS gateway' + const message = `Connected to ${config.host}` console.time(message) ipfs = await ipfsClient(config) console.timeEnd(message) diff --git a/client/src/routes/Publish/Files/Ipfs/index.tsx b/client/src/routes/Publish/Files/Ipfs/index.tsx index 161d031..6810d68 100644 --- a/client/src/routes/Publish/Files/Ipfs/index.tsx +++ b/client/src/routes/Publish/Files/Ipfs/index.tsx @@ -6,6 +6,7 @@ import Label from '../../../../components/atoms/Form/Label' import Spinner from '../../../../components/atoms/Spinner' import Dropzone from '../../../../components/molecules/Dropzone' import { formatBytes, pingUrl } from './utils' +import { ipfsGatewayUri } from '../../../../config' import styles from './index.module.scss' const config = { @@ -26,10 +27,7 @@ export default function Ipfs({ addFile }: { addFile(url: string): void }) { const [loading, setLoading] = useState(false) const [message, setMessage] = useState('') - async function saveToIpfs( - data: File | Buffer | ReadableStream, - size: number - ) { + async function saveToIpfs(data: Buffer, size: number) { const totalSize = formatBytes(size, 0) setLoading(true) @@ -52,9 +50,8 @@ export default function Ipfs({ addFile }: { addFile(url: string): void }) { // Ping gateway url to make it globally available, // but store native url in DDO. - // https://ipfs.github.io/public-gateway-checker/ + const urlGateway = `${ipfsGatewayUri}/ipfs/${cid}` const url = `ipfs://${cid}` - const urlGateway = `https://ipfs.infura.io/ipfs/${cid}` setMessage('Checking IPFS gateway URL') await pingUrl(urlGateway) @@ -68,7 +65,15 @@ export default function Ipfs({ addFile }: { addFile(url: string): void }) { } function handleOnDrop(files: File[]) { - files.forEach((file: File) => saveToIpfs(file, file.size)) + files.forEach((file: File) => { + const reader: any = new FileReader() + + reader.readAsArrayBuffer(file) + reader.onloadend = () => { + const buffer: any = Buffer.from(reader.result) + saveToIpfs(buffer, file.size) + } + }) } return ( diff --git a/client/src/routes/Publish/Files/index.tsx b/client/src/routes/Publish/Files/index.tsx index fc38b6f..50a0515 100644 --- a/client/src/routes/Publish/Files/index.tsx +++ b/client/src/routes/Publish/Files/index.tsx @@ -128,6 +128,8 @@ export default class Files extends PureComponent { isFormShown: false, isIpfsFormShown: false }) + + this.forceUpdate() } private removeFile = (index: number) => {