-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
132 lines (101 loc) · 3.63 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
from flask import Flask, flash, redirect, render_template, request, send_file
from werkzeug.utils import secure_filename
import decrypter as dec
import divider as dv
import encrypter as enc
import restore as rst
import tools
UPLOAD_FOLDER = './uploads/'
UPLOAD_KEY = './key/'
ALLOWED_EXTENSIONS = set(['pem'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['UPLOAD_KEY'] = UPLOAD_KEY
#port = int(os.getenv('PORT', 8000))
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def start_encryption():
dv.divide()
tools.empty_folder('uploads')
enc.encrypter()
return render_template('success.html')
def start_decryption():
dec.decrypter()
tools.empty_folder('key')
rst.restore()
return render_template('restore_success.html')
@app.route('/return-key')
def return_key():
print("reached")
list_directory = tools.list_dir('key')
filename = './key/' + list_directory[0]
print(filename)
return send_file(filename, attachment_filename="My_Key.pem", as_attachment=True, cache_timeout=0)
@app.route('/return-file/')
def return_file():
list_directory = tools.list_dir('restored_file')
filename = './restored_file/' + list_directory[0]
print("****************************************")
print(list_directory[0])
print("****************************************")
return send_file(filename, attachment_filename=list_directory[0], as_attachment=True, cache_timeout=0)
@app.route('/download/')
def downloads():
return render_template('download.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/upload')
def call_page_upload():
return render_template('upload.html')
@app.route('/home')
def back_home():
tools.empty_folder('key')
tools.empty_folder('restored_file')
return render_template('index.html')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/data', methods=['GET', 'POST'])
def upload_file():
tools.empty_folder('uploads')
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return 'NO FILE SELECTED'
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
return start_encryption()
return 'Invalid File Format !'
@app.route('/download_data', methods=['GET', 'POST'])
def upload_key():
tools.empty_folder('key')
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return 'NO FILE SELECTED'
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_KEY'], file.filename))
return start_decryption()
return 'Invalid File Format !'
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000, debug=True)
# app.run()