This commit is contained in:
parent
46980f4574
commit
5774c239ad
BIN
dist/rupload-1.3.37-py3-none-any.whl
vendored
BIN
dist/rupload-1.3.37-py3-none-any.whl
vendored
Binary file not shown.
BIN
dist/rupload-1.3.37.tar.gz
vendored
BIN
dist/rupload-1.3.37.tar.gz
vendored
Binary file not shown.
@ -16,6 +16,7 @@ python_requires = >=3.7
|
||||
install_requires =
|
||||
aiohttp==3.10.10
|
||||
dataset==1.6.2
|
||||
pillow
|
||||
|
||||
[options.packages.find]
|
||||
where = src
|
||||
|
@ -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
|
||||
|
||||
|
@ -1,2 +1,3 @@
|
||||
aiohttp==3.10.10
|
||||
dataset==1.6.2
|
||||
pillow
|
||||
|
@ -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),
|
||||
]
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user