Added thumnails.
All checks were successful
RUpload build / Build (push) Successful in 1m9s

This commit is contained in:
retoor 2024-12-03 21:45:26 +01:00
parent 46980f4574
commit 5774c239ad
6 changed files with 35 additions and 2 deletions

Binary file not shown.

Binary file not shown.

View File

@ -16,6 +16,7 @@ python_requires = >=3.7
install_requires =
aiohttp==3.10.10
dataset==1.6.2
pillow
[options.packages.find]
where = src

View File

@ -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

View File

@ -1,2 +1,3 @@
aiohttp==3.10.10
dataset==1.6.2
pillow

View File

@ -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'<a href="{path}"><img class="thumbnail" src="{path}" alt="{image_path.name}" /></a>\n'
thumbnail_path = url.rstrip("/") + "/thumbnail/" + image_path.name
images_html += f'<a href="{path}"><img class="thumbnail" src="{thumbnail_path}" alt="{image_path.name}" /></a>\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),
]
)