diff --git a/README.md b/README.md index a5a322d..77d9ff1 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,8 @@ Certain values can be set via environment variables, using the `-e` parameter on * __OUTPUT_TEMPLATE__: The template for the filenames of the downloaded videos, formatted according to [this spec](https://github.com/yt-dlp/yt-dlp/blob/master/README.md#output-template). Defaults to `%(title)s.%(ext)s`. * __OUTPUT_TEMPLATE_CHAPTER__: The template for the filenames of the downloaded videos when split into chapters via postprocessors. Defaults to `%(title)s - %(section_number)s %(section_title)s.%(ext)s`. -* __OUTPUT_TEMPLATE_PLAYLIST__: The template for the filenames of the downloaded videos when downloaded as a playlist. Defaults to `%(playlist_title)s/%(title)s.%(ext)s`. When empty, then `OUTPUT_TEMPLATE` is used. -* __OUTPUT_TEMPLATE_CHANNEL__: The template for the filenames of the downloaded videos when downloaded as a channel. Defaults to `%(channel)s/%(title)s.%(ext)s`. When empty, then `OUTPUT_TEMPLATE` is used. +* __OUTPUT_TEMPLATE_PLAYLIST__: The template for the filenames of the downloaded videos when downloaded as a playlist. Defaults to empty (uses `OUTPUT_TEMPLATE`). Set to e.g. `%(playlist_title)s/%(title)s.%(ext)s` to group each playlist into its own subdirectory. +* __OUTPUT_TEMPLATE_CHANNEL__: The template for the filenames of the downloaded videos when downloaded as a channel. Defaults to empty (uses `OUTPUT_TEMPLATE`). Set to e.g. `%(channel)s/%(title)s.%(ext)s` to group each channel into its own subdirectory. * __YTDL_OPTIONS__: Additional options to pass to yt-dlp in JSON format. [See available options here](https://github.com/yt-dlp/yt-dlp/blob/master/yt_dlp/YoutubeDL.py#L222). They roughly correspond to command-line options, though some do not have exact equivalents here. For example, `--recode-video` has to be specified via `postprocessors`. Also note that dashes are replaced with underscores. You may find [this script](https://github.com/yt-dlp/yt-dlp/blob/master/devscripts/cli_to_api.py) helpful for converting from command-line options to `YTDL_OPTIONS`. * __YTDL_OPTIONS_FILE__: A path to a JSON file that will be loaded and used for populating `YTDL_OPTIONS` above. Please note that if both `YTDL_OPTIONS_FILE` and `YTDL_OPTIONS` are specified, the options in `YTDL_OPTIONS` take precedence. The file will be monitored for changes and reloaded automatically when changes are detected. diff --git a/app/main.py b/app/main.py index 54bc952..d2a1923 100644 --- a/app/main.py +++ b/app/main.py @@ -57,8 +57,8 @@ class Config: 'PUBLIC_HOST_AUDIO_URL': 'audio_download/', 'OUTPUT_TEMPLATE': '%(title)s.%(ext)s', 'OUTPUT_TEMPLATE_CHAPTER': '%(title)s - %(section_number)02d - %(section_title)s.%(ext)s', - 'OUTPUT_TEMPLATE_PLAYLIST': '%(playlist_title)s/%(title)s.%(ext)s', - 'OUTPUT_TEMPLATE_CHANNEL': '%(channel)s/%(title)s.%(ext)s', + 'OUTPUT_TEMPLATE_PLAYLIST': '', + 'OUTPUT_TEMPLATE_CHANNEL': '', 'DEFAULT_OPTION_PLAYLIST_ITEM_LIMIT' : '0', 'CLEAR_COMPLETED_AFTER': '0', 'YTDL_OPTIONS': '{}', @@ -115,6 +115,25 @@ class Config: def _apply_runtime_overrides(self): self.YTDL_OPTIONS.update(self._runtime_overrides) + # Keys sent to the browser. Sensitive or server-only keys (YTDL_OPTIONS, + # paths, TLS config, etc.) are intentionally excluded. + _FRONTEND_KEYS = ( + 'CUSTOM_DIRS', + 'CREATE_CUSTOM_DIRS', + 'OUTPUT_TEMPLATE_CHAPTER', + 'PUBLIC_HOST_URL', + 'PUBLIC_HOST_AUDIO_URL', + 'DEFAULT_OPTION_PLAYLIST_ITEM_LIMIT', + ) + + def frontend_safe(self) -> dict: + """Return only the config keys that are safe to expose to browser clients. + + Sensitive or server-only keys (YTDL_OPTIONS, file-system paths, TLS + settings, etc.) are intentionally excluded. + """ + return {k: getattr(self, k) for k in self._FRONTEND_KEYS} + def load_ytdl_options(self) -> tuple[bool, str]: try: self.YTDL_OPTIONS = json.loads(os.environ.get('YTDL_OPTIONS', '{}')) @@ -420,7 +439,7 @@ async def history(request): async def connect(sid, environ): log.info(f"Client connected: {sid}") await sio.emit('all', serializer.encode(dqueue.get()), to=sid) - await sio.emit('configuration', serializer.encode(config), to=sid) + await sio.emit('configuration', serializer.encode(config.frontend_safe()), to=sid) if config.CUSTOM_DIRS: await sio.emit('custom_dirs', serializer.encode(get_custom_dirs()), to=sid) if config.YTDL_OPTIONS_FILE: @@ -448,8 +467,12 @@ def get_custom_dirs(): else: return re.search(config.CUSTOM_DIRS_EXCLUDE_REGEX, d) is None - # Recursively lists all subdirectories of DOWNLOAD_DIR + # Recursively lists all subdirectories of DOWNLOAD_DIR. + # Always include '' (the base directory itself) even when the + # directory is empty or does not yet exist. dirs = list(filter(include_dir, map(convert, path.glob('**/')))) + if '' not in dirs: + dirs.insert(0, '') return dirs diff --git a/app/ytdl.py b/app/ytdl.py index f64f669..f73eb36 100644 --- a/app/ytdl.py +++ b/app/ytdl.py @@ -29,6 +29,26 @@ def _compile_outtmpl_pattern(field: str) -> re.Pattern: return re.compile(STR_FORMAT_RE_TMPL.format(re.escape(field), conversion_types)) +# Characters that are invalid in Windows/NTFS path components. These are pre- +# sanitised when substituting playlist/channel titles into output templates so +# that downloads do not fail on NTFS-mounted volumes or Windows Docker hosts. +_WINDOWS_INVALID_PATH_CHARS = re.compile(r'[\\:*?"<>|]') + + +def _sanitize_path_component(value: Any) -> Any: + """Replace characters that are invalid in Windows path components with '_'. + + Non-string values (int, float, None, …) are passed through unchanged so + that ``_outtmpl_substitute_field`` can still coerce them with format specs + (e.g. ``%(playlist_index)02d``). Only string values are sanitised because + Windows-invalid characters are only a concern for human-readable strings + (titles, channel names, etc.) that may end up as directory names. + """ + if not isinstance(value, str): + return value + return _WINDOWS_INVALID_PATH_CHARS.sub('_', value) + + def _outtmpl_substitute_field(template: str, field: str, value: Any) -> str: """Substitute a single field in an output template, applying any format specifiers to the value.""" pattern = _compile_outtmpl_pattern(field) @@ -631,13 +651,13 @@ class DownloadQueue: output = self.config.OUTPUT_TEMPLATE_PLAYLIST for property, value in entry.items(): if property.startswith("playlist"): - output = _outtmpl_substitute_field(output, property, value) + output = _outtmpl_substitute_field(output, property, _sanitize_path_component(value)) if entry is not None and entry.get('channel_index') is not None: if len(self.config.OUTPUT_TEMPLATE_CHANNEL): output = self.config.OUTPUT_TEMPLATE_CHANNEL for property, value in entry.items(): if property.startswith("channel"): - output = _outtmpl_substitute_field(output, property, value) + output = _outtmpl_substitute_field(output, property, _sanitize_path_component(value)) ytdl_options = dict(self.config.YTDL_OPTIONS) playlist_item_limit = getattr(dl, 'playlist_item_limit', 0) if playlist_item_limit > 0: