create directory automatically

This commit is contained in:
boyska 2024-12-07 19:40:27 +01:00
parent 67527ef585
commit c6c6bd7f0b

View file

@ -70,21 +70,21 @@ async def upload(
proxy: Annotated[ProxyInfo, Header()], proxy: Annotated[ProxyInfo, Header()],
request: Request, request: Request,
): ):
assert data.file.filename is not None
now = datetime.datetime.now() now = datetime.datetime.now()
directory = f"{now.year}/{now.month}" directory = f"{now.year}/{now.month}"
if not Path(data.file.filename).suffix: if not Path(data.file.filename).suffix:
raise HTTPException(status_code=400, detail="Invalid filename extension") raise HTTPException(status_code=400, detail="Invalid filename extension")
# XXX: normalize filename directory_absolute = Path(CONFIG["general"]["files"]) / directory
# XXX: avoid duplicates directory_absolute.mkdir(parents=True, exist_ok=True)
temp = tempfile.NamedTemporaryFile( temp = tempfile.NamedTemporaryFile(
prefix=normalize_filename(Path(data.file.filename).stem), prefix=normalize_filename(Path(data.file.filename).stem),
suffix=Path(data.file.filename).suffix, suffix=Path(data.file.filename).suffix,
dir=Path(CONFIG["general"]["files"]) / directory, dir=directory_absolute,
delete=False, delete=False,
) )
temp.close() temp.close()
print(temp.name)
sha256 = hashlib.sha256() sha256 = hashlib.sha256()
try: try: