Skip to content

Commit

Permalink
Merge pull request #91 from joegasewicz/Fix-static-routes-for-images-…
Browse files Browse the repository at this point in the history
…&-static-files-#90

#90 Fix static routes for images & static files
  • Loading branch information
joegasewicz committed Mar 7, 2024
2 parents 0a4ed56 + 2adeb2c commit 5e5d9aa
Show file tree
Hide file tree
Showing 8 changed files with 417 additions and 167 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.10", 3.11]
python-version: ["3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v2
Expand Down
12 changes: 8 additions & 4 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ name = "pypi"

[packages]
birman = "0.0.2"
sphinx-rtd-theme = "*"

[dev-packages]
pytest = "*"
gunicorn = "*"
sphinx = "*"
pylint = "2.15.6"
pytest-deprecate = "*"
tox = "*"
pip = "*"
install = "*"
sphinx-rtd-theme = "1.2.2"

sphinx-rtd-theme = "*"
pylint = "*"
# below pkgs are for py310 running pylint in CI
tomli = "*"
typing-extensions = "*"
dill = "*"
exceptiongroup = "*"
529 changes: 379 additions & 150 deletions Pipfile.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class sets the `STATIC_DIR` directory.

class Static(AbstractRoute):
def get(self, req: Request, res: Response) -> None:
res.set_static("/static/imgs/cat1.jpg")
res.set_static(req.path)
```
You can set the static file path using the :class:`~BaseOptions`.
```python
Expand All @@ -261,7 +261,7 @@ class Options(BaseOptions):
# Now in a route handler we can access static directory the via options
class Static(AbstractRoute):
def get(self, req: Request, res: Response) -> None:
res.set_static(f"{res.options.STATIC_DIR}/imgs/cat1.jpg")
res.set_static(req.path)
```
By default, `STATIC_DIR` is set to `/static`, if your static file is nested
within a Python package, for example `app/static` the set as `STATIC_DIR = "app/static"`
Expand Down
4 changes: 4 additions & 0 deletions bobtail/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ class MultipartFormDataError(Exception):

class TemplatePathError(Exception):
pass


class StaticFileError(Exception):
pass
2 changes: 1 addition & 1 deletion bobtail/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Request(ABC):

multipart: MultipartForm

def __init__(self, *,
def __init__(self, *, # pylint: disable=too-many-arguments
path: str,
method: str,
byte_data: bytes,
Expand Down
28 changes: 20 additions & 8 deletions bobtail/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json

from bobtail.options import BaseOptions
from bobtail.exceptions import StaticFileError


class Response:
Expand Down Expand Up @@ -137,7 +138,7 @@ class sets the `STATIC_DIR` directory.
class Static(AbstractRoute):
def get(self, req: Request, res: Response) -> None:
res.set_static("/static/imgs/cat1.jpg")
res.set_static(req.path)
You can set the static file path using the :class:`~BaseOptions`.
For example::
Expand All @@ -148,7 +149,7 @@ class Options(BaseOptions):
# Now in a route handler we can access static directory the via options
class Static(AbstractRoute):
def get(self, req: Request, res: Response) -> None:
res.set_static(f"{res.options.STATIC_DIR}/imgs/cat1.jpg")
res.set_static(req.path)
By default, `STATIC_DIR` is set to `/static`, if your static file is nested
within a Python package, for example `app/static` the set as `STATIC_DIR = "app/static"`
Expand All @@ -175,9 +176,20 @@ def get(self, req: Request, res: Response) -> None:
if len(path_seg) <= 1:
return None
path = path_seg[1]
path = f"{self.options.STATIC_DIR}{path}"
self.set_headers({"Content-Type": "image/jpeg"})
with open(path, "rb") as f:
file_data = f.read()
f.close()
self.static = file_data
try:
file_suffix = path.split("/")[-1:][0].split(".")[-1:][0]
path = f"{self.options.STATIC_DIR}{path}"
if file_suffix in ("jpg", "jpeg", "png"):
self.set_headers({"Content-Type": "image/jpeg"})
except Exception as exc:
self.set_status(500)
raise StaticFileError(
f"Error getting filetype from static file path - {path}"
) from exc
try:
with open(path, "rb") as f:
file_data = f.read()
f.close()
self.static = file_data
except FileNotFoundError:
self.set_status(404)
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="bobtail",
version="0.0.25",
version="0.0.26",
description="A little Python http framework",
packages=["bobtail"],
py_modules=["bobtail"],
Expand All @@ -17,6 +17,7 @@
classifiers=[
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
Expand Down

0 comments on commit 5e5d9aa

Please sign in to comment.