1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

validate image paths relative to app directory

This commit is contained in:
checktheroads
2020-02-15 11:00:51 -07:00
parent 92c163736d
commit d4e5f9b66e
3 changed files with 47 additions and 30 deletions

View File

@@ -1,7 +1,9 @@
"""Utility Functions for Pydantic Models."""
# Standard Library
import os
import re
from pathlib import Path
# Third Party
from pydantic import BaseModel
@@ -77,3 +79,33 @@ class AnyUri(str):
def __repr__(self):
"""Stringify custom field representation."""
return f"AnyUri({super().__repr__()})"
def validate_image(value):
"""Convert file path to URL path.
Arguments:
value {FilePath} -- Path to logo file.
Returns:
{str} -- Formatted logo path
"""
base_path = value.split("/")
if base_path[0] == "/":
value = "/".join(base_path[1:])
if base_path[0] not in ("images", "custom"):
raise ValueError(
"Logo files must be in the 'custom/' directory of your hyperglass directory. Got: {f}",
f=value,
)
if base_path[0] == "custom":
config_path = Path(os.environ["hyperglass_directory"])
custom_file = config_path / "static" / value
if not custom_file.exists():
raise ValueError("'{f}' does not exist", f=str(custom_file))
return value

View File

@@ -1,15 +1,18 @@
"""Validate OpenGraph Configuration Parameters."""
# Standard Library
import os
from typing import Optional
from pathlib import Path
# Third Party
import PIL.Image as PilImage
from pydantic import FilePath, StrictInt, root_validator
from pydantic import StrictInt, StrictStr, root_validator
# Project
from hyperglass.configuration.models._utils import HyperglassModel
from hyperglass.configuration.models._utils import HyperglassModel, validate_image
CONFIG_PATH = Path(os.environ["hyperglass_directory"])
class OpenGraph(HyperglassModel):
@@ -17,10 +20,10 @@ class OpenGraph(HyperglassModel):
width: Optional[StrictInt]
height: Optional[StrictInt]
image: Optional[FilePath]
image: Optional[StrictStr]
@root_validator
def validate_image(cls, values):
def validate_opengraph(cls, values):
"""Set default opengraph image location.
Arguments:
@@ -40,13 +43,13 @@ class OpenGraph(HyperglassModel):
)
)
if values["image"] is None:
image = (
Path(__file__).parent.parent.parent
/ "static/images/hyperglass-opengraph.png"
)
values["image"] = "".join(str(image).split("static")[1::])
values["image"] = "images/hyperglass-opengraph.png"
with PilImage.open(image) as img:
values["image"] = validate_image(values["image"])
image_file = CONFIG_PATH / "static" / values["image"]
with PilImage.open(image_file) as img:
width, height = img.size
if values["width"] is None:
values["width"] = width

View File

@@ -1,9 +1,7 @@
"""Validate branding configuration variables."""
# Standard Library
import os
from typing import Optional
from pathlib import Path
# Third Party
from pydantic import (
@@ -20,7 +18,7 @@ from pydantic.color import Color
# Project
from hyperglass.constants import DNS_OVER_HTTPS, FUNC_COLOR_MAP
from hyperglass.configuration.models._utils import HyperglassModel
from hyperglass.configuration.models._utils import HyperglassModel, validate_image
from hyperglass.configuration.models.opengraph import OpenGraph
@@ -116,23 +114,7 @@ class Logo(HyperglassLevel3):
Returns:
{str} -- Formatted logo path
"""
base_path = value.split("/")
if base_path[0] == "/":
value = "/".join(base_path[1:])
if base_path[0] not in ("images", "custom"):
raise ValueError(
"Logo files must be in the 'custom/' directory of your hyperglass directory. Got: {f}",
f=value,
)
if base_path[0] == "custom":
config_path = Path(os.environ["hyperglass_directory"])
custom_file = config_path / "static" / value
if not custom_file.exists():
raise ValueError("'{f}' does not exist", f=str(custom_file))
return value
return validate_image(value)
class Config:
"""Override pydantic config."""