Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle gracefully downloadurls and srcsets with optional languages overwrite for files #1266

Merged
merged 7 commits into from
Sep 30, 2024
14 changes: 13 additions & 1 deletion src/viur/core/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from viur.core import conf, current, db, errors, utils
from viur.core.bones import BaseBone, BooleanBone, KeyBone, NumericBone, StringBone
from viur.core.decorators import *
from viur.core.i18n import LanguageWrapper
from viur.core.prototypes.tree import SkelType, Tree, TreeSkel
from viur.core.skeleton import SkeletonInstance, skeletonByKind
from viur.core.tasks import CallDeferred, DeleteEntitiesIter, PeriodicTask
Expand Down Expand Up @@ -630,7 +631,8 @@ def create_src_set(
file: t.Union["SkeletonInstance", dict, str],
expires: t.Optional[datetime.timedelta | int] = datetime.timedelta(hours=1),
width: t.Optional[int] = None,
height: t.Optional[int] = None
height: t.Optional[int] = None,
language: t.Optional[str] = None
skoegl marked this conversation as resolved.
Show resolved Hide resolved
) -> str:
"""
Generates a string suitable for use as the srcset tag in html. This functionality provides the browser
Expand All @@ -646,6 +648,7 @@ def create_src_set(
If a given width is not available, it will be skipped.
:param height: A list of heights that should be included in the srcset. If a given height is not available,
it will be skipped.
:param language: Language overwrite if file has multiple languages, and we want to explicitly specify one
:return: The srctag generated or an empty string if a invalid file object was supplied
"""
if not width and not height:
Expand All @@ -658,6 +661,15 @@ def create_src_set(
if not file:
return ""

if isinstance(file, LanguageWrapper):
current_lang = current.language.get()
if language in file and file[language]:
file = file[language]
elif current_lang and current_lang in file:
file = file[current_lang]
else:
file = file[conf.i18n.default_language]
skoegl marked this conversation as resolved.
Show resolved Hide resolved

if "dlkey" not in file and "dest" in file:
file = file["dest"]

Expand Down
22 changes: 18 additions & 4 deletions src/viur/core/render/html/env/viur.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import string
from viur.core import Method, current, db, errors, prototypes, securitykey, utils
from viur.core.config import conf
from viur.core.i18n import LanguageWrapper
from viur.core.i18n import translate as translate_class
from viur.core.render.html.utils import jinjaGlobalFilter, jinjaGlobalFunction
from viur.core.request import TEMPLATE_STYLE_KEY
Expand Down Expand Up @@ -679,7 +680,8 @@ def downloadUrlFor(
fileObj: dict,
expires: t.Optional[int] = conf.render_html_download_url_expiration,
derived: t.Optional[str] = None,
downloadFileName: t.Optional[str] = None
downloadFileName: t.Optional[str] = None,
language: t.Optional[str] = None
skoegl marked this conversation as resolved.
Show resolved Hide resolved
) -> str:
"""
Constructs a signed download-url for the given file-bone. Mostly a wrapper around
Expand All @@ -694,8 +696,19 @@ def downloadUrlFor(
Optional the filename of a derived file,
otherwise the download-link will point to the originally uploaded file.
:param downloadFileName: The filename to use when saving the response payload locally.
:param language: Language overwrite if fileObj has multiple languages and we want to explicitly specify one
:return: THe signed download-url relative to the current domain (eg /download/...)
"""

if isinstance(fileObj, LanguageWrapper):
current_lang = current.language.get()
if language in fileObj and fileObj[language]:
fileObj = fileObj[language]
elif current_lang and current_lang in fileObj:
fileObj = fileObj[current_lang]
else:
fileObj = fileObj[conf.i18n.default_language]
phorward marked this conversation as resolved.
Show resolved Hide resolved

if "dlkey" not in fileObj and "dest" in fileObj:
fileObj = fileObj["dest"]

Expand Down Expand Up @@ -733,7 +746,8 @@ def srcSetFor(
fileObj: dict,
expires: t.Optional[int] = conf.render_html_download_url_expiration,
width: t.Optional[int] = None,
height: t.Optional[int] = None
height: t.Optional[int] = None,
language: t.Optional[str] = None
skoegl marked this conversation as resolved.
Show resolved Hide resolved
) -> str:
"""
Generates a string suitable for use as the srcset tag in html. This functionality provides the browser with a list
Expand All @@ -750,10 +764,10 @@ def srcSetFor(
If a given width is not available, it will be skipped.
:param height: A list of heights that should be included in the srcset.
If a given height is not available, it will be skipped.

:param language: Language overwrite if fileObj has multiple languages and we want to explicitly specify one
:return: The srctag generated or an empty string if a invalid file object was supplied
"""
return file.File.create_src_set(fileObj, expires, width, height)
return file.File.create_src_set(fileObj, expires, width, height, language)


@jinjaGlobalFunction
Expand Down
Loading