Fixed image input for absolute path (#1004)

* modify file function for both absolute and relative path input

* apply Path to keep app.cwd

* formatting and path check fixes

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
JefferyChiang 2022-04-16 01:29:21 +08:00 committed by GitHub
parent 10f938af9f
commit be291bf895
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,7 @@ import posixpath
import secrets
import traceback
import urllib
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Type
import orjson
@ -212,7 +213,8 @@ def file(path):
io.BytesIO(file_data), attachment_filename=os.path.basename(path)
)
else:
return FileResponse(safe_join(app.cwd, path))
if Path(app.cwd).resolve() in Path(path).resolve().parents:
return FileResponse(Path(path).resolve())
@app.get("/api", response_class=HTMLResponse) # Needed for Spaces
@ -344,7 +346,6 @@ def safe_join(directory: str, path: str) -> Optional[str]:
or filename.startswith("../")
):
return None
return posixpath.join(directory, filename)