diff --git a/dist/rupload-1.3.37-py3-none-any.whl b/dist/rupload-1.3.37-py3-none-any.whl index a300158..f21f427 100644 Binary files a/dist/rupload-1.3.37-py3-none-any.whl and b/dist/rupload-1.3.37-py3-none-any.whl differ diff --git a/dist/rupload-1.3.37.tar.gz b/dist/rupload-1.3.37.tar.gz index 5e6a630..f2b4176 100644 Binary files a/dist/rupload-1.3.37.tar.gz and b/dist/rupload-1.3.37.tar.gz differ diff --git a/setup.cfg b/setup.cfg index 8a83c01..7935fc5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -16,6 +16,7 @@ python_requires = >=3.7 install_requires = aiohttp==3.10.10 dataset==1.6.2 + pillow [options.packages.find] where = src diff --git a/src/rupload.egg-info/PKG-INFO b/src/rupload.egg-info/PKG-INFO index ea294cf..b2a3f81 100644 --- a/src/rupload.egg-info/PKG-INFO +++ b/src/rupload.egg-info/PKG-INFO @@ -9,6 +9,7 @@ Requires-Python: >=3.7 Description-Content-Type: text/markdown Requires-Dist: aiohttp==3.10.10 Requires-Dist: dataset==1.6.2 +Requires-Dist: pillow # RUpload diff --git a/src/rupload.egg-info/requires.txt b/src/rupload.egg-info/requires.txt index 15b74db..ef2e58b 100644 --- a/src/rupload.egg-info/requires.txt +++ b/src/rupload.egg-info/requires.txt @@ -1,2 +1,3 @@ aiohttp==3.10.10 dataset==1.6.2 +pillow diff --git a/src/rupload/app.py b/src/rupload/app.py index d655f5f..72b70f6 100644 --- a/src/rupload/app.py +++ b/src/rupload/app.py @@ -200,7 +200,8 @@ def create_images_html(url, image_paths): images_html = "" for image_path in image_paths: path = url.rstrip("/") + "/" + image_path.name - images_html += f'{image_path.name}\n' + thumbnail_path = url.rstrip("/") + "/thumbnail/" + image_path.name + images_html += f'{image_path.name}\n' return images_html @@ -267,6 +268,34 @@ async def handle_upload(request: web.Request): return web.Response(status=400, text="No file uploaded.") +async def handle_thumbnail(request: web.Request): + path = request.match_info["path"] + + safe_path = pathlib.Path(request.app.upload_path).joinpath(path) + + if not safe_path.exists(): + return web.Response(status=404, text="File not found.") + + if not safe_path.is_file(): + return web.Response(status=400, text="Invalid file type.") + + try: + pathlib.Path(safe_path).relative_to(request.app.upload_path) + except ValueError: + return web.Response(status=404, text="File not found.") + + thumbnail_path = pathlib.Path(safe_path).parent.joinpath(".thumbnail").joinpath(safe_path.name) + if not thumbnail_path.parent.exists(): + thumbnail_path.parent.mkdir(exist_ok=True,parents=False) + + if not thumbnail_path.exists(): + from PIL import Image + image = Image.open(safe_path) + image.thumbnail((200, 200)) + image.save(thumbnail_path) + + return web.FileResponse(thumbnail_path) + async def handle_index(request: web.Request): image_paths = get_images(request.app.upload_path) images_html = create_images_html( @@ -308,10 +337,11 @@ def create_app( ) pathlib.Path(upload_path).mkdir(parents=True, exist_ok=True) app.add_routes( - [ + [ web.get("/", handle_index), web.post("/upload", handle_upload), web.static("/", upload_path), + web.get("/uploads/thumbnail/{path:.*}", handle_thumbnail), web.static("/uploads", upload_path), ] )