Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

. #2

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const DStorage = artifacts.require("DStorage");
//Import Contractconst DStorage = artifacts.require("DStorage");

module.exports = function(deployer) {
deployer.deploy(DStorage);
//Deploy Contract
};
4,939 changes: 0 additions & 4,939 deletions src/abis/DStorage.json

This file was deleted.

1,439 changes: 0 additions & 1,439 deletions src/abis/Migrations.json

This file was deleted.

122 changes: 36 additions & 86 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import DStorage from '../abis/DStorage.json'
//import DStorage from '../abis/DStorage.json'
import React, { Component } from 'react';
import Navbar from './Navbar'
import Main from './Main'
import Web3 from 'web3';
import './App.css';

const ipfsClient = require('ipfs-http-client')
const ipfs = ipfsClient({ host: 'ipfs.infura.io', port: 5001, protocol: 'https' }) // leaving out the arguments will default to these values
//Declare IPFS

class App extends Component {

Expand All @@ -16,105 +15,56 @@ class App extends Component {
}

async loadWeb3() {
if (window.ethereum) {
window.web3 = new Web3(window.ethereum)
await window.ethereum.enable()
}
else if (window.web3) {
window.web3 = new Web3(window.web3.currentProvider)
}
else {
window.alert('Non-Ethereum browser detected. You should consider trying MetaMask!')
}
//Setting up Web3
}

async loadBlockchainData() {
const web3 = window.web3
// Load account
const accounts = await web3.eth.getAccounts()
this.setState({ account: accounts[0] })
// Network ID
const networkId = await web3.eth.net.getId()
const networkData = DStorage.networks[networkId]
if(networkData) {
// Assign contract
const dstorage = new web3.eth.Contract(DStorage.abi, networkData.address)
this.setState({ dstorage })
// Get files amount
const filesCount = await dstorage.methods.fileCount().call()
this.setState({ filesCount })
// Load files&sort by the newest
for (var i = filesCount; i >= 1; i--) {
const file = await dstorage.methods.files(i).call()
this.setState({
files: [...this.state.files, file]
})
}
} else {
window.alert('DStorage contract not deployed to detected network.')
}
//Declare Web3

//Load account

//Network ID

//IF got connection, get data from contracts
//Assign contract

//Get files amount

//Load files&sort by the newest

//Else
//alert Error

}

// Get file from user
captureFile = event => {
event.preventDefault()

const file = event.target.files[0]
const reader = new window.FileReader()

reader.readAsArrayBuffer(file)
reader.onloadend = () => {
this.setState({
buffer: Buffer(reader.result),
type: file.type,
name: file.name
})
console.log('buffer', this.state.buffer)
}
}


//Upload File
uploadFile = description => {
console.log("Submitting file to IPFS...")

// Add file to the IPFS
ipfs.add(this.state.buffer, (error, result) => {
console.log('IPFS result', result.size)
if(error) {
console.error(error)
return
}

this.setState({ loading: true })
// Assign value for the file without extension
if(this.state.type === ''){
this.setState({type: 'none'})
}
this.state.dstorage.methods.uploadFile(result[0].hash, result[0].size, this.state.type, this.state.name, description).send({ from: this.state.account }).on('transactionHash', (hash) => {
this.setState({
loading: false,
type: null,
name: null
})
window.location.reload()
}).on('error', (e) =>{
window.alert('Error')
this.setState({loading: false})
})
})

//Add file to the IPFS

//Check If error
//Return error

//Set state to loading

//Assign value for the file without extension

//Call smart contract uploadFile function

}

//Set states
constructor(props) {
super(props)
this.state = {
account: '',
dstorage: null,
files: [],
loading: false,
type: null,
name: null
}
this.uploadFile = this.uploadFile.bind(this)
this.captureFile = this.captureFile.bind(this)

//Bind functions
}

render() {
Expand Down
69 changes: 6 additions & 63 deletions src/components/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,14 @@ class Main extends Component {
<main role="main" className="col-lg-12 ml-auto mr-auto" style={{ maxWidth: '1024px' }}>
<div className="content">
<p>&nbsp;</p>
<div className="card mb-3 mx-auto bg-dark" style={{ maxWidth: '512px' }}>
<h2 className="text-white text-monospace bg-dark"><b><ins>Share File</ins></b></h2>
<form onSubmit={(event) => {
event.preventDefault()
const description = this.fileDescription.value
this.props.uploadFile(description)
}} >
<div className="form-group">
<br></br>
<input
id="fileDescription"
type="text"
ref={(input) => { this.fileDescription = input }}
className="form-control text-monospace"
placeholder="description..."
required />
</div>
<input type="file" onChange={this.props.captureFile} className="text-white text-monospace"/>
<button type="submit" className="btn-primary btn-block"><b>Upload!</b></button>
</form>
</div>
<h1>DStorage starter_code</h1>
{/* Creatining uploading card ... */}
{/* Uploading file... */}
<p>&nbsp;</p>
{/* Create Table*/}
<table className="table-sm table-bordered text-monospace" style={{ width: '1000px', maxHeight: '450px'}}>
<thead style={{ 'fontSize': '15px' }}>
<tr className="bg-dark text-white">
<th scope="col" style={{ width: '10px'}}>id</th>
<th scope="col" style={{ width: '200px'}}>name</th>
<th scope="col" style={{ width: '230px'}}>description</th>
<th scope="col" style={{ width: '120px'}}>type</th>
<th scope="col" style={{ width: '90px'}}>size</th>
<th scope="col" style={{ width: '90px'}}>date</th>
<th scope="col" style={{ width: '120px'}}>uploader/view</th>
<th scope="col" style={{ width: '120px'}}>hash/view/get</th>
</tr>
</thead>
{ this.props.files.map((file, key) => {
return(
<thead style={{ 'fontSize': '12px' }} key={key}>
<tr>
<td>{file.fileId}</td>
<td>{file.fileName}</td>
<td>{file.fileDescription}</td>
<td>{file.fileType}</td>
<td>{convertBytes(file.fileSize)}</td>
<td>{moment.unix(file.uploadTime).format('h:mm:ss A M/D/Y')}</td>
<td>
<a
href={"https://etherscan.io/address/" + file.uploader}
rel="noopener noreferrer"
target="_blank">
{file.uploader.substring(0,10)}...
</a>
</td>
<td>
<a
href={"https://ipfs.infura.io/ipfs/" + file.fileHash}
rel="noopener noreferrer"
target="_blank">
{file.fileHash.substring(0,10)}...
</a>
</td>
</tr>
</thead>
)
})}
{/* Set table columns */}
{/* Mapping rows... */}
</table>
</div>
</main>
Expand Down
22 changes: 1 addition & 21 deletions src/components/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,7 @@ class Navbar extends Component {
D$t0r@g3
</a>
<ul className="navbar-nav px-3">
<li>
<small id="account">
<a target="_blank"
alt=""
className="text-white"
rel="noopener noreferrer"
href={"https://etherscan.io/address/" + this.props.account}>
{this.props.account.substring(0,6)}...{this.props.account.substring(38,42)}
</a>
</small>
{ this.props.account
? <img
alt=""
className='ml-2'
width='30'
height='30'
src={`data:image/png;base64,${new Identicon(this.props.account, 30).toString()}`}
/>
: <span></span>
}
</li>
<b className='text-white'>{'0x0'}</b>
</ul>
</nav>
);
Expand Down
52 changes: 17 additions & 35 deletions src/contracts/DStorage.sol
Original file line number Diff line number Diff line change
@@ -1,55 +1,37 @@
pragma solidity ^0.5.0;

contract DStorage {
string public name = 'DStorage';
uint public fileCount = 0;
mapping(uint => File) public files;

struct File {
uint fileId;
string fileHash;
uint fileSize;
string fileType;
string fileName;
string fileDescription;
uint uploadTime;
address payable uploader;
}
// Name
// Number of files
// Mapping fileId=>Struct

// Struct

event FileUploaded(
uint fileId,
string fileHash,
uint fileSize,
string fileType,
string fileName,
string fileDescription,
uint uploadTime,
address payable uploader
);

// Event

constructor() public {
}

function uploadFile(string memory _fileHash, uint _fileSize, string memory _fileType, string memory _fileName, string memory _fileDescription) public {
// Upload File function

// Make sure the file hash exists
require(bytes(_fileHash).length > 0);

// Make sure file type exists
require(bytes(_fileType).length > 0);

// Make sure file description exists
require(bytes(_fileDescription).length > 0);

// Make sure file fileName exists
require(bytes(_fileName).length > 0);

// Make sure uploader address exists
require(msg.sender!=address(0));

// Make sure file size is more than 0
require(_fileSize>0);


// Increment file id
fileCount ++;

// Add File to the contract
files[fileCount] = File(fileCount, _fileHash, _fileSize, _fileType, _fileName, _fileDescription, now, msg.sender);

// Trigger an event
emit FileUploaded(fileCount, _fileHash, _fileSize, _fileType, _fileName, _fileDescription, now, msg.sender);
}

}