import React, { Component } from 'react' import UploadImage from './lib/uploadImage.component' import { post } from './util' import './app.css' const initialState = { image: null, url: "", res: null, loading: false, } export default class PhashApp extends Component { state = { ...initialState } upload(blob) { if (this.state.image) { URL.revokeObjectURL(this.state.image) } const url = URL.createObjectURL(blob) this.setState({ image: url, loading: true }) const fd = new FormData() fd.append('q', blob) post('/api/v1/match', fd) .then(res => { console.log(res) this.setState({ res, loading: false }) }) .catch(err => { console.log(err) this.setState({ loading: false }) }) } submit() { const { url } = this.state if (!url || url.indexOf('http') !== 0) return this.setState({ image: url, loading: true }) const fd = new FormData() fd.append('url', url) post('/api/v1/match', fd) .then(res => { console.log(res) this.setState({ res, loading: false }) }) .catch(err => { console.log(err) this.setState({ loading: false }) }) } render() { return (

Search by Image

{this.renderQuery()} {this.renderResults()}
) } renderQuery() { const { image } = this.state const style = {} if (image) { style.backgroundImage = 'url(' + image + ')' style.backgroundSize = 'cover' style.opacity = 1 } return (

{image &&
}
) } renderResults() { const { loading, res } = this.state if (!res) { return (
) } if (loading) { return (
Loading...
) } const { success, error, match, results } = res if (!success) { return (
Error: {error.replace(/_/g, ' ')}
) } if (!match) { return (
No match, image added to database
) } return (
{results.map(({ phash, score, sha256, url }) => (

{score == 0 ? Exact match : Score: {score} }
{sha256} Phash: {phash.toString(16)}
))}
) } }