check if passed name is basename

This commit is contained in:
Ren Tatsumoto 2025-06-04 13:45:55 +03:00
parent 3ded1db9ec
commit ba25b11e50

View file

@ -37,22 +37,25 @@ class SoundOrVideoTag:
"""Contains the filename inside a [sound:...] tag. """Contains the filename inside a [sound:...] tag.
Video files also use [sound:...]. Video files also use [sound:...].
Anki add-ons can supply an absolute file path to play any file on disk
using the built-in media player.
""" """
filename: str filename: str
def path(self, media_folder: str) -> str: def path(self, media_folder: str) -> str:
"Prepend the media folder to the filename." "Prepend the media folder to the filename."
if os.path.isfile(absolute_path := os.path.abspath(self.filename)): if os.path.basename(self.filename) == self.filename:
# Path in the current collection's media folder.
# Ensure filename doesn't reference parent folder.
head, tail = media_folder, self.filename
else:
# Add-ons can use absolute paths to play arbitrary files on disk. # Add-ons can use absolute paths to play arbitrary files on disk.
# Example: sound.av_player.play_tags([SoundOrVideoTag("/path/to/file")]) # Example: sound.av_player.play_tags([SoundOrVideoTag("/path/to/file")])
head, tail = os.path.split(absolute_path) head, tail = os.path.split(os.path.abspath(self.filename))
tail = hooks.media_file_filter(tail) tail = hooks.media_file_filter(tail)
return os.path.join(head, tail) return os.path.join(head, tail)
# Ensure filename doesn't reference parent folder
filename = os.path.basename(self.filename)
filename = hooks.media_file_filter(filename)
return os.path.join(media_folder, filename)
# note this does not include image tags, which are handled with HTML. # note this does not include image tags, which are handled with HTML.