mirror of
https://github.com/AmanTahiliani/PeerNotes.git
synced 2026-08-07 11:55:12 -04:00
Merge pull request #19 from AmanTahiliani/aman-fix-cors
Fixed flask cors and added linting
This commit is contained in:
@@ -68,7 +68,7 @@ class PollOnlineView(APIView):
|
|||||||
user = request.user
|
user = request.user
|
||||||
try:
|
try:
|
||||||
data = request.data
|
data = request.data
|
||||||
ip_address = data['ip']
|
ip_address = data["ip"]
|
||||||
print("Local IP found in request")
|
print("Local IP found in request")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
ip_address = get_client_ip(request)
|
ip_address = get_client_ip(request)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def update_user_ip(request):
|
|||||||
user = request.user
|
user = request.user
|
||||||
try:
|
try:
|
||||||
data = request.data
|
data = request.data
|
||||||
ip_address = data['ip']
|
ip_address = data["ip"]
|
||||||
print("Local IP found in request")
|
print("Local IP found in request")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
ip_address = get_client_ip(request)
|
ip_address = get_client_ip(request)
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ ALLOWED_HOSTS = ["*"]
|
|||||||
# ALLOWED_HOSTS = []
|
# ALLOWED_HOSTS = []
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
|
|||||||
@@ -19,3 +19,5 @@ tomli==2.0.1
|
|||||||
typing_extensions==4.10.0
|
typing_extensions==4.10.0
|
||||||
Werkzeug==3.0.1
|
Werkzeug==3.0.1
|
||||||
zipp==3.17.0
|
zipp==3.17.0
|
||||||
|
flask
|
||||||
|
flask-cors
|
||||||
@@ -4,108 +4,116 @@ import socket
|
|||||||
import shutil
|
import shutil
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from flask_cors import CORS
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Enable CORS for all routes
|
||||||
|
CORS(app)
|
||||||
|
|
||||||
@app.route('/copy-file', methods = ['POST'])
|
|
||||||
|
@app.route("/copy-file", methods=["POST"])
|
||||||
def copy_file():
|
def copy_file():
|
||||||
try:
|
try:
|
||||||
data = request.json
|
data = request.json
|
||||||
source_file = data['file_path']
|
source_file = data["file_path"]
|
||||||
id = data['file_id']
|
id = data["file_id"]
|
||||||
if not os.path.exists(source_file):
|
if not os.path.exists(source_file):
|
||||||
return f"Source file does not exist at {source_file}", 400
|
return f"Source file does not exist at {source_file}", 400
|
||||||
filename = os.path.basename(source_file)
|
filename = os.path.basename(source_file)
|
||||||
destination_path = os.path.join('./uploads/', str(id) +'_' + filename)
|
destination_path = os.path.join("./uploads/", str(id) + "_" + filename)
|
||||||
shutil.copyfile(source_file, destination_path)
|
shutil.copyfile(source_file, destination_path)
|
||||||
return "File Moved successfully", 200
|
return "File Moved successfully", 200
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'An error occured: {e}')
|
print(f"An error occured: {e}")
|
||||||
return "An error occured", 400
|
return "An error occured", 400
|
||||||
|
|
||||||
|
|
||||||
@app.route('/ip', methods=["GET"])
|
@app.route("/ip", methods=["GET"])
|
||||||
def get_internal_ip():
|
def get_internal_ip():
|
||||||
internal_ip = None
|
internal_ip = None
|
||||||
try:
|
try:
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
s.connect(("8.8.8.8", 80))
|
s.connect(("8.8.8.8", 80))
|
||||||
internal_ip = s.getsockname()[0]
|
internal_ip = s.getsockname()[0]
|
||||||
s.close()
|
s.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error:", e)
|
print("Error:", e)
|
||||||
return internal_ip, 200
|
return internal_ip, 200
|
||||||
|
|
||||||
@app.route('/send', methods=['GET'])
|
|
||||||
|
@app.route("/send", methods=["GET"])
|
||||||
def send():
|
def send():
|
||||||
file_id = request.args.get('id')
|
file_id = request.args.get("id")
|
||||||
filename = request.args.get('filename')
|
filename = request.args.get("filename")
|
||||||
|
|
||||||
file_path = './uploads/' + file_id + '_' + 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'])
|
return send_file(
|
||||||
|
file_path, as_attachment=True, download_name=str(file_id) + "_" + filename
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/request", methods=["GET"])
|
||||||
def request_file():
|
def request_file():
|
||||||
data = request.json
|
data = request.json
|
||||||
file_id = data['id']
|
file_id = data["id"]
|
||||||
filename = data['filename']
|
filename = data["filename"]
|
||||||
ip = data['ip']
|
ip = data["ip"]
|
||||||
|
|
||||||
|
|
||||||
url = f"http://{ip}:8080/send"
|
url = f"http://{ip}:8080/send"
|
||||||
params = {
|
params = {
|
||||||
'id': file_id,
|
"id": file_id,
|
||||||
'filename': filename,
|
"filename": filename,
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.get(url, params=params)
|
response = requests.get(url, params=params)
|
||||||
file_path = './uploads/' + str(file_id) + '_' + filename
|
file_path = "./uploads/" + str(file_id) + "_" + filename
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
if os.path.exists(file_path):
|
if os.path.exists(file_path):
|
||||||
return "File with name already exists", 200
|
return "File with name already exists", 200
|
||||||
|
|
||||||
with open(file_path, 'wb') as f:
|
with open(file_path, "wb") as f:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
print("File downloaded successfully")
|
print("File downloaded successfully")
|
||||||
return f'File Downloaded to location to location {file_path}', 200
|
return f"File Downloaded to location to location {file_path}", 200
|
||||||
else:
|
else:
|
||||||
error = response.text
|
error = response.text
|
||||||
print("Error:", error)
|
print("Error:", error)
|
||||||
return error, response.status_code
|
return error, response.status_code
|
||||||
|
|
||||||
@app.route('/request-tests', methods=['GET'])
|
|
||||||
|
@app.route("/request-tests", methods=["GET"])
|
||||||
def request_test_():
|
def request_test_():
|
||||||
data = request.json
|
data = request.json
|
||||||
file_id = data['id']
|
file_id = data["id"]
|
||||||
filename = data['filename']
|
filename = data["filename"]
|
||||||
ip = data['ip']
|
ip = data["ip"]
|
||||||
new_filename = data['new_filename']
|
new_filename = data["new_filename"]
|
||||||
|
|
||||||
|
|
||||||
url = f"http://{ip}:8080/send"
|
url = f"http://{ip}:8080/send"
|
||||||
params = {
|
params = {
|
||||||
'id': file_id,
|
"id": file_id,
|
||||||
'filename': filename,
|
"filename": filename,
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.get(url, params=params)
|
response = requests.get(url, params=params)
|
||||||
file_path = './uploads/' + str(file_id) + '_' + new_filename
|
file_path = "./uploads/" + str(file_id) + "_" + new_filename
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
if os.path.exists(file_path):
|
if os.path.exists(file_path):
|
||||||
return "File with name already exists", 200
|
return "File with name already exists", 200
|
||||||
|
|
||||||
with open(file_path, 'wb') as f:
|
with open(file_path, "wb") as f:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
print("File downloaded successfully")
|
print("File downloaded successfully")
|
||||||
return f'File Downloaded to location to location {file_path}', 200
|
return f"File Downloaded to location to location {file_path}", 200
|
||||||
else:
|
else:
|
||||||
error = response.text
|
error = response.text
|
||||||
print("Error:", error)
|
print("Error:", error)
|
||||||
return error, response.status_code
|
return error, response.status_code
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run(debug=True, host= '0.0.0.0', port=8080)
|
app.run(debug=True, host="0.0.0.0", port=8080)
|
||||||
|
|||||||
Reference in New Issue
Block a user