forked from tqtezos/minter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request tqtezos#33 from tqtezos/alex-kooper/ipfs
Alex kooper/ipfs
- Loading branch information
Showing
16 changed files
with
880 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ yarn-error.log* | |
.pgdata | ||
.flextesa/ | ||
.tzindex | ||
.ipfs | ||
|
||
# Runtime data | ||
pids | ||
|
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,27 @@ | ||
#!/usr/bin/env bash | ||
|
||
IPFS_DIR=$PROJECT_ROOT_DIR/.ipfs | ||
|
||
configure_ipfs() { | ||
docker run --rm -it --name ipfs-node \ | ||
-v $IPFS_DIR:/data/ipfs \ | ||
-p 8080:8080 -p 4001:4001 -p 5001:5001 \ | ||
ipfs/go-ipfs \ | ||
config "$@" | ||
} | ||
|
||
# Enable CORS for IPFS Gateway to be able to connect to it from Web application. | ||
# It is only done on the first run when creating IPFS docker volume. | ||
if [ ! -f $IPFS_DIR/config ]; then | ||
printf "Configuring IPFS Server...\n" | ||
|
||
configure_ipfs --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]' | ||
configure_ipfs --json API.HTTPHeaders.Access-Control-Allow-Methods '["GET", "PUT", "POST"]' | ||
|
||
printf "Done configuring IPFS Server!\n" | ||
else | ||
printf "Found an IPFS configuration in $IPFS_DIR\n" | ||
fi | ||
|
||
docker stack deploy -c $DOCKER_STACK_DIR/ipfs.yml $STACK_NAME | ||
|
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,10 @@ | ||
{ | ||
"semi": true, | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"singleQuote": true, | ||
"bracketSpacing": true, | ||
"arrowParens": "avoid", | ||
"printWidth": 80 | ||
} | ||
|
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,21 @@ | ||
declare module 'ipfs-http-client' { | ||
|
||
export type FileContent = any | Blob | string; | ||
|
||
export interface Cid { | ||
toString: () => string; | ||
} | ||
|
||
export interface IpfsFile { | ||
path: string; | ||
cid: Cid; | ||
size: number; | ||
} | ||
|
||
export interface IpfsClientApi { | ||
add: (data: FileContent) => Promise<IpfsFile>; | ||
} | ||
|
||
export default function(any): IpfsClientApi; | ||
} | ||
|
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,48 @@ | ||
import url from 'url'; | ||
import IpfsClient from 'ipfs-http-client'; | ||
|
||
import config from '../config'; | ||
|
||
/** | ||
* Promisified version of FileReader.readAsArrayBuffer | ||
*/ | ||
const readBlobAsArrayBuffer = (blob: Blob): Promise<ArrayBuffer> => ( | ||
new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
|
||
reader.onload = () => { resolve(reader.result as ArrayBuffer); }; | ||
reader.onerror = () => { reject(reader.error); reader.abort(); }; | ||
|
||
reader.readAsArrayBuffer(blob); | ||
}) | ||
) | ||
|
||
const ipfsClient = IpfsClient(config.ipfs.apiUrl); | ||
|
||
export interface IpfsContent { | ||
// Content identifier, also known as 'hash' | ||
cid: string; | ||
|
||
// The size of the content in bytes | ||
size: number; | ||
|
||
// URL of the content on the IPFS server it was uploaded to (fast download) | ||
url: string; | ||
|
||
// URL of the content on one of the pubic IPFS servers (it may take a long time to download) | ||
publicGatewayUrl: string; | ||
} | ||
|
||
const uploadToIpfs = async (blob: Blob): Promise<IpfsContent> => { | ||
const buffer = await readBlobAsArrayBuffer(blob); | ||
const ipfsFile = await ipfsClient.add(buffer); | ||
|
||
return { | ||
cid: ipfsFile.cid.toString(), | ||
size: ipfsFile.size, | ||
url: url.resolve(config.ipfs.gatewayUrl, `ipfs/${ipfsFile.cid}`), | ||
publicGatewayUrl: url.resolve(config.ipfs.publicGatewayUrl, `ipfs/${ipfsFile.cid}`) | ||
} | ||
} | ||
|
||
export default uploadToIpfs; |
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
68 changes: 68 additions & 0 deletions
68
client/src/components/CreateNonFungiblePage/ImageIpfsUpload.tsx
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,68 @@ | ||
/** @jsx jsx */ | ||
import { jsx } from '@emotion/core'; | ||
import { FC } from 'react'; | ||
import { Upload, Button, message } from 'antd'; | ||
import { UploadOutlined } from '@ant-design/icons'; | ||
import { UploadChangeParam } from 'antd/lib/upload'; | ||
import { RcCustomRequestOptions, RcFile } from 'antd/lib/upload/interface'; | ||
import uploadToIpfs, { IpfsContent } from '../../api/ipfsUploader'; | ||
|
||
|
||
export interface ImageIpfsUploadProps { | ||
onChange: (info: IpfsContent) => void | ||
} | ||
|
||
const ImageIpfsUpload: FC<ImageIpfsUploadProps> = ({ onChange }) => { | ||
const onChangeHandler = async (info: UploadChangeParam) => { | ||
if (info.file.status === 'done') { | ||
const hideLoadingMessage = message.loading('Uploading image to IPFS Server...', 0);; | ||
|
||
try { | ||
const ipfsContent = await uploadToIpfs(info.file.originFileObj as Blob); | ||
message.success('Succesfully uploaded image to IPFS Server.') | ||
onChange(ipfsContent) | ||
} catch (error) { | ||
message.error(`Problems uploading image to IPFS Server! Please try later.`, 10); | ||
console.error(`Problem uploading to IPFS: ${error.toString()}`) | ||
} finally { | ||
hideLoadingMessage(); | ||
} | ||
} | ||
} | ||
|
||
const dummyRequest = ({ file, onSuccess }: RcCustomRequestOptions) => { | ||
setTimeout(() => { | ||
onSuccess({}, file); | ||
}, 0); | ||
}; | ||
|
||
const validateImageType = (file: RcFile) => { | ||
const isImage = file.type.startsWith('image') | ||
|
||
if (!isImage) { | ||
message.error(`${file.name} is not an image file`); | ||
} | ||
|
||
return isImage; | ||
} | ||
|
||
return ( | ||
<Upload | ||
customRequest={dummyRequest} | ||
showUploadList={false} | ||
beforeUpload={validateImageType} | ||
onChange={onChangeHandler} | ||
> | ||
<Button | ||
type="primary" | ||
shape="round" | ||
size="large" | ||
css={{width: '12em'}} | ||
> | ||
<UploadOutlined /> Click to Upload | ||
</Button> | ||
</Upload> | ||
); | ||
} | ||
|
||
export default ImageIpfsUpload; |
40 changes: 40 additions & 0 deletions
40
client/src/components/CreateNonFungiblePage/ImagePreview.tsx
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,40 @@ | ||
/** @jsx jsx */ | ||
import { jsx } from '@emotion/core'; | ||
import styled from '@emotion/styled' | ||
import { FC } from 'react'; | ||
import { Form, Empty } from 'antd'; | ||
import { IpfsContent } from '../../api/ipfsUploader'; | ||
|
||
const Image = styled.img({ | ||
width: '100%' | ||
}); | ||
|
||
const ImageContainer = styled.div({ | ||
width: '20em', | ||
border: 'solid 1px #C8C8C8', | ||
padding: '1.5em' | ||
}) | ||
|
||
const ImagePreview: FC<{ipfsContent?: IpfsContent}> = ({ ipfsContent }) => ( | ||
<Form layout="vertical"> | ||
<Form.Item label="Image Preview"> | ||
<ImageContainer> | ||
{ipfsContent ? ( | ||
<a | ||
href={ipfsContent.publicGatewayUrl} | ||
title="Click to download this image from the IPFS Public Gateway" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
<Image src={ipfsContent.url} alt="token" /> | ||
</a> | ||
) : ( | ||
<Empty description="Upload an Image" image={Empty.PRESENTED_IMAGE_SIMPLE} /> | ||
)} | ||
</ImageContainer> | ||
</Form.Item> | ||
</Form> | ||
); | ||
|
||
export default ImagePreview; | ||
|
Oops, something went wrong.