-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
89 lines (62 loc) · 2.29 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
from flask import Flask, jsonify, request
from flask_cors import CORS
from pyenc.encryptor import Encryptor
import os, os.path
from driveapi import auth
from uploadFile import upload_file_to_drive
from downloadFile import download_file_from_drive
from fileSearch import search_files_by_name
from allFileList import list_files
app = Flask(__name__)
CORS(app)
name_list = []
encryptor = Encryptor()
def update_name_list():
return list_files()
@app.route('/login', methods=['GET'])
def login():
auth()
return "login"
@app.route('/upload', methods=['POST'])
def upload_file():
if 'image' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['image']
upload_dir = './static/uploaded'
if not os.path.exists(upload_dir):
os.makedirs(upload_dir)
file_path = os.path.join(upload_dir, file.filename)
file.save(file_path)
keyVal = encryptor.key_create()
encryptor.key_write(keyVal, './mykey.txt')
loaded_key = encryptor.key_load('./mykey.txt')
encryptor.file_encrypt(loaded_key, file_path, f'./static/encryptedLocal/{file.filename}.enc')
upload_file_to_drive(f'./static/encryptedLocal/{file.filename}.enc', f'{file.filename}.enc')
return jsonify({'Result': 'File Uploaded Successfully'})
@app.route('/api/data', methods=['GET', 'POST'])
def get_data():
name_list = update_name_list()
data = {}
data.update({"name": name_list})
return data
@app.route('/send', methods=['POST','GET'])
def send_data():
data = request.json
filename = data.get('filename')
fileName = filename.split('.')[0]+'.'+filename.split('.')[-2]
file_id = search_files_by_name(f'{filename}')
download_file_from_drive(file_id, f'./static/encryptedDrive/enc_{fileName}')
key_chk = data.get('input')
print(key_chk)
if len(key_chk) == 44:
try:
encryptor.file_decrypt(key_chk, f'./static/encryptedDrive/enc_{fileName}', f'./static/decrypted/dec_{fileName}')
if os.path.exists(f'./static/decrypted/dec_{fileName}') == True:
print('Correct Key!')
except:
print('Incorrect Key!')
else:
print('Incorrect Key!')
return jsonify({'Result': 'Success'})
if __name__ == '__main__':
app.run(debug=True, port=8080)