|
@@ -4,6 +4,7 @@ from flask_bootstrap import Bootstrap
|
|
|
import sqlite3
|
|
|
import re
|
|
|
from difflib import SequenceMatcher
|
|
|
+from flask_cors import CORS
|
|
|
import PyPDF2
|
|
|
import difflib
|
|
|
import fitz
|
|
@@ -11,9 +12,11 @@ from zipfile import ZipFile
|
|
|
import uuid
|
|
|
import sys
|
|
|
import os
|
|
|
+from pathlib import Path
|
|
|
|
|
|
ALLOWED_EXTENSIONS = {'pdf'}
|
|
|
app = Flask(__name__)
|
|
|
+CORS(app)
|
|
|
Bootstrap(app)
|
|
|
app.secret_key = '12345'
|
|
|
def allowed_file(filename):
|
|
@@ -27,22 +30,27 @@ def upload_reference_file():
|
|
|
cursor = sqliteConnection.cursor()
|
|
|
print("Connected to SQLite")
|
|
|
cursor.execute('''CREATE TABLE IF NOT EXISTS reference_file (
|
|
|
- UUID NOT NULL,
|
|
|
- reference_pdf_name NOT NULL,
|
|
|
- reference_pdf text NOT NULL
|
|
|
- );''')
|
|
|
+ UUID NOT NULL,
|
|
|
+ reference_pdf_name NOT NULL,
|
|
|
+ reference_pdf text NOT NULL,
|
|
|
+ description text NOT NULL
|
|
|
+ );''')
|
|
|
query = """ INSERT INTO reference_file
|
|
|
- (UUID,reference_pdf_name,reference_pdf) VALUES (?,?,?)"""
|
|
|
+ (UUID,reference_pdf_name,reference_pdf, description) VALUES (?,?,?,?)"""
|
|
|
+ #### POST REQUEST FOR UPLOAD, DELETE
|
|
|
if request.method == 'POST':
|
|
|
+ #### UPLOAD
|
|
|
if request.files:
|
|
|
+ print('sdddssd')
|
|
|
+ desc = request.form['description']
|
|
|
file_ref = request.files['reference']
|
|
|
- files_json = [{'file_ref':file_ref.filename,'uuid':str(uu_id.int)}]
|
|
|
+ files_json = [{'file_ref':file_ref.filename,'uuid':str(uu_id.int), 'desc':desc}]
|
|
|
print(files_json)
|
|
|
# print(allowed_file(file_orig.filename))
|
|
|
try:
|
|
|
if allowed_file(file_ref.filename):
|
|
|
blob_ref = base64.b64encode(file_ref.read())
|
|
|
- data_tuple = (str(uu_id.int),file_ref.filename,blob_ref)
|
|
|
+ data_tuple = (str(uu_id.int),file_ref.filename,blob_ref,desc)
|
|
|
cursor.execute(query,data_tuple)
|
|
|
sqliteConnection.commit()
|
|
|
cursor.close()
|
|
@@ -55,6 +63,7 @@ def upload_reference_file():
|
|
|
print(exc_type, fname, exc_tb.tb_lineno)
|
|
|
print(e)
|
|
|
return Response("Error in uploading", status=400, mimetype='application/json')
|
|
|
+ #### DELETE
|
|
|
else:
|
|
|
uuid_value = request.json['uuid']
|
|
|
print(str(uuid_value))
|
|
@@ -62,9 +71,11 @@ def upload_reference_file():
|
|
|
print(sql)
|
|
|
cursor.execute(sql)
|
|
|
sqliteConnection.commit()
|
|
|
+ return jsonify('') #Response("Deleted", mimetype='application/json')
|
|
|
+ #### GET REQUEST FOR GET RESULTS
|
|
|
else:
|
|
|
try:
|
|
|
- cursor.execute('Select UUID, reference_pdf_name from reference_file')
|
|
|
+ cursor.execute('Select UUID, reference_pdf_name, description from reference_file')
|
|
|
db_data = [i for i in cursor.fetchall()]
|
|
|
print(db_data)
|
|
|
cursor.close()
|
|
@@ -72,7 +83,7 @@ def upload_reference_file():
|
|
|
return jsonify(db_data)
|
|
|
except:
|
|
|
return jsonify('')
|
|
|
- return render_template('index.html')
|
|
|
+ # return render_template('index.html')
|
|
|
|
|
|
@app.route('/update_reference_ajax', methods=['GET', 'POST'])
|
|
|
def update_reference_file():
|
|
@@ -82,37 +93,41 @@ def update_reference_file():
|
|
|
print("Connected to SQLite")
|
|
|
|
|
|
query = """ UPDATE reference_file
|
|
|
- SET reference_pdf_name = :name , reference_pdf = :file
|
|
|
+ SET reference_pdf_name = :name , reference_pdf = :file , description = :desc
|
|
|
WHERE UUID = :uuid
|
|
|
"""
|
|
|
+ #### POST REQUEST FOR UPDATE
|
|
|
if request.method == 'POST':
|
|
|
if request.files:
|
|
|
- uuid = request.form['uuid']
|
|
|
- file_ref = request.files['reference']
|
|
|
- files_json = [{'file_ref':file_ref.filename,'uuid':str(uuid)}]
|
|
|
- # print(files_json)
|
|
|
- # print(allowed_file(file_orig.filename))
|
|
|
try:
|
|
|
- if allowed_file(file_ref.filename):
|
|
|
- blob_ref = base64.b64encode(file_ref.read())
|
|
|
- data_tuple = (file_ref.filename,blob_ref,str(uuid))
|
|
|
- data = {'name' : file_ref.filename, 'file' : blob_ref, 'uuid' : str(uuid)}
|
|
|
- cursor.execute(query,data)
|
|
|
- sqliteConnection.commit()
|
|
|
- cursor.close()
|
|
|
- return jsonify(files_json)
|
|
|
- else:
|
|
|
- return Response("All fields must be selected", status=400, mimetype='application/json')
|
|
|
- except Exception as e:
|
|
|
- exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
|
- fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
|
|
|
- print(exc_type, fname, exc_tb.tb_lineno)
|
|
|
- print(e)
|
|
|
- return Response("Error in uploading", status=400, mimetype='application/json')
|
|
|
-
|
|
|
- return render_template('index.html')
|
|
|
-
|
|
|
-
|
|
|
+ uuid = request.form['uuid']
|
|
|
+ desc = request.form['desc']
|
|
|
+ file_ref = request.files['reference']
|
|
|
+ files_json = [{'file_ref':file_ref.filename,'uuid':str(uuid),'desc':desc}]
|
|
|
+ # print(files_json)
|
|
|
+ # print(allowed_file(file_orig.filename))
|
|
|
+ try:
|
|
|
+ if allowed_file(file_ref.filename):
|
|
|
+ blob_ref = base64.b64encode(file_ref.read())
|
|
|
+ data_tuple = (file_ref.filename,blob_ref,str(uuid))
|
|
|
+ data = {'name' : file_ref.filename, 'file' : blob_ref, 'desc' : desc, 'uuid' : str(uuid)}
|
|
|
+ cursor.execute(query,data)
|
|
|
+ sqliteConnection.commit()
|
|
|
+ cursor.close()
|
|
|
+ return jsonify(files_json)
|
|
|
+ else:
|
|
|
+ return Response("All fields must be selected", status=400, mimetype='application/json')
|
|
|
+ except Exception as e:
|
|
|
+ exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
|
+ fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
|
|
|
+ print(exc_type, fname, exc_tb.tb_lineno)
|
|
|
+ print(e)
|
|
|
+ return Response("Error in uploading", status=400, mimetype='application/json')
|
|
|
+ except:
|
|
|
+ return Response("All fields must be selected", status=400, mimetype='application/json')
|
|
|
+ else:
|
|
|
+ return Response("Bad request", status=400, mimetype='application/json')
|
|
|
+ # return render_template('index.html')
|
|
|
|
|
|
@app.route('/upload_original_ajax', methods=['GET', 'POST'])
|
|
|
def upload_original_file():
|
|
@@ -123,32 +138,37 @@ def upload_original_file():
|
|
|
cursor.execute('''CREATE TABLE IF NOT EXISTS original_file (
|
|
|
UUID NOT NULL,
|
|
|
original_pdf_name NOT NULL,
|
|
|
- original_pdf text NOT NULL
|
|
|
+ original_pdf text NOT NULL,
|
|
|
+ description text NOT NULL,
|
|
|
+ reference_uuid NOT NULL
|
|
|
);''')
|
|
|
query = """ INSERT INTO original_file
|
|
|
- (UUID,original_pdf_name,original_pdf) VALUES (?,?,?)"""
|
|
|
+ (UUID,original_pdf_name,original_pdf, description, reference_uuid) VALUES (?,?,?,?,?)"""
|
|
|
if request.method == 'POST':
|
|
|
if request.files:
|
|
|
- file_orig = request.files['original']
|
|
|
- files_json = [{'file_orig':file_orig.filename,'uuid':str(uu_id.int)}]
|
|
|
- print(files_json)
|
|
|
- # print(allowed_file(file_orig.filename))
|
|
|
try:
|
|
|
+ desc = request.form['description']
|
|
|
+ ref_uuid = request.form['reference_uuid']
|
|
|
+ file_orig = request.files['original']
|
|
|
+ files_json = [{'file_orig':file_orig.filename,'uuid':str(uu_id.int), 'desc':desc, 'ref_uuid':ref_uuid}]
|
|
|
+ print(files_json)
|
|
|
+ # print(allowed_file(file_orig.filename))
|
|
|
+ # try:
|
|
|
if allowed_file(file_orig.filename):
|
|
|
blob_orig = base64.b64encode(file_orig.read())
|
|
|
- data_tuple = (str(uu_id.int),file_orig.filename,blob_orig)
|
|
|
+ data_tuple = (str(uu_id.int),file_orig.filename,blob_orig, desc, ref_uuid)
|
|
|
cursor.execute(query,data_tuple)
|
|
|
sqliteConnection.commit()
|
|
|
cursor.close()
|
|
|
return jsonify(files_json)
|
|
|
else:
|
|
|
- return Response("All fields must be selected", status=400, mimetype='application/json')
|
|
|
+ return Response("All fields must be selected", status=422, mimetype='application/json')
|
|
|
except Exception as e:
|
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
|
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
|
|
|
print(exc_type, fname, exc_tb.tb_lineno)
|
|
|
print(e)
|
|
|
- return Response("Error in uploading", status=400, mimetype='application/json')
|
|
|
+ return Response("All fields must be selected", status=422, mimetype='application/json')
|
|
|
else:
|
|
|
uuid_value = request.json['uuid']
|
|
|
print(str(uuid_value))
|
|
@@ -156,9 +176,10 @@ def upload_original_file():
|
|
|
print(sql)
|
|
|
cursor.execute(sql)
|
|
|
sqliteConnection.commit()
|
|
|
+ return jsonify('')
|
|
|
else:
|
|
|
try:
|
|
|
- cursor.execute('Select UUID, original_pdf_name from original_file')
|
|
|
+ cursor.execute('Select UUID, original_pdf_name, description, reference_uuid from original_file')
|
|
|
db_data = [i for i in cursor.fetchall()]
|
|
|
print(db_data)
|
|
|
cursor.close()
|
|
@@ -166,17 +187,13 @@ def upload_original_file():
|
|
|
return jsonify(db_data)
|
|
|
except:
|
|
|
return jsonify('')
|
|
|
- return render_template('index.html')
|
|
|
+ # return render_template('index.html')
|
|
|
|
|
|
-
|
|
|
-@app.route('/')
|
|
|
-def index():
|
|
|
- return render_template('index.html')
|
|
|
-# /<uuid1, uuid2>
|
|
|
@app.route('/comparison',methods=['POST', 'GET'])
|
|
|
def comparison_():
|
|
|
if request.method == 'GET':
|
|
|
try:
|
|
|
+ root_dir = Path.cwd()
|
|
|
uuid1 = request.args.get('uuid1', None)
|
|
|
uuid2 = request.args.get('uuid2', None)
|
|
|
print(uuid1,uuid2)
|
|
@@ -189,20 +206,24 @@ def comparison_():
|
|
|
blob = cursor.fetchone()
|
|
|
blob_orig = base64.b64decode(blob[0])
|
|
|
|
|
|
+ with open(root_dir.joinpath('temp', 'temp_orig.pdf'), 'wb') as f:
|
|
|
+ f.write(blob_orig)
|
|
|
+
|
|
|
ref_sql = 'Select reference_pdf from reference_file where UUID = "{}"'.format(str(uuid2))
|
|
|
cursor.execute(ref_sql)
|
|
|
blob = cursor.fetchone()
|
|
|
blob_ref = base64.b64decode(blob[0])
|
|
|
- with open('temp/temp_orig.pdf', 'wb') as f:
|
|
|
- f.write(blob_orig)
|
|
|
- with open('temp/temp_ref.pdf', 'wb') as f:
|
|
|
+
|
|
|
+ with open(root_dir.joinpath('temp', 'temp_ref.pdf'), 'wb') as f:
|
|
|
f.write(blob_ref)
|
|
|
|
|
|
- input_file1 = 'temp/temp_orig.pdf'
|
|
|
- input_file2 = 'temp/temp_ref.pdf'
|
|
|
|
|
|
- output_file1 = 'output/Original_file.pdf'
|
|
|
- output_file2 = 'output/Reference_file.pdf'
|
|
|
+ input_file1 = root_dir.joinpath('temp', 'temp_orig.pdf')
|
|
|
+ input_file2 = root_dir.joinpath('temp', 'temp_ref.pdf')
|
|
|
+
|
|
|
+ output_file1 = Path() / 'output' / 'Original_file.pdf'
|
|
|
+ output_file2 = Path() / 'output' / 'Reference_file.pdf'
|
|
|
+
|
|
|
print('Comparing files ', input_file1, ' and ', input_file2, '.....')
|
|
|
|
|
|
fullText1 = ""
|
|
@@ -381,7 +402,7 @@ def comparison_():
|
|
|
except:
|
|
|
pass
|
|
|
doc2.save(output_file2, garbage=4, deflate=True, clean=True)
|
|
|
- zipObj = ZipFile('output/output.zip', 'w')
|
|
|
+ zipObj = ZipFile(root_dir.joinpath('output', 'output.zip'), 'w')
|
|
|
zipObj.write(output_file1)
|
|
|
zipObj.write(output_file2)
|
|
|
zipObj.close()
|
|
@@ -390,11 +411,19 @@ def comparison_():
|
|
|
|
|
|
print('error in comparison')
|
|
|
print(e)
|
|
|
- return redirect(url_for('.index',message = 'error in comparison'))
|
|
|
+ return Response("Error in Comparison", status=400, mimetype='application/json')
|
|
|
|
|
|
|
|
|
- return send_file('output\\output.zip', as_attachment=True)
|
|
|
+ return send_file(root_dir.joinpath('output', 'output.zip'), as_attachment=True)
|
|
|
else:
|
|
|
- return redirect(url_for('.index',message = 'Request type not matched.'))
|
|
|
+ print('sss')
|
|
|
+ return Response("Bad Request", status=400, mimetype='application/json')
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+@app.route('/')
|
|
|
+def index():
|
|
|
+ return render_template('default.html')
|
|
|
+
|
|
|
if __name__ == "__main__":
|
|
|
app.run()
|