Refactor: serve content file func

This commit is contained in:
Aroy-Art 2025-03-11 20:38:57 +01:00
parent 5b212b3f0d
commit b6b8b5918c
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -9,16 +9,33 @@ from sorl.thumbnail import get_thumbnail
from .models import PostFileModel
THUMBNAIL_SIZES = {
"sx": (64, ".thumb_64.jpg"),
"sm": (256, ".thumb_256.jpg"),
"md": (748, ".thumb_748.jpg"),
"lg": (1024, ".thumb_1024.jpg"),
"xl": (2048, ".thumb_2048.jpg"),
}
def get_thumbnail_file(source_path, size_key):
"""Generates and retrieves the thumbnail file."""
size, suffix = THUMBNAIL_SIZES.get(size_key, (None, ""))
if size:
thumbnail_file = get_thumbnail(source_path, str(size), upscale=False)
return os.path.abspath(
os.path.join(settings.MEDIA_ROOT, thumbnail_file.name)
), suffix
return None, ""
def serve_content_file(request, file_hash):
"""
View function to serve content files for download or inline viewing
View function to serve content files for download or inline viewing.
"""
print(f"Requesting file: {file_hash}")
download = request.GET.get("d")
thumbnail = request.GET.get("t")
download = request.GET.get("d") == "0"
thumbnail_key = request.GET.get("t")
try:
obj_file = get_object_or_404(PostFileModel, hash_blake3=file_hash)
@ -26,53 +43,24 @@ def serve_content_file(request, file_hash):
file_type = obj_file.file_type
source_file = obj_file.file
if thumbnail:
if file_type != "image":
source_file = obj_file.thumbnail
# Use thumbnail if requested and file type is image
if thumbnail_key and file_type != "image":
source_file = obj_file.thumbnail
if thumbnail == "sx":
file_name += ".thumb_64.jpg"
thumbnail_file = get_thumbnail(source_file.path, "64", upscale=False)
file = open(
os.path.abspath(os.path.join(settings.MEDIA_ROOT, thumbnail_file.name)),
"rb",
)
elif thumbnail == "sm":
file_name += ".thumb_256.jpg"
thumbnail_file = get_thumbnail(source_file.path, "256", upscale=False)
file = open(
os.path.abspath(os.path.join(settings.MEDIA_ROOT, thumbnail_file.name)),
"rb",
)
elif thumbnail == "md":
file_name += ".thumb_748.jpg"
thumbnail_file = get_thumbnail(source_file.path, "748", upscale=False)
file = open(
os.path.abspath(os.path.join(settings.MEDIA_ROOT, thumbnail_file.name)),
"rb",
)
elif thumbnail == "lg":
file_name += ".thumb_1024.jpg"
thumbnail_file = get_thumbnail(source_file.path, "1024", upscale=False)
file = open(
os.path.abspath(os.path.join(settings.MEDIA_ROOT, thumbnail_file.name)),
"rb",
)
elif thumbnail == "xl":
file_name += ".thumb_2048.jpg"
thumbnail_file = get_thumbnail(source_file.path, "2048", upscale=False)
file = open(
os.path.abspath(os.path.join(settings.MEDIA_ROOT, thumbnail_file.name)),
"rb",
)
# Retrieve the requested thumbnail file if applicable
if thumbnail_key in THUMBNAIL_SIZES:
thumbnail_path, suffix = get_thumbnail_file(source_file.path, thumbnail_key)
if thumbnail_path:
file_name += suffix
file = open(thumbnail_path, "rb")
else:
file = source_file.file
else:
file = source_file.file
response = FileResponse(file)
if download == "1":
response["Content-Disposition"] = f'attachment; filename="{file_name}"'
else:
response["Content-Disposition"] = f'inline; filename="{file_name}"'
disposition_type = "attachment" if download else "inline"
response["Content-Disposition"] = f'{disposition_type}; filename="{file_name}"'
return response