normalize filename
This commit is contained in:
parent
d197f80d7b
commit
67527ef585
1 changed files with 13 additions and 1 deletions
|
@ -52,6 +52,18 @@ class UploadModel(BaseModel):
|
||||||
file: UploadFile
|
file: UploadFile
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_filename(filename: str):
|
||||||
|
"""
|
||||||
|
>>> normalize_filename("ciao")
|
||||||
|
'ciao'
|
||||||
|
>>> normalize_filename("Hello, 42 worlds!")
|
||||||
|
'Hello42worlds'
|
||||||
|
"""
|
||||||
|
return "".join(
|
||||||
|
c for c in filename
|
||||||
|
if c.isalnum() or c in '-_'
|
||||||
|
)
|
||||||
|
|
||||||
@app.post("/upload")
|
@app.post("/upload")
|
||||||
async def upload(
|
async def upload(
|
||||||
data: Annotated[UploadModel, Form()],
|
data: Annotated[UploadModel, Form()],
|
||||||
|
@ -66,7 +78,7 @@ async def upload(
|
||||||
# XXX: normalize filename
|
# XXX: normalize filename
|
||||||
# XXX: avoid duplicates
|
# XXX: avoid duplicates
|
||||||
temp = tempfile.NamedTemporaryFile(
|
temp = tempfile.NamedTemporaryFile(
|
||||||
prefix=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=Path(CONFIG["general"]["files"]) / directory,
|
||||||
delete=False,
|
delete=False,
|
||||||
|
|
Loading…
Reference in a new issue