Skip to content

Commit 5e5d9aa

Browse files
author
Joe Gasewicz
authored
Merge pull request #91 from joegasewicz/Fix-static-routes-for-images-&-static-files-#90
#90 Fix static routes for images & static files
2 parents 0a4ed56 + 2adeb2c commit 5e5d9aa

File tree

8 files changed

+417
-167
lines changed

8 files changed

+417
-167
lines changed

.github/workflows/python-package.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
fail-fast: false
1717
matrix:
18-
python-version: ["3.10", 3.11]
18+
python-version: ["3.10", "3.11", "3.12"]
1919

2020
steps:
2121
- uses: actions/checkout@v2

Pipfile

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ name = "pypi"
55

66
[packages]
77
birman = "0.0.2"
8+
sphinx-rtd-theme = "*"
89

910
[dev-packages]
1011
pytest = "*"
1112
gunicorn = "*"
1213
sphinx = "*"
13-
pylint = "2.15.6"
1414
pytest-deprecate = "*"
1515
tox = "*"
1616
pip = "*"
17-
install = "*"
18-
sphinx-rtd-theme = "1.2.2"
19-
17+
sphinx-rtd-theme = "*"
18+
pylint = "*"
19+
# below pkgs are for py310 running pylint in CI
20+
tomli = "*"
21+
typing-extensions = "*"
22+
dill = "*"
23+
exceptiongroup = "*"

Pipfile.lock

+379-150
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class sets the `STATIC_DIR` directory.
250250

251251
class Static(AbstractRoute):
252252
def get(self, req: Request, res: Response) -> None:
253-
res.set_static("/static/imgs/cat1.jpg")
253+
res.set_static(req.path)
254254
```
255255
You can set the static file path using the :class:`~BaseOptions`.
256256
```python
@@ -261,7 +261,7 @@ class Options(BaseOptions):
261261
# Now in a route handler we can access static directory the via options
262262
class Static(AbstractRoute):
263263
def get(self, req: Request, res: Response) -> None:
264-
res.set_static(f"{res.options.STATIC_DIR}/imgs/cat1.jpg")
264+
res.set_static(req.path)
265265
```
266266
By default, `STATIC_DIR` is set to `/static`, if your static file is nested
267267
within a Python package, for example `app/static` the set as `STATIC_DIR = "app/static"`

bobtail/exceptions.py

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ class MultipartFormDataError(Exception):
2020

2121
class TemplatePathError(Exception):
2222
pass
23+
24+
25+
class StaticFileError(Exception):
26+
pass

bobtail/request.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Request(ABC):
3030

3131
multipart: MultipartForm
3232

33-
def __init__(self, *,
33+
def __init__(self, *, # pylint: disable=too-many-arguments
3434
path: str,
3535
method: str,
3636
byte_data: bytes,

bobtail/response.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33

44
from bobtail.options import BaseOptions
5+
from bobtail.exceptions import StaticFileError
56

67

78
class Response:
@@ -137,7 +138,7 @@ class sets the `STATIC_DIR` directory.
137138
138139
class Static(AbstractRoute):
139140
def get(self, req: Request, res: Response) -> None:
140-
res.set_static("/static/imgs/cat1.jpg")
141+
res.set_static(req.path)
141142
142143
You can set the static file path using the :class:`~BaseOptions`.
143144
For example::
@@ -148,7 +149,7 @@ class Options(BaseOptions):
148149
# Now in a route handler we can access static directory the via options
149150
class Static(AbstractRoute):
150151
def get(self, req: Request, res: Response) -> None:
151-
res.set_static(f"{res.options.STATIC_DIR}/imgs/cat1.jpg")
152+
res.set_static(req.path)
152153
153154
By default, `STATIC_DIR` is set to `/static`, if your static file is nested
154155
within a Python package, for example `app/static` the set as `STATIC_DIR = "app/static"`
@@ -175,9 +176,20 @@ def get(self, req: Request, res: Response) -> None:
175176
if len(path_seg) <= 1:
176177
return None
177178
path = path_seg[1]
178-
path = f"{self.options.STATIC_DIR}{path}"
179-
self.set_headers({"Content-Type": "image/jpeg"})
180-
with open(path, "rb") as f:
181-
file_data = f.read()
182-
f.close()
183-
self.static = file_data
179+
try:
180+
file_suffix = path.split("/")[-1:][0].split(".")[-1:][0]
181+
path = f"{self.options.STATIC_DIR}{path}"
182+
if file_suffix in ("jpg", "jpeg", "png"):
183+
self.set_headers({"Content-Type": "image/jpeg"})
184+
except Exception as exc:
185+
self.set_status(500)
186+
raise StaticFileError(
187+
f"Error getting filetype from static file path - {path}"
188+
) from exc
189+
try:
190+
with open(path, "rb") as f:
191+
file_data = f.read()
192+
f.close()
193+
self.static = file_data
194+
except FileNotFoundError:
195+
self.set_status(404)

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name="bobtail",
10-
version="0.0.25",
10+
version="0.0.26",
1111
description="A little Python http framework",
1212
packages=["bobtail"],
1313
py_modules=["bobtail"],
@@ -17,6 +17,7 @@
1717
classifiers=[
1818
"Programming Language :: Python :: 3.10",
1919
"Programming Language :: Python :: 3.11",
20+
"Programming Language :: Python :: 3.12",
2021
"License :: OSI Approved :: MIT License",
2122
"Operating System :: OS Independent",
2223
],

0 commit comments

Comments
 (0)