Skip to content

Commit

Permalink
fixed offline support and false notification
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaaoBlues committed May 24, 2022
1 parent ea5712e commit 33d1bd0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
5 changes: 2 additions & 3 deletions copypasta.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def api(api_req):


if not is_online():
return jsonify({"error","you are currently offline"})
return "You are offline :/"

#create a qr code containing the ip with google chart api
r = get("https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl="+make_qr_url(),allow_redirects=True)
Expand Down Expand Up @@ -543,8 +543,6 @@ def upload():
full_path = path.join(app.config['UPLOAD_FOLDER'],"files_hist", path.splitext(filename)[0]+str(i)+"."+filename.split(".")[-1])
i += 1

print(full_path)

file.save(full_path)
store_to_history({"file_name" : f"{file.filename}","file_type" : f"{file_type}","date" : f"{time}","path" : f"{full_path}"})

Expand All @@ -568,6 +566,7 @@ def upload():

if not is_server_already_running():


if is_online():
#create a qr code containing the ip with google chart api
r = get("https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl="+make_qr_url(),allow_redirects=True)
Expand Down
14 changes: 9 additions & 5 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ <h1>Last files/data sent :</h1>
var n_elements = 0;
var is_online = true;

function fill_history_tab(){
function fill_history_tab(init=false){

var xmlHttp = new XMLHttpRequest();

Expand Down Expand Up @@ -283,11 +283,14 @@ <h1>Last files/data sent :</h1>
}

var ele_l = elements.length;

if(n_elements< ele_l || n_elements > ele_l){

//don't notify user at app startup as it's only already sent content
if(!init){
notify("New element received !");
document.getElementById("history_table_body").innerHTML = all;
n_elements = elements.length;
}
document.getElementById("history_table_body").innerHTML = all;
n_elements = elements.length;
}


Expand All @@ -296,6 +299,7 @@ <h1>Last files/data sent :</h1>

if(!isOnline() && is_online){
notify("network change detected !")
request("http://127.0.0.1:21987/api/update_ip");
is_online = false;
}

Expand Down Expand Up @@ -362,7 +366,7 @@ <h1>Last files/data sent :</h1>



window.onload = fill_history_tab();
window.onload = fill_history_tab(init=true);



Expand Down
19 changes: 13 additions & 6 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ def get_private_ip():
"""

try:

# check if online before, because some things I don't fully understand made
# this request return the last private IP address sometimes when onffline
if is_online():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
except:
# probably offline
return "127.0.0.1"

else:
return "You are offline :/"


def make_qr_url():
Expand Down Expand Up @@ -255,5 +258,9 @@ def create_shortcut(path, target='', wDir='', icon=''):


def is_online():

return get_private_ip() != "127.0.0.1"
try:
socket.create_connection(("8.8.8.8",53))
return True
except OSError:
return False

0 comments on commit 33d1bd0

Please sign in to comment.