Skip to content

Commit

Permalink
feat: Add File.get_download_url() (#1305)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Max Meyer <[email protected]>
  • Loading branch information
ArneGudermann and phorward authored Oct 30, 2024
1 parent 909f176 commit 743600b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/viur/core/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,56 @@ def add(self, skelType: SkelType, node: db.Key | int | str | None = None, *args,

return super().add(skelType, node, *args, **kwargs)

@exposed
def get_download_url(
self,
key: t.Optional[db.Key] = None,
dlkey: t.Optional[str] = None,
filename: t.Optional[str] = None,
derived: bool = False,

):
"""
Request a download url for a given file
:param key: The key of the file
:param dlkey: The download key of the file
:param filename: The filename to be given. If no filename is provided
downloadUrls for all derived files are returned in case of `derived=True`.
:param derived: True, if a derived file download URL is being requested.
"""
skel = self.viewSkel("leaf")
if dlkey is not None:
skel = skel.all().filter("dlkey", dlkey).getSkel()
elif key is None and dlkey is None:
raise errors.BadRequest("No key or dlkey provided")

if not (skel and skel.read(key)):
raise errors.NotFound()

if not self.canView("leaf", skel):
raise errors.Unauthorized()

dlkey = skel["dlkey"]

if derived and filename is None:
res = {}
for filename in skel["derived"]["files"]:
res[filename] = self.create_download_url(dlkey, filename, derived)
else:
if derived:
# Check if Filename exist in the Derives. We sign nothing that not exist.
if filename not in skel["derived"]["files"]:
raise errors.NotFound("File not in derives")
else:
if filename is None:
filename = skel["name"]
elif filename != skel["name"]:
raise errors.NotFound("Filename not match")

res = self.create_download_url(dlkey, filename, derived)

return self.render.view(res)

def onEdit(self, skelType: SkelType, skel: SkeletonInstance):
super().onEdit(skelType, skel)
old_skel = self.editSkel(skelType)
Expand Down

0 comments on commit 743600b

Please sign in to comment.