41 lines
965 B
Python
41 lines
965 B
Python
from datetime import datetime
|
|
import secrets
|
|
import string
|
|
import passlib.hash
|
|
|
|
VALID_TOKEN_CHARS = string.digits + string.ascii_letters
|
|
|
|
|
|
def random_token():
|
|
return "".join(secrets.choice(VALID_TOKEN_CHARS) for i in range(8))
|
|
|
|
|
|
def file_date_human(num):
|
|
return datetime.fromtimestamp(num).strftime("%y-%m-%d")
|
|
|
|
|
|
def file_time_human(num):
|
|
return datetime.fromtimestamp(num).strftime("%y-%m-%d %H:%M")
|
|
|
|
|
|
def file_size_human(num, HTML=True):
|
|
space = " " if HTML else " "
|
|
for x in [space + "B", "KB", "MB", "GB", "TB"]:
|
|
if num < 1024.0:
|
|
if x == space + "B":
|
|
return "%d%s%s" % (num, space, x)
|
|
return "%3.1f%s%s" % (num, space, x)
|
|
num /= 1024.0
|
|
|
|
|
|
def file_size_MB(num):
|
|
return "{:,.2f}".format(num / (1024 * 1024))
|
|
|
|
|
|
def hash_password(password):
|
|
return passlib.hash.argon2.hash(password)
|
|
|
|
|
|
def verify_password(password, hash):
|
|
return passlib.hash.argon2.verify(password, hash)
|