diff --git a/Makefile b/Makefile index f153fc1..e3febf7 100644 --- a/Makefile +++ b/Makefile @@ -5,13 +5,15 @@ GUNICORN=./.venv/bin/gunicorn GUNICORN_WORKERS = 1 PORT = 8081 +python: + $(PYTHON) +run: + $(GUNICORN) -w $(GUNICORN_WORKERS) -k aiohttp.worker.GunicornWebWorker snek.gunicorn:app --bind 0.0.0.0:$(PORT) --reload + install: python3 -m venv .venv $(PIP) install -e . -run: - $(GUNICORN) -w $(GUNICORN_WORKERS) -k aiohttp.worker.GunicornWebWorker snek.gunicorn:app --bind 0.0.0.0:$(PORT) --reload - diff --git a/pyproject.toml b/pyproject.toml index 2d5b6d9..e1075c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ dependencies = [ "asyncssh", "emoji", "aiofiles", - "PyJWT" + "PyJWT", + "multiavatar" ] diff --git a/src/snek/app.py b/src/snek/app.py index 3a61c2b..99f23e7 100644 --- a/src/snek/app.py +++ b/src/snek/app.py @@ -33,8 +33,10 @@ from snek.view.status import StatusView from snek.view.web import WebView from snek.view.upload import UploadView from snek.view.search_user import SearchUserView +from snek.view.avatar import AvatarView from snek.system.profiler import profiler_handler + SESSION_KEY = b"c79a0c5fda4b424189c427d28c9f7c34" @@ -101,6 +103,7 @@ class Application(BaseApplication): self.router.add_view("/drive.bin/{uid}.{ext}", UploadView) self.router.add_view("/search-user.html", SearchUserView) self.router.add_view("/search-user.json", SearchUserView) + self.router.add_view("/avatar/{uid}.svg", AvatarView) self.router.add_get("/http-get", self.handle_http_get) self.router.add_get("/http-photo", self.handle_http_photo) self.router.add_get("/rpc.ws", RPCView) diff --git a/src/snek/templates/message.html b/src/snek/templates/message.html index 2773f0a..e8afc31 100644 --- a/src/snek/templates/message.html +++ b/src/snek/templates/message.html @@ -1 +1 @@ -
{{user_nick[0]}}
{{user_nick}}
{% autoescape false %}{% emoji %}{% linkify %}{% markdown %}{% autoescape false %}{{ message }}{%raw %} {% endraw%}{%endautoescape%}{% endmarkdown %}{% endlinkify %}{% endemoji %}{% endautoescape %}
+
{{user_nick}}
{% autoescape false %}{% emoji %}{% linkify %}{% markdown %}{% autoescape false %}{{ message }}{%raw %} {% endraw%}{%endautoescape%}{% endmarkdown %}{% endlinkify %}{% endemoji %}{% endautoescape %}
diff --git a/src/snek/view/avatar.py b/src/snek/view/avatar.py new file mode 100644 index 0000000..065ff6a --- /dev/null +++ b/src/snek/view/avatar.py @@ -0,0 +1,39 @@ +# Written by retoor@molodetz.nl + +# This code defines a WebView class that inherits from BaseView and includes a method for rendering a web template, requiring login access for its usage. + +# The code imports the BaseView class from the `snek.system.view` module. + +# MIT License +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +from multiavatar import multiavatar + +from aiohttp import web +from snek.system.view import BaseView + +class AvatarView(BaseView): + login_required = True + + async def get(self): + uid = self.request.match_info.get("uid") + avatar = multiavatar.multiavatar(uid,None, None) + response = web.Response(text=avatar, content_type='image/svg+xml') + response.headers['Cache-Control'] = f'public, max-age={1337*42}' + return response diff --git a/src/snek/view/upload.py b/src/snek/view/upload.py index 05ffcaa..a80ec72 100644 --- a/src/snek/view/upload.py +++ b/src/snek/view/upload.py @@ -23,7 +23,9 @@ class UploadView(BaseView): async def get(self): uid = self.request.match_info.get("uid") drive_item = await self.services.drive_item.get(uid) - return web.FileResponse(drive_item["path"]) + response = web.FileResponse(drive_item["path"]) + response.headers['Cache-Control'] = f'public, max-age={1337*420}' + return response async def post(self): reader = await self.request.multipart()