-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
170 lines (137 loc) · 5.18 KB
/
app.py
File metadata and controls
170 lines (137 loc) · 5.18 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from flask import Flask, render_template, request, jsonify
import os
import re
import app_functions
import subprocess
app = Flask(__name__)
RECEIVED_FILES_FOLDER = 'downloads'
ACTIVITY_LOG_FILE = 'activity.log'
# Create required directories on startup
if not os.path.exists(RECEIVED_FILES_FOLDER):
os.makedirs(RECEIVED_FILES_FOLDER)
if not os.path.exists(ACTIVITY_LOG_FILE):
open(ACTIVITY_LOG_FILE, 'w').close()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/send')
def send_page():
return render_template('send.html')
@app.route('/receive')
def receive_page():
return render_template('receive.html')
@app.route('/api/start_receiver', methods=['POST'])
def api_start_receiver():
success = app_functions.start_receiver()
return jsonify({'status': 'listening' if success else 'already_running'})
@app.route('/api/stop_receiver', methods=['POST'])
def api_stop_receiver():
success = app_functions.stop_receiver()
return jsonify({'status': 'stopped' if success else 'not_running'})
@app.route('/api/receiver_status')
def api_receiver_status():
status = 'listening' if app_functions.RECEIVER_PROCESS and app_functions.RECEIVER_PROCESS.poll() is None else 'not_running'
return jsonify({'status': status})
@app.route('/api/send_files', methods=['POST'])
def api_send_files():
ip = request.form.get('ip')
files = request.files.getlist('files')
print(f"Received files for IP: {ip}")
print(f"Received files: {files}")
# Save uploaded files temporarily
temp_files = []
for file in files:
temp_path = os.path.join('temp_uploads', file.filename)
file.save(temp_path)
temp_files.append(temp_path)
# **TEMPORARY CHANGE FOR TESTING:** Add placeholder file paths
# temp_files = ['temp_uploads/LAB_practice.pdf', 'temp_uploads/LAB_practice - Copy.pdf']
# print(f"File paths (for testing): {temp_files}")
results = app_functions.send_files(ip, temp_files)
# Cleanup temp files
for f in temp_files:
os.remove(f)
return jsonify(results)
# @app.route('/api/send_files', methods=['POST'])
# def api_send_files():
# ip = request.form.get('ip')
# if not ip:
# return jsonify([{'error': 'No IP provided'}]), 400
#
# # The key must match what your frontend sends: 'files' (not 'files[]')
# files = request.files.getlist('files')
# print(f"Received files for IP: {ip}")
# print(f"Number of files received: {len(files)}")
# for idx, file in enumerate(files):
# print(f"File {idx}: {file.filename} ({file.content_length} bytes)")
#
# if not files or files[0].filename == '':
# return jsonify([{'error': 'No files uploaded'}]), 400
#
# # Create temp_uploads directory if it doesn't exist
# if not os.path.exists('temp_uploads'):
# os.makedirs('temp_uploads')
#
# # Save uploaded files temporarily
# temp_files = []
# for file in files:
# temp_path = os.path.join('temp_uploads', file.filename)
# file.save(temp_path)
# temp_files.append(temp_path)
# print(f"Saved files to: {temp_files}")
#
# results = []
# # Iterate and send files one by one
# for temp_file in temp_files:
# try:
# # Execute sender.py as a subprocess
# process = subprocess.run(['python', 'sender.py', ip, temp_file], capture_output=True, text=True)
# results.append({
# 'file': os.path.basename(temp_file),
# 'status': 'success' if process.returncode == 0 else 'error',
# 'output': process.stdout.strip(),
# 'error': process.stderr.strip()
# })
# print(f"Sent file: {temp_file}")
# except Exception as e:
# results.append({
# 'file': os.path.basename(temp_file),
# 'status': 'error',
# 'error': str(e)
# })
# print(f"Error sending file {temp_file}: {e}")
#
# # Cleanup temp files
# for f in temp_files:
# try:
# os.remove(f)
# print(f"Cleaned up: {f}")
# except Exception as e:
# print(f"Error cleaning up {f}: {e}")
#
# return jsonify(results)
@app.route('/api/received_files')
def api_received_files():
files = []
if os.path.exists(RECEIVED_FILES_FOLDER):
files = [{'name': f} for f in os.listdir(RECEIVED_FILES_FOLDER)
if os.path.isfile(os.path.join(RECEIVED_FILES_FOLDER, f))]
return jsonify(files)
@app.route('/api/activity_log')
def api_activity_log():
logs = []
if os.path.exists(ACTIVITY_LOG_FILE):
with open(ACTIVITY_LOG_FILE, 'r') as f:
for line in f:
match = re.match(r'\[(.*?)\]\s*(.*)', line.strip())
if match:
logs.append({'time': match.group(1), 'msg': match.group(2)})
return jsonify(logs)
@app.route('/api/clear_activity_log', methods=['POST'])
def api_clear_activity_log():
open(ACTIVITY_LOG_FILE, 'w').close()
return jsonify({'status': 'cleared'})
if __name__ == '__main__':
if not os.path.exists('temp_uploads'):
os.makedirs('temp_uploads')
app.run(host='0.0.0.0', port=5000, debug=True)