Skip to content
Snippets Groups Projects
Commit 0f62fe25 authored by James Calfee's avatar James Calfee
Browse files

Slate image upload integration (work in progress) #185

parent 5f1ac978
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ export const userWatches = [ ...@@ -20,6 +20,7 @@ export const userWatches = [
loginErrorWatch, loginErrorWatch,
lookupPreviousOwnerAuthorityWatch, lookupPreviousOwnerAuthorityWatch,
watchLoadSavingsWithdraw, watchLoadSavingsWithdraw,
uploadImageWatch,
] ]
const highSecurityPages = Array(/\/market/, /\/@.+\/(transfers|permissions|password)/, /\/~witnesses/) const highSecurityPages = Array(/\/market/, /\/@.+\/(transfers|permissions|password)/, /\/~witnesses/)
...@@ -348,6 +349,68 @@ function* lookupPreviousOwnerAuthority({payload: {}}) { ...@@ -348,6 +349,68 @@ function* lookupPreviousOwnerAuthority({payload: {}}) {
yield put(user.actions.setUser({previous_owner_authority})) yield put(user.actions.setUser({previous_owner_authority}))
} }
import {Signature, hash} from 'shared/ecc'
function* uploadImageWatch() {
yield* takeLatest('user/UPLOAD_IMAGE', uploadImage);
}
function* uploadImage({payload: {file, progress}}) {
progress = msg => {
console.log('Upload image progress', msg)
progress(msg)
}
if(!file) {
console.error('uploadImage required: file')
return
}
const reader = new FileReader()
const data = yield new Promise(resolve => {
reader.addEventListener('load', () => {
const result = new Buffer(reader.result, 'binary')
resolve(result)
})
reader.readAsBinaryString(file)
})
const bufSha = hash.sha256(data)
const stateUser = yield select(state => state.user)
const username = stateUser.getIn(['current', 'username'])
const d = stateUser.getIn(['current', 'private_keys', 'posting_private'])
if(!username) {
progress({error: 'Not logged in'})
return
}
if(!d) {
progress({error: 'Login with your posting key'})
return
}
const formData = new FormData()
formData.append('file', file)
const sig = Signature.signBufferSha256(bufSha, d)
const postUrl = `http://localhost:3234/${username}/${sig.toHex()}`
fetch(postUrl, {
method: 'post',
body: formData
})
.then(r => r.json())
.then(res => {
const {error} = res
if(error) {
progress({error: 'Error: ' + error})
return
}
const {files: [url]} = res
progress({url: `${url}/${file.name}`})
})
}
// function* getCurrentAccount() { // function* getCurrentAccount() {
// const current = yield select(state => state.user.get('current')) // const current = yield select(state => state.user.get('current'))
// if (!current) return // if (!current) return
......
import React from 'react' import React from 'react'
import {connect} from 'react-redux'
export default class Image extends React.Component { export default connect(
(state, ownProps) => ownProps,
dispatch => ({
uploadImage: (file, progress) => {
dispatch({
type: 'user/UPLOAD_IMAGE',
payload: {file, progress},
})
},
})
)(
class Image extends React.Component {
state = {}; state = {};
componentDidMount() { componentDidMount() {
...@@ -15,7 +28,7 @@ export default class Image extends React.Component { ...@@ -15,7 +28,7 @@ export default class Image extends React.Component {
console.log("** image being loaded.. ----->", file) console.log("** image being loaded.. ----->", file)
const reader = new FileReader() const reader = new FileReader()
reader.addEventListener('load', () => { reader.addEventListener('load', () => {
//this.setState({ src: reader.result }) // this.setState({ src: reader.result })
const {editor, node} = this.props const {editor, node} = this.props
const state = editor.getState(); const state = editor.getState();
const next = state const next = state
...@@ -27,22 +40,27 @@ export default class Image extends React.Component { ...@@ -27,22 +40,27 @@ export default class Image extends React.Component {
}) })
.apply() .apply()
editor.onChange(next) editor.onChange(next)
const {uploadImage} = this.props
uploadImage(file, progress => {
this.setState({ progress })
})
}) })
reader.readAsDataURL(file) reader.readAsDataURL(file)
} }
render() { render() {
const { node, state, attributes } = this.props const { node, state, attributes } = this.props
let { src } = this.state let { src, progress } = this.state
const isFocused = state.selection.hasEdgeIn(node) const isFocused = state.selection.hasEdgeIn(node)
const className = isFocused ? 'active' : null const className = isFocused ? 'active' : null
if(src) { if(src) {
console.log("** uploaded image being rendered..", src, state) console.log("** uploaded image being rendered..", (src ? src.substring(0, 30) : '') + '...', state)
} else { } else {
const src2 = node.data.get('src') const src2 = node.data.get('src')
console.log("** image source was not in state. data.src = ", src2) console.log("** image source was not in state. data.src = ", (src2 ? src2.substring(0, 30) : '') + '...')
src = src2; src = src2;
} }
...@@ -50,9 +68,10 @@ export default class Image extends React.Component { ...@@ -50,9 +68,10 @@ export default class Image extends React.Component {
// src = 'https://img1.steemit.com/0x0/http://ariasprado.name/wp-content/uploads/2012/09/missing-tile-256x256.png' // src = 'https://img1.steemit.com/0x0/http://ariasprado.name/wp-content/uploads/2012/09/missing-tile-256x256.png'
// src = $STM_Config.img_proxy_prefix + '0x0/' + src // src = $STM_Config.img_proxy_prefix + '0x0/' + src
} }
return src ? <span>
return src <img {...attributes} src={src} className={className} />
? <img {...attributes} src={src} className={className} /> {progress}
</span>
: <span>Loading... ({src})</span> : <span>Loading... ({src})</span>
} }
} })
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment