forked from sachdevs/Note-it-Now
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
101 lines (84 loc) · 2.8 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import sys
import os
from flask import Flask, url_for, jsonify, Response, session, request, redirect, render_template
import flask
import json
import base64
import uuid
from flask_oauthlib.client import OAuth
# Credentials file for google
import credentials
from ocr import *
app = Flask(__name__, static_url_path='/static')
app.config['GOOGLE_ID'] = credentials.client_id
app.config['GOOGLE_SECRET'] = credentials.client_secret
app.secret_key = "dev"
oauth = OAuth(app)
google = oauth.remote_app(
'google',
consumer_key=app.config.get('GOOGLE_ID'),
consumer_secret=app.config.get('GOOGLE_SECRET'),
request_token_params={'scope':'email'},
base_url='https://www.googleapis.com/oauth2/v1/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://accounts.google.com/o/oauth2/token',
authorize_url='https://accounts.google.com/o/oauth2/auth',
)
@google.tokengetter
def get_google_oauth_token():
return session.get('google_token')
@app.route('/login')
def login():
return google.authorize(callback=url_for('authorized', _external=True))
@app.route('/login/authorized')
def authorized():
resp = google.authorized_response()
if resp is None:
return 'Access denied: reason=%s error=%s' % (
request.args['error_reason'],
request.args['error_description']
)
session['google_token'] = (resp['access_token'], '')
me = google.get('userinfo')
return redirect(url_for('main'))
@app.route('/')
def authenticate():
return render_template("index.html")
@app.route('/main')
def main():
if("google_token" in session):
return render_template('main.html')
else:
return redirect("/")
#endpoint to accept image upload
@app.route('/upload', methods=['POST'])
def spliceImage():
if request.method == 'POST':
data = request.form['image']
tempFileName = "static/temp/"+str(uuid.uuid4())
decode64String(tempFileName,data);
images = segmenter.getImages(tempFileName)
return json.dumps(images)
abort(404)
@app.route("/logout")
def logout():
session.pop('google_token', None)
return redirect(url_for('index'))
@app.route('/files', methods=['POST'])
def filesView():
if("access_token" in session):
return flask.jsonify(**{ "didstuff": True })
#helper function to decode the image from the import client
def decode64String(filename, imagestr):
with open(filename,"wb") as f:
f.write(base64.b64decode(imagestr))
return filename
#helper function to encode to 64bit to send to a client
def get64String(filename):
with open(filename, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return encoded_string
#run the server
if __name__ == "__main__":
app.run(debug = True, host = '0.0.0.0', port = 5000)