mirror of
https://github.com/gradio-app/gradio.git
synced 2025-02-23 11:39:17 +08:00
Fix validate url (#2957)
* Fix validate url * Add unit test + fix impl * Fix CHANGELOG * Use head instead of get to check url * Add head route for files Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
parent
0091b366af
commit
cab8d888dc
@ -12,6 +12,7 @@ No changes to highlight.
|
|||||||
* Fixes bug where png files were not being recognized when uploading images by [@abidlabs](https://github.com/abidlabs) in [PR 3002](https://github.com/gradio-app/gradio/pull/3002)
|
* Fixes bug where png files were not being recognized when uploading images by [@abidlabs](https://github.com/abidlabs) in [PR 3002](https://github.com/gradio-app/gradio/pull/3002)
|
||||||
* Fixes bug where temporary uploaded files were not being added to temp sets by [@abidlabs](https://github.com/abidlabs) in [PR 3005](https://github.com/gradio-app/gradio/pull/3005)
|
* Fixes bug where temporary uploaded files were not being added to temp sets by [@abidlabs](https://github.com/abidlabs) in [PR 3005](https://github.com/gradio-app/gradio/pull/3005)
|
||||||
* Fixes issue where markdown support in chatbot breaks older demos [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 3006](https://github.com/gradio-app/gradio/pull/3006)
|
* Fixes issue where markdown support in chatbot breaks older demos [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 3006](https://github.com/gradio-app/gradio/pull/3006)
|
||||||
|
* Fix bug where the Image component could not serialize image urls by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 2957](https://github.com/gradio-app/gradio/pull/2957)
|
||||||
|
|
||||||
## Documentation Changes:
|
## Documentation Changes:
|
||||||
* SEO improvements to guides by[@aliabd](https://github.com/aliabd) in [PR 2915](https://github.com/gradio-app/gradio/pull/2915)
|
* SEO improvements to guides by[@aliabd](https://github.com/aliabd) in [PR 2915](https://github.com/gradio-app/gradio/pull/2915)
|
||||||
|
@ -255,6 +255,7 @@ class App(FastAPI):
|
|||||||
else:
|
else:
|
||||||
return FileResponse(blocks.favicon_path)
|
return FileResponse(blocks.favicon_path)
|
||||||
|
|
||||||
|
@app.head("/file={path:path}", dependencies=[Depends(login_check)])
|
||||||
@app.get("/file={path:path}", dependencies=[Depends(login_check)])
|
@app.get("/file={path:path}", dependencies=[Depends(login_check)])
|
||||||
async def file(path: str, request: fastapi.Request):
|
async def file(path: str, request: fastapi.Request):
|
||||||
abs_path = str(Path(path).resolve())
|
abs_path = str(Path(path).resolve())
|
||||||
|
@ -80,8 +80,10 @@ class ImgSerializable(Serializable):
|
|||||||
"""
|
"""
|
||||||
if x is None or x == "":
|
if x is None or x == "":
|
||||||
return None
|
return None
|
||||||
|
is_url = utils.validate_url(x)
|
||||||
|
path = x if is_url else Path(load_dir) / x
|
||||||
return processing_utils.encode_url_or_file_to_base64(
|
return processing_utils.encode_url_or_file_to_base64(
|
||||||
Path(load_dir) / x, encryption_key=encryption_key
|
path, encryption_key=encryption_key
|
||||||
)
|
)
|
||||||
|
|
||||||
def deserialize(
|
def deserialize(
|
||||||
|
@ -710,7 +710,10 @@ def append_unique_suffix(name: str, list_of_names: List[str]):
|
|||||||
def validate_url(possible_url: str) -> bool:
|
def validate_url(possible_url: str) -> bool:
|
||||||
headers = {"User-Agent": "gradio (https://gradio.app/; team@gradio.app)"}
|
headers = {"User-Agent": "gradio (https://gradio.app/; team@gradio.app)"}
|
||||||
try:
|
try:
|
||||||
return requests.get(possible_url, headers=headers).ok
|
head_request = requests.head(possible_url, headers=headers)
|
||||||
|
if head_request.status_code == 405:
|
||||||
|
return requests.get(possible_url, headers=headers).ok
|
||||||
|
return head_request.ok
|
||||||
except Exception:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -668,6 +668,12 @@ class TestImage:
|
|||||||
image_output = gr.Image(type="numpy")
|
image_output = gr.Image(type="numpy")
|
||||||
assert image_output.postprocess(y_img).startswith("data:image/png;base64,")
|
assert image_output.postprocess(y_img).startswith("data:image/png;base64,")
|
||||||
|
|
||||||
|
@pytest.mark.flaky
|
||||||
|
def test_serialize_url(self):
|
||||||
|
img = "https://gradio.app/assets/img/header-image.jpg"
|
||||||
|
expected = processing_utils.encode_url_or_file_to_base64(img)
|
||||||
|
assert gr.Image().serialize(img) == expected
|
||||||
|
|
||||||
def test_in_interface_as_input(self):
|
def test_in_interface_as_input(self):
|
||||||
"""
|
"""
|
||||||
Interface, process, interpret
|
Interface, process, interpret
|
||||||
|
Loading…
Reference in New Issue
Block a user