Files
FileCodeBox/core/utils.py
T

59 lines
1.1 KiB
Python
Raw Normal View History

2023-08-14 01:56:46 +08:00
# @Time : 2023/8/13 19:54
# @Author : Lan
# @File : utils.py
# @Software: PyCharm
2023-08-14 05:03:07 +08:00
import datetime
2023-08-24 14:04:00 +08:00
import hashlib
2023-08-14 01:56:46 +08:00
import random
import string
2023-08-24 14:04:00 +08:00
import time
2023-08-14 01:56:46 +08:00
2023-08-14 13:30:10 +08:00
from apps.base.depends import IPRateLimit
2023-08-14 01:56:46 +08:00
async def get_random_num():
"""
获取随机数
:return:
"""
return random.randint(10000, 99999)
r_s = string.ascii_uppercase + string.digits
async def get_random_string():
"""
获取随机字符串
:return:
"""
return ''.join(random.choice(r_s) for _ in range(5))
2023-08-14 05:03:07 +08:00
async def get_now():
"""
获取当前时间
:return:
"""
return datetime.datetime.now(
datetime.timezone(datetime.timedelta(hours=8))
)
2023-08-24 14:04:00 +08:00
2023-09-06 11:06:18 +08:00
async def get_select_token(code: str):
2023-08-24 14:04:00 +08:00
"""
获取下载token
:param code:
:return:
"""
token = "123456"
return hashlib.sha256(f"{code}{int(time.time() / 1000)}000{token}".encode()).hexdigest()
2023-09-06 11:06:18 +08:00
async def get_file_url(code: str):
2023-08-24 14:04:00 +08:00
"""
对于需要通过服务器中转下载的服务,获取文件下载地址
:param code:
:return:
"""
2023-09-06 11:06:18 +08:00
return f'/share/download?key={await get_select_token(code)}&code={code}'