2023-10-16 21:58:50 +02:00
2023-10-18 12:55:20 +02:00
from flask import Flask , redirect , url_for , request , render_template , make_response
2023-10-17 18:25:38 +02:00
from datetime import datetime
2023-10-18 12:55:20 +02:00
from hashlib import sha256
from uuid import uuid4 as uuid
2023-10-17 18:25:38 +02:00
## Import db class from func.py and initialise it
from func import db
db = db ( )
2023-10-18 12:55:20 +02:00
db . startup ( )
2023-10-17 18:25:38 +02:00
2023-10-16 21:58:50 +02:00
app = Flask ( __name__ )
2023-10-17 18:25:38 +02:00
## CUSTOM FILTERS
@app.template_filter ( ' ctime ' )
def timectime ( s ) :
return datetime . utcfromtimestamp ( s ) . strftime ( ' % Y- % m- %d % H: % M ' )
@app.template_filter ( ' spacer ' )
2023-10-18 12:55:20 +02:00
def convsize ( s ) :
2023-10-17 18:25:38 +02:00
sizes = ( " B " , " KB " , " MB " , " GB " , " TB " )
n = 0
while s > 1000 :
n + = 1
s = s / 1000
return str ( " %.2f " % s ) + sizes [ n ]
2023-10-16 21:58:50 +02:00
## WEB FRONTEND
@app.route ( ' / ' )
def homepage ( ) :
2023-10-18 12:55:20 +02:00
# try to get sesskey, else logout state
try :
# get sesskey and get info about user
sesskey = request . cookies . get ( ' session ' )
res , userid = db . check_sesskey ( sesskey )
if not res :
return 500
userdata = db . get_user_info ( userid )
logged_in = True
except Exception as e :
logged_in = False
userdata = ( )
2023-10-17 18:25:38 +02:00
archives = db . get_n_archives ( )
2023-10-18 12:55:20 +02:00
return render_template ( " home.html " , title = " Homepage " , userdata = userdata , login = logged_in , archives = archives )
@app.route ( ' /user ' )
@app.route ( ' /user/<userid> ' )
def userpage ( userid : int = 0 ) :
if userid == 0 :
return make_response ( redirect ( ' / ' ) )
@app.route ( ' /login ' , methods = [ " GET " , " POST " ] )
def loginpage ( ) :
# POST: Process login request
if request . method == ' POST ' :
username = request . form [ ' username ' ]
password = sha256 ( request . form [ ' password ' ] . encode ( ) ) . hexdigest ( )
code , userid , passhash = db . get_passhash ( username )
if code != 200 :
return passhash
# if passwords match, create session and return cookie
if password . upper ( ) == passhash . upper ( ) :
lifetime = 3000000 # lifetime of the sesskey in seconds
sesskey = str ( uuid ( ) )
db . set_sesskey ( sesskey , userid , lifetime )
resp = setcookie ( " session " , sesskey , lifetime )
return resp
else :
return " <h2>You ' ve entered the wrong password. This incident will be reported.</h2><br> Go back and try again.<br> " + password . upper ( ) + " <br> " + passhash . upper ( ) # TODO: DELETE
# GET: Login form
else :
return render_template ( " login.html " , title = " Login " )
## FUNCTIONS
def setcookie ( name : str , value : str , lifetime : int = 10000 ) :
resp = make_response ( redirect ( ' / ' ) )
resp . set_cookie ( name , value , max_age = lifetime )
return resp
2023-10-16 21:58:50 +02:00
## API CALLS
# main driver function
if __name__ == ' __main__ ' :
# run app if executed directly
app . run ( )