mirror of
https://github.com/AmanTahiliani/PeerNotes.git
synced 2026-08-07 11:55:12 -04:00
Initial Peer service code
This commit is contained in:
54
file_peer/peer_service.py
Normal file
54
file_peer/peer_service.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from flask import Flask, send_file, request
|
||||
import requests
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route('/send', methods=['GET'])
|
||||
def send():
|
||||
file_id = request.args.get('id')
|
||||
filename = request.args.get('filename')
|
||||
|
||||
file_path = './uploads/' + file_id + '_' + filename
|
||||
|
||||
return send_file(file_path, as_attachment=True, download_name = str(file_id)+'_'+filename)
|
||||
|
||||
@app.route('/request', methods=['GET'])
|
||||
def request_file():
|
||||
data = request.json
|
||||
file_id = data['id']
|
||||
filename = data['filename']
|
||||
ip = data['ip']
|
||||
|
||||
|
||||
url = f"http://{ip}:8080/send"
|
||||
params = {
|
||||
'id': file_id,
|
||||
'filename': filename,
|
||||
}
|
||||
|
||||
response = requests.get(url, params=params)
|
||||
file_path = './uploads/' + file_id + '_' + filename
|
||||
|
||||
if response.status_code == 200:
|
||||
if os.path.exists(file_path):
|
||||
# If it does, generate a new filename
|
||||
i = 1
|
||||
beg, end = file_path.split('.')
|
||||
while os.path.exists(f'{beg}_{i}.{end}'):
|
||||
i += 1
|
||||
file_path = f'{beg}_{i}.{end}'
|
||||
print("File with name already exists")
|
||||
|
||||
with open(file_path, 'wb') as f:
|
||||
f.write(response.content)
|
||||
print("File downloaded successfully")
|
||||
return f'File Downloaded to location to location {file_path}',
|
||||
else:
|
||||
error = response.text
|
||||
print("Error:", error)
|
||||
return error, response.status_code
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, port=8080)
|
||||
2
file_peer/uploads/1_abcd.txt
Normal file
2
file_peer/uploads/1_abcd.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Hello World!
|
||||
Flask Notes Transfer Test
|
||||
Reference in New Issue
Block a user