summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2019-04-27 00:25:09 +0200
committerJules Laplace <julescarbon@gmail.com>2019-04-27 00:25:09 +0200
commita0e583ee4eebe37db4011a99b65d0d60db324054 (patch)
tree51845a417f2998719160f7815d3f1f4fe8e89f20
parent8e9214f5d4d2a725eecdb11368451d99168ae240 (diff)
api now fetches url
-rw-r--r--check/app/models/sql_factory.py7
-rw-r--r--check/app/server/api.py11
-rw-r--r--client/app.js25
3 files changed, 30 insertions, 13 deletions
diff --git a/check/app/models/sql_factory.py b/check/app/models/sql_factory.py
index 5433b67..ad27f62 100644
--- a/check/app/models/sql_factory.py
+++ b/check/app/models/sql_factory.py
@@ -32,15 +32,16 @@ class FileTable(Base):
__tablename__ = 'files'
id = Column(Integer, primary_key=True)
sha256 = Column(String(64, convert_unicode=True), nullable=False)
- url = Column(String(255, convert_unicode=True), nullable=False)
phash = Column(BigInteger, nullable=False, index=True)
ext = Column(String(4, convert_unicode=True), nullable=False)
+ url = Column(String(255, convert_unicode=True), nullable=False)
def toJSON(self):
return {
'id': self.id,
'sha256': self.sha256,
'phash': self.phash,
'ext': self.ext,
+ 'url': self.url,
}
Base.metadata.create_all(engine)
@@ -56,7 +57,7 @@ def search_by_phash(phash, threshold=6, limit=1):
LIMIT :limit
"""
matches = connection.execute(text(cmd), phash=phash, threshold=threshold, limit=limit).fetchall()
- keys = ('id', 'sha256', 'phash', 'ext', 'score')
+ keys = ('id', 'sha256', 'phash', 'ext', 'url', 'score')
results = [ dict(zip(keys, values)) for values in matches ]
return results
@@ -65,7 +66,7 @@ def search_by_hash(hash):
match = session.query(FileTable).filter(FileTable.sha256 == hash)
return match.first()
-def add_phash(sha256, phash, ext, url):
+def add_phash(sha256=None, phash=None, ext=None, url=None):
"""Add a file to the table"""
rec = FileTable(sha256=sha256, phash=phash, ext=ext, url=url)
session = Session()
diff --git a/check/app/server/api.py b/check/app/server/api.py
index 3742b15..63de5b6 100644
--- a/check/app/server/api.py
+++ b/check/app/server/api.py
@@ -34,9 +34,9 @@ def match():
start = time.time()
try:
- threshold = int(request.args.get('threshold') or 6)
- limit = int(request.args.get('limit') or 1)
- add = str(request.args.get('add') or 'true') == 'true'
+ threshold = int(request.form.get('threshold') or 6)
+ limit = int(request.form.get('limit') or 1)
+ add = str(request.form.get('add') or 'true') == 'true'
except:
return jsonify({
'success': False,
@@ -61,7 +61,7 @@ def match():
im = Image.open(file.stream).convert('RGB')
else:
- url = request.args.get('url')
+ url = request.form.get('url')
if not url:
return jsonify({
'success': False,
@@ -88,7 +88,8 @@ def match():
if len(results) == 0:
if add and url:
- hash = sha256_stream(file)
+ # hash = sha256_stream(file)
+ hash = sha256_stream(io.BytesIO(raw))
add_phash(sha256=hash, phash=phash, ext=ext, url=url)
match = False
else:
diff --git a/client/app.js b/client/app.js
index cadafef..d18a5aa 100644
--- a/client/app.js
+++ b/client/app.js
@@ -4,9 +4,10 @@ import UploadImage from './lib/uploadImage.component'
import { post } from './util'
const initialState = {
- 'image': null,
- 'res': null,
- 'loading': false,
+ image: null,
+ url: "",
+ res: null,
+ loading: false,
}
export default class PhashApp extends Component {
@@ -35,6 +36,19 @@ export default class PhashApp extends Component {
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() {
@@ -108,10 +122,11 @@ export default class PhashApp extends Component {
)
}
- const { ext, phash, score, sha256 } = closest_match
+ const { phash, score, sha256, url} = closest_match
return (
<div className='results'>
- Closest match: {sha256}{'.'}{ext}<br />
+ <img src={url} /><br />
+ Closest match: {sha256}<br />
Score: {score}<br />
Phash: {phash.toString(16)}
</div>