added padding
This commit is contained in:
12
db.py
12
db.py
@@ -4,6 +4,8 @@ from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
from sqlalchemy import select
|
||||
|
||||
PADDING = 5
|
||||
|
||||
engine = create_engine("sqlite:///example.db")
|
||||
session = Session(bind=engine)
|
||||
|
||||
@@ -22,19 +24,19 @@ class File(Base):
|
||||
def __repr__(self):
|
||||
return f"<File(id={self.id}, extension='{self.extension}', hash='{self.hash}')>"
|
||||
|
||||
def to_base36(n: int) -> str:
|
||||
def to_base36(n: int, width: int) -> str:
|
||||
if n < 0:
|
||||
raise ValueError("Only non-negative integers supported")
|
||||
|
||||
chars = "0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
if n == 0:
|
||||
return "0"
|
||||
return "0".rjust(width, "0")
|
||||
|
||||
result = []
|
||||
while n > 0:
|
||||
n, rem = divmod(n, 36)
|
||||
result.append(chars[rem])
|
||||
return "".join(reversed(result))
|
||||
return "".join(reversed(result)).rjust(width, "0")
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
@@ -55,7 +57,7 @@ def file_exists(size: int, hash_value: str) -> bool:
|
||||
if existed_file is None:
|
||||
return None
|
||||
|
||||
url = f"{to_base36(existed_file.id)}"
|
||||
url = f"{to_base36(existed_file.id, PADDING)}"
|
||||
if existed_file.extension:
|
||||
url += f".{existed_file.extension}"
|
||||
|
||||
@@ -75,7 +77,7 @@ def add_file(filename: str, content_type, size: int, hash):
|
||||
new_file.hash = hash
|
||||
db.add(new_file)
|
||||
db.commit()
|
||||
url = f"{to_base36(new_file.id)}"
|
||||
url = f"{to_base36(new_file.id, PADDING)}"
|
||||
if new_file.extension:
|
||||
url += f".{new_file.extension}"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user