From c9c72cdc3128fe272edeb6ec20959b2248f33877 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 25 Apr 2019 19:46:20 +0200 Subject: frontend demo --- client/app.js | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 client/app.js (limited to 'client/app.js') diff --git a/client/app.js b/client/app.js new file mode 100644 index 0000000..96c426c --- /dev/null +++ b/client/app.js @@ -0,0 +1,102 @@ +import React, { Component } from 'react' + +import UploadImage from './lib/uploadImage.component' +import { post } from './util' + +const initialState = { + 'image': null, + '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 }) + }) + } + + render() { + return ( +
+

Perceptual Hash Demo

+ {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, closest_match } = res + if (!success) { + return ( +
+ Error: {error} +
+ ) + } + + if (!match) { + return ( +
+ No match, image added to database +
+ ) + } + console.log(closest_match) + const { ext, phash, score, sha256 } = closest_match + return ( +
+ Closest match: {sha256}{'.'}{ext}
+ Score: {score}
+ Phash: {phash.toString(16)} +
+ ) + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2