-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontroller.py
More file actions
231 lines (180 loc) · 6.96 KB
/
Copy pathcontroller.py
File metadata and controls
231 lines (180 loc) · 6.96 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from flask import Flask, jsonify, request
import client
import os
from datetime import datetime
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# @app.route('/upload', methods=['POST'])
# def upload_file():
# file_path = request.json.get('file_path')
# client.upload_file(file_path)
# return jsonify({"status": "File uploaded successfully"}), 200
# Temporary folder to save files
TEMP_UPLOAD_FOLDER = "temp_uploads"
os.makedirs(TEMP_UPLOAD_FOLDER, exist_ok=True)
@app.route('/upload', methods=['POST'])
def upload_file():
try:
# Check if a file is part of the request
if 'files' in request.files:
uploaded_file = request.files['files']
# Save the file temporarily
temp_path = os.path.join(TEMP_UPLOAD_FOLDER, uploaded_file.filename)
uploaded_file.save(temp_path)
# Simulate processing the file via its temporary path
client.upload_file(temp_path)
# Remove the temporary file
os.remove(temp_path)
return jsonify({"status": "File uploaded successfully", "temp_path": temp_path}), 200
# If no file, check for a file path in JSON data
elif request.json and 'file_path' in request.json:
file_path = request.json.get('file_path')
client.upload_file(file_path)
return jsonify({"status": "File uploaded successfully"}), 200
# If neither file nor path is provided, return an error
else:
return jsonify({"error": "No file or file path provided"}), 400
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/download', methods=['POST'])
def download_file():
data = request.json
cid = data.get('cid')
home_dir = os.path.expanduser('~')
downloads_folder = os.path.join(home_dir, 'Downloads')
# saving file along with timestamp
current_timestamp = datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
filename = f"({current_timestamp}) {data.get('filename')}"
file_path = os.path.join(downloads_folder, filename)
os.makedirs(downloads_folder, exist_ok=True)
result = client.download_file(cid, file_path)
if result['success']:
return jsonify({"status": "success", "message": result['message']}), 200
else:
return jsonify({"status": "failure", "message": result['message']}), 500
@app.route('/peers', methods=['GET'])
def get_all_peers():
peers = client.get_all_peers()
return jsonify(peers), 200
@app.route('/pinned_files', methods=['GET'])
def get_all_pinned_files():
pinned_files = client.get_all_pinned_file()
return jsonify(pinned_files), 200
@app.route('/file_status/<string:cid>', methods=['GET'])
def get_file_status(cid):
file_status = client.get_file_status(cid)
return jsonify(file_status), 200
@app.route('/peer_files/<string:peer_id>', methods=['GET'])
def get_other_peer_file_structure(peer_id):
files = client.get_other_peer_file_structure(peer_id)
return jsonify(files), 200
@app.route('/all_files', methods=['GET'])
def get_all_files():
all_files = client.get_all_file()
return jsonify({"data": all_files}), 200
@app.route('/delete', methods=['POST'])
def delete_file():
data = request.json
cid = data.get('cid')
deletion_status = client.delete_file(cid)
return jsonify({"status": deletion_status}), 200
@app.route('/fav_peers', methods=['GET'])
def get_favorite_peers():
try:
favorite_peers = client.get_my_favorite_peer()
return jsonify({
"status": "success",
"data": favorite_peers
}), 200
except Exception as e:
print(f"Error fetching favorite peers: {e}")
return jsonify({
"status": "error",
"message": "Failed to fetch favorite peers."
}), 500
@app.route('/add_fav_peers', methods=['POST'])
def add_favorite_peer_controller():
try:
request_data = request.get_json()
if not request_data:
return jsonify({
"status": "error",
"message": "Invalid input. JSON payload expected."
}), 400
peer_id = request_data.get('peer_id')
nickname = request_data.get('nickname')
if not peer_id or not nickname:
return jsonify({
"status": "error",
"message": "Both 'peer_id' and 'nickname' are required."
}), 400
updated_favorite_list = client.add_favorite_peer(peer_id, nickname)
return jsonify({
"status": "success",
"data": updated_favorite_list
}), 200
except Exception as e:
print(f"Error adding favorite peer: {e}")
return jsonify({
"status": "error",
"message": "Failed to add favorite peer."
}), 500
@app.route('/rename_fav_peers/<peer_id>', methods=['PUT'])
def change_nickname_controller(peer_id):
try:
request_data = request.get_json()
if not request_data:
return jsonify({
"status": "error",
"message": "Invalid input. JSON payload expected."
}), 400
new_nickname = request_data.get('new_nickname')
if not new_nickname:
return jsonify({
"status": "error",
"message": "The 'new_nickname' field is required."
}), 400
updated_favorite_list = client.change_nickname(peer_id, new_nickname)
return jsonify({
"status": "success",
"data": updated_favorite_list
}), 200
except KeyError:
return jsonify({
"status": "error",
"message": f"No peer found with ID {peer_id}."
}), 404
except Exception as e:
print(f"Error changing nickname: {e}")
return jsonify({
"status": "error",
"message": "Failed to change nickname."
}), 500
@app.route('/remove_fav_peers/<peer_id>', methods=['DELETE'])
def remove_favorite_peer_controller(peer_id):
try:
updated_favorite_list = client.remove_favorite_peer(peer_id)
if peer_id not in updated_favorite_list:
return jsonify({
"status": "success",
"message": f"Peer {peer_id} successfully removed.",
"data": updated_favorite_list
}), 200
else:
return jsonify({
"status": "error",
"message": f"Failed to remove peer {peer_id}. Peer might not exist."
}), 400
except Exception as e:
print(f"Error removing favorite peer: {e}")
return jsonify({
"status": "error",
"message": "Failed to remove favorite peer."
}), 500
@app.route('/dashboard/file-stats', methods=['GET'])
def get_dashboard_stats():
dashboard_data = client.fetch_dashboard_data()
return jsonify({"data": dashboard_data}), 200
if __name__ == '__main__':
app.run(debug=True)