Static DIR and FILE resource types (#2244)
* Explicit static directive for serving file or dir Co-authored-by: anbuhckr <36891836+anbuhckr@users.noreply.github.com> Co-authored-by: anbuhckr <miki.suhendra@gmail.com>
This commit is contained in:
parent
404c5f9f9e
commit
d9796e9b1e
|
@ -592,6 +592,7 @@ class RouteMixin:
|
||||||
strict_slashes=None,
|
strict_slashes=None,
|
||||||
content_type=None,
|
content_type=None,
|
||||||
apply=True,
|
apply=True,
|
||||||
|
resource_type=None,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Register a root to serve files from. The input can either be a
|
Register a root to serve files from. The input can either be a
|
||||||
|
@ -641,6 +642,7 @@ class RouteMixin:
|
||||||
host,
|
host,
|
||||||
strict_slashes,
|
strict_slashes,
|
||||||
content_type,
|
content_type,
|
||||||
|
resource_type,
|
||||||
)
|
)
|
||||||
self._future_statics.add(static)
|
self._future_statics.add(static)
|
||||||
|
|
||||||
|
@ -836,8 +838,27 @@ class RouteMixin:
|
||||||
name = static.name
|
name = static.name
|
||||||
# If we're not trying to match a file directly,
|
# If we're not trying to match a file directly,
|
||||||
# serve from the folder
|
# serve from the folder
|
||||||
|
if not static.resource_type:
|
||||||
if not path.isfile(file_or_directory):
|
if not path.isfile(file_or_directory):
|
||||||
uri += "/<__file_uri__:path>"
|
uri += "/<__file_uri__:path>"
|
||||||
|
elif static.resource_type == "dir":
|
||||||
|
if path.isfile(file_or_directory):
|
||||||
|
raise TypeError(
|
||||||
|
"Resource type improperly identified as directory. "
|
||||||
|
f"'{file_or_directory}'"
|
||||||
|
)
|
||||||
|
uri += "/<__file_uri__:path>"
|
||||||
|
elif static.resource_type == "file" and not path.isfile(
|
||||||
|
file_or_directory
|
||||||
|
):
|
||||||
|
raise TypeError(
|
||||||
|
"Resource type improperly identified as file. "
|
||||||
|
f"'{file_or_directory}'"
|
||||||
|
)
|
||||||
|
elif static.resource_type != "file":
|
||||||
|
raise ValueError(
|
||||||
|
"The resource_type should be set to 'file' or 'dir'"
|
||||||
|
)
|
||||||
|
|
||||||
# special prefix for static files
|
# special prefix for static files
|
||||||
# if not static.name.startswith("_static_"):
|
# if not static.name.startswith("_static_"):
|
||||||
|
|
|
@ -52,6 +52,7 @@ class FutureStatic(NamedTuple):
|
||||||
host: Optional[str]
|
host: Optional[str]
|
||||||
strict_slashes: Optional[bool]
|
strict_slashes: Optional[bool]
|
||||||
content_type: Optional[bool]
|
content_type: Optional[bool]
|
||||||
|
resource_type: Optional[str]
|
||||||
|
|
||||||
|
|
||||||
class FutureSignal(NamedTuple):
|
class FutureSignal(NamedTuple):
|
||||||
|
|
|
@ -523,3 +523,56 @@ def test_multiple_statics(app, static_file_directory):
|
||||||
assert response.body == get_file_content(
|
assert response.body == get_file_content(
|
||||||
static_file_directory, "python.png"
|
static_file_directory, "python.png"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_resource_type_default(app, static_file_directory):
|
||||||
|
app.static("/static", static_file_directory)
|
||||||
|
app.static("/file", get_file_path(static_file_directory, "test.file"))
|
||||||
|
|
||||||
|
_, response = app.test_client.get("/static")
|
||||||
|
assert response.status == 404
|
||||||
|
|
||||||
|
_, response = app.test_client.get("/file")
|
||||||
|
assert response.status == 200
|
||||||
|
assert response.body == get_file_content(
|
||||||
|
static_file_directory, "test.file"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_resource_type_file(app, static_file_directory):
|
||||||
|
app.static(
|
||||||
|
"/file",
|
||||||
|
get_file_path(static_file_directory, "test.file"),
|
||||||
|
resource_type="file",
|
||||||
|
)
|
||||||
|
|
||||||
|
_, response = app.test_client.get("/file")
|
||||||
|
assert response.status == 200
|
||||||
|
assert response.body == get_file_content(
|
||||||
|
static_file_directory, "test.file"
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(TypeError):
|
||||||
|
app.static("/static", static_file_directory, resource_type="file")
|
||||||
|
|
||||||
|
|
||||||
|
def test_resource_type_dir(app, static_file_directory):
|
||||||
|
app.static("/static", static_file_directory, resource_type="dir")
|
||||||
|
|
||||||
|
_, response = app.test_client.get("/static/test.file")
|
||||||
|
assert response.status == 200
|
||||||
|
assert response.body == get_file_content(
|
||||||
|
static_file_directory, "test.file"
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(TypeError):
|
||||||
|
app.static(
|
||||||
|
"/file",
|
||||||
|
get_file_path(static_file_directory, "test.file"),
|
||||||
|
resource_type="dir",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_resource_type_unknown(app, static_file_directory, caplog):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
app.static("/static", static_file_directory, resource_type="unknown")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user