Skip to content

Commit

Permalink
better uploads handling, no need of code anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaaoBlues committed Feb 16, 2023
1 parent a7b6ac4 commit 560b3c9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
16 changes: 8 additions & 8 deletions client.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import requests
from util import *

def send_text_scan(text:str,ip_addr:str,upload_code:str) -> bool:

def send_text_scan(text:str,ip_addr:str) -> bool:
"""
sends the specified text
to the instance of copypasta running on the specified address
"""
r = requests.post(f"http://{ip_addr}:21987/upload",json={"type" : "text", "content" : f"{text}"})

return r.status_code == 200


def send_file(file_path:str,ip_addr:str,upload_code:str):
def send_file(file_path:str,ip_addr:str):

"""
sends the specified file
Expand All @@ -23,24 +23,24 @@ def send_file(file_path:str,ip_addr:str,upload_code:str):
r = requests.post(f"http://{ip_addr}:21987/upload",files=files)
return r.status_code == 200

def send_keystrokes(text:str,upload_code:str):
def send_keystrokes(text:str):
r = requests.post("http://127.0.0.1:21987/upload",json={"type" : "keystrokes", "content" : {"text" : f"{text}"}})
return r.status_code == 200

def send_barcode(text:str,upload_code:str):
def send_barcode(text:str):
r = requests.post("http://127.0.0.1:21987/upload",json={"type" : "isbn", "content" : f"{text}"})
return r.status_code == 200


def send_wifi(ssid:str, encryption:str, key:str,upload_code:str):
def send_wifi(ssid:str, encryption:str, key:str):
r = requests.post("http://127.0.0.1:21987/upload",json={"type" : "wifi", "content" : {"ssid" : f"{ssid}", "encryption" : f"{encryption}", "key" : f"{key}"}})
return r.status_code == 200


def send_email(dest_addr:str,subject:str,content:str,upload_code:str):
def send_email(dest_addr:str,subject:str,content:str):
r = requests.post("http://127.0.0.1:21987/upload",json={"type" : "email", "content" : {"address" : f"{dest_addr}", "subject" : f"{subject}", "content" : f"{content}"}})
return r.status_code == 200

def send_url(url:str,upload_code:str):
def send_url(url:str):
r = requests.post("http://127.0.0.1:21987/upload",json={"type" : "url", "content" : f"{url}"})
return r.status_code == 200
39 changes: 29 additions & 10 deletions copypasta.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,16 @@ def home():
init_history_file()

#render the html with the history
return render_template("index.html",copypasta_url=COPYPASTA_URL,server_version=get_server_version(),hist = get_history_json(),ip=get_private_ip(),hostname=socket.gethostname(),tab=path.exists("static/tab"))
return render_template("index.html",
copypasta_url=COPYPASTA_URL,
server_version=get_server_version(),
hist = get_history_json(),
ip=get_private_ip(),
hostname=socket.gethostname(),
tab=path.exists("static/tab"),
is_accepting_uploads = is_accepting_uploads()
)



else:
Expand Down Expand Up @@ -355,6 +364,18 @@ def copy_last_scan(json_data:dict):
notify_desktop("CopyPasta","scan copied to clipboard :D")


#set the current uploads accepting state
@socketio.on("[CHANGE_ACCEPTING_UPLOADS_STATE]")
def change_accepting_uploads(json_data:dict):

change_accepting_uploads_state()

if is_accepting_uploads():
socketio.emit("[NOTIFY_USER]",{"msg":"CopyPasta is now accepting incoming files !"})

else:
socketio.emit("[NOTIFY_USER]",{"msg":"CopyPasta is now refusing incoming files !"})




Expand Down Expand Up @@ -528,6 +549,7 @@ def api(api_req):
# return url to javascript for qr code generation and notification
safe_arg = url_encode(url_encode(path.basename(file_path))) # twice because google charts api will transform it once before qr generation
return f"http://{get_private_ip()}:21987/download/main_page?file={safe_arg}"

else:
return jsonify({"Error" : "wrong api call"})

Expand Down Expand Up @@ -574,17 +596,16 @@ def delete_ot_dl(response):


@app.route("/upload",methods=["POST"])

def upload():

if request.method == "POST":


r = request.get_json(silent=True)
time = date.today().strftime("%d/%m/%Y")

# check upload mode
if not is_upload_mode_activated(APP_PATH):
if not is_accepting_uploads():
socketio.emit("[NOTIFY_USER]",{"msg":"Someone is trying to upload to your computer !<br> If this is you, you must turn on uploads (the switch right under) ;)"})
notify_desktop("An upload request has been detected !","Someone is trying to upload to your computer ! If it is you, don't forget to activate uploads from the web app :)")
return jsonify({"Error":"Upload mode is disapled."}),403

notify_desktop("New scan Incoming !", "Click to open CopyPasta")
Expand Down Expand Up @@ -777,10 +798,9 @@ def client():
try:
scan_ctt = request.form.get("text_content",type=str)
except ValueError:
return render_template("send_client.html",msg="ValueError")
return render_template("send_client.html",msg="erreur, champs non remplis")

if not pc_client.send_text_scan(scan_ctt,ip_addr):
msg = "/!\ Client is not accepting uploads or is offline /!\\"
Process(target = pc_client.send_text_scan,args = (scan_ctt,ip_addr)).start()



Expand Down Expand Up @@ -808,8 +828,7 @@ def client():
# save the file into it
file.save(f"tmp/{filename}")

if not pc_client.send_file(f"tmp/{filename}",ip_addr):
msg = " /!\ Client is not accepting uploads or is offline /!\\"
Process(target = pc_client.send_file, args = (f"tmp/{filename}",ip_addr)).start()


# clear temporary file in 10s, but we don't sleep on main thread ^^
Expand Down
23 changes: 22 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,16 @@ <h6>

<h1>Last files/data sent :</h1>
<hr/>
<button class="btn btn-danger mb-3" id="delete_all_history_button" onclick="send_event('[DEL_HISTORY]',{data : ''})">Delete all history <i class="bi bi-trash-fill fs-5"></i></button>

<div style="display:flex;">
<button class="btn btn-danger mb-3" id="delete_all_history_button" onclick="send_event('[DEL_HISTORY]',{data : ''})">Delete all history <i class="bi bi-trash-fill fs-5"></i></button>
<div class="form-check form-switch mt-2 mx-3">
<label class="form-check-label" for="uploads_state">Accept incoming uploads (activate this to recieve things)</label>
<input class="form-check-input" type="checkbox" id="uploads_state" onclick="change_accepting_uploads_state()"
{% if is_accepting_uploads%}checked{%endif%}
/>
</div>
</div>

<table class="table table-striped text-center" id="history_table">
<thead>
Expand Down Expand Up @@ -170,6 +179,18 @@ <h1>Last files/data sent :</h1>
}


function change_accepting_uploads_state(){
send_event('[CHANGE_ACCEPTING_UPLOADS_STATE]',{data:''});

if( document.getElementById("uploads_state").checked){
document.getElementById("uploads_state").removeAttribute("checked");
}else{
document.getElementById("uploads_state").setAttribute("checked","true");
}
}




function send_text_to_phone(){
let text = prompt('Type/paste your text/url here (max 4000 characters)');
Expand Down
10 changes: 1 addition & 9 deletions templates/send_client.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title translate="no">CopyPasta/Video</title>
<title translate="no">CopyPasta/Client</title>
<link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" integrity="sha384-eoTu3+HydHRBIjnCVwsFyCpUDZHZSFKEJD0mc3ZqSBSb6YhZzRHeiomAUWCstIWo" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
Expand Down Expand Up @@ -53,10 +53,6 @@ <h5>What do you want to send ?</h5>
<h3>Send some text to another pc !</h3>
<input type="text" class="ip_addr_input" id="ip_addr_input" name="pc_ip_addr" class="input" placeholder="Other pc's IP address"/>
<br>
<input type="text" class="upload_code_input" id="upload_code_input" name="upload_code" class="input" placeholder="Other pc's upload code"/>
<br>
<label for="upload_code_input" style="margin-bottom: 2%;">This code is available on the home CopyPasta page of the other pc. Same for the IP address</label>
<br>
<textarea name="text_content" class="mt-2 text_content" type="text" placeholder="Type your text here !"></textarea>
<input name="type" type="text" value="text" hidden/>
<br>
Expand All @@ -69,10 +65,6 @@ <h3>Send some text to another pc !</h3>
<h3>Send a file to another pc !</h3>
<input type="text" class="ip_addr_input" id="ip_addr_input" name="pc_ip_addr" class="input" placeholder="Other pc's IP address"/>
<br>
<input type="text" class="upload_code_input" id="upload_code_input" name="upload_code" class="input" placeholder="Other pc's upload code"/>
<br>
<label for="upload_code_input" style="margin-bottom: 2%;">This code is available on the home CopyPasta page of the other pc. Same for the IP address</label>
<br>
<input class="mt-2" type="file" placeholder="Select the file here" name="files_input" id="file_input"/>
<input name="type" type="text" value="file" hidden/>
<br>
Expand Down
19 changes: 18 additions & 1 deletion util.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def init_config_file():
with open("static/config.json","w") as f:
f.write(dumps(
{
"accepting_uploads":False,
"accepting_uploads":True,
"disable_updates":False,
"open_since_last_check":1
}
Expand All @@ -358,3 +358,20 @@ def is_accepting_uploads() -> bool:
config = loads(f.read())

return config["accepting_uploads"]

def change_accepting_uploads_state():
"""
"""

config = {}

with open("static/config.json","r") as f:

config = loads(f.read())

with open("static/config.json","w") as f:

config["accepting_uploads"] = not config["accepting_uploads"]

f.write(dumps(config))

0 comments on commit 560b3c9

Please sign in to comment.