Fix boolean env variables (closes #213)

pull/178/head
Alex Shnitman 2023-02-04 11:09:36 +02:00
parent 3e6c63646c
commit ea7a7b0711
5 changed files with 16 additions and 5 deletions

4
.dockerignore Normal file
View File

@ -0,0 +1,4 @@
.git
.venv
ui/.angular
ui/node_modules

View File

@ -37,7 +37,7 @@ Certain values can be set via environment variables, using the `-e` parameter on
* __DOWNLOAD_DIR__: path to where the downloads will be saved. Defaults to `/downloads` in the docker image, and `.` otherwise. * __DOWNLOAD_DIR__: path to where the downloads will be saved. Defaults to `/downloads` in the docker image, and `.` otherwise.
* __AUDIO_DOWNLOAD_DIR__: path to where audio-only downloads will be saved, if you wish to separate them from the video downloads. Defaults to the value of `DOWNLOAD_DIR`. * __AUDIO_DOWNLOAD_DIR__: path to where audio-only downloads will be saved, if you wish to separate them from the video downloads. Defaults to the value of `DOWNLOAD_DIR`.
* __CUSTOM_DIRS__: whether to enable downloading videos into custom directories within the __DOWNLOAD_DIR__ (or __AUDIO_DOWNLOAD_DIR__). When enabled, a drop-down appears next to the Add button to specify the download directory. Defaults to `true`. * __CUSTOM_DIRS__: whether to enable downloading videos into custom directories within the __DOWNLOAD_DIR__ (or __AUDIO_DOWNLOAD_DIR__). When enabled, a drop-down appears next to the Add button to specify the download directory. Defaults to `true`.
* __CREATE_CUSTOM_DIRS__: whether to support automatically creating directories within the __DOWNLOAD_DIR__ (or __AUDIO_DOWNLOAD_DIR__) if they do not exist. When enabled, the download directory selector becomes supports free-text input, and the specified directory will be created recursively. Defaults to `false`. * __CREATE_CUSTOM_DIRS__: whether to support automatically creating directories within the __DOWNLOAD_DIR__ (or __AUDIO_DOWNLOAD_DIR__) if they do not exist. When enabled, the download directory selector becomes supports free-text input, and the specified directory will be created recursively. Defaults to `true`.
* __STATE_DIR__: path to where the queue persistence files will be saved. Defaults to `/downloads/.metube` in the docker image, and `.` otherwise. * __STATE_DIR__: path to where the queue persistence files will be saved. Defaults to `/downloads/.metube` in the docker image, and `.` otherwise.
* __URL_PREFIX__: base path for the web server (for use when hosting behind a reverse proxy). Defaults to `/`. * __URL_PREFIX__: base path for the web server (for use when hosting behind a reverse proxy). Defaults to `/`.
* __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__: 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`.

View File

@ -26,12 +26,19 @@ class Config:
'YTDL_OPTIONS': '{}', 'YTDL_OPTIONS': '{}',
} }
_BOOLEAN = ('CUSTOM_DIRS', 'CREATE_CUSTOM_DIRS')
def __init__(self): def __init__(self):
for k, v in self._DEFAULTS.items(): for k, v in self._DEFAULTS.items():
setattr(self, k, os.environ[k] if k in os.environ else v) setattr(self, k, os.environ[k] if k in os.environ else v)
for k, v in self.__dict__.items(): for k, v in self.__dict__.items():
if v.startswith('%%'): if v.startswith('%%'):
setattr(self, k, getattr(self, v[2:])) setattr(self, k, getattr(self, v[2:]))
if k in self._BOOLEAN:
if v not in ('true', 'false', 'True', 'False', 'on', 'off', '1', '0'):
log.error(f'Environment variable "{k}" is set to a non-boolean value "{v}"')
sys.exit(1)
setattr(self, k, v in ('true', 'True', 'on', '1'))
if not self.URL_PREFIX.endswith('/'): if not self.URL_PREFIX.endswith('/'):
self.URL_PREFIX += '/' self.URL_PREFIX += '/'
try: try:

View File

@ -236,14 +236,14 @@ class DownloadQueue:
# Keep consistent with frontend # Keep consistent with frontend
base_directory = self.config.DOWNLOAD_DIR if (quality != 'audio' and format != 'mp3') else self.config.AUDIO_DOWNLOAD_DIR base_directory = self.config.DOWNLOAD_DIR if (quality != 'audio' and format != 'mp3') else self.config.AUDIO_DOWNLOAD_DIR
if folder: if folder:
if self.config.CUSTOM_DIRS != 'true': if not self.config.CUSTOM_DIRS:
return {'status': 'error', 'msg': f'A folder for the download was specified but CUSTOM_DIRS is not true in the configuration.'} return {'status': 'error', 'msg': f'A folder for the download was specified but CUSTOM_DIRS is not true in the configuration.'}
dldirectory = os.path.realpath(os.path.join(base_directory, folder)) dldirectory = os.path.realpath(os.path.join(base_directory, folder))
real_base_directory = os.path.realpath(base_directory) real_base_directory = os.path.realpath(base_directory)
if not dldirectory.startswith(real_base_directory): if not dldirectory.startswith(real_base_directory):
return {'status': 'error', 'msg': f'Folder "{folder}" must resolve inside the base download directory "{real_base_directory}"'} return {'status': 'error', 'msg': f'Folder "{folder}" must resolve inside the base download directory "{real_base_directory}"'}
if not os.path.isdir(dldirectory): if not os.path.isdir(dldirectory):
if self.config.CREATE_CUSTOM_DIRS != 'true': if not self.config.CREATE_CUSTOM_DIRS:
return {'status': 'error', 'msg': f'Folder "{folder}" for download does not exist inside base directory "{real_base_directory}", and CREATE_CUSTOM_DIRS is not true in the configuration.'} return {'status': 'error', 'msg': f'Folder "{folder}" for download does not exist inside base directory "{real_base_directory}", and CREATE_CUSTOM_DIRS is not true in the configuration.'}
os.makedirs(dldirectory, exist_ok=True) os.makedirs(dldirectory, exist_ok=True)
else: else:

View File

@ -82,11 +82,11 @@ export class AppComponent implements AfterViewInit {
} }
showAdvanced() { showAdvanced() {
return this.downloads.configuration['CUSTOM_DIRS'] == 'true'; return this.downloads.configuration['CUSTOM_DIRS'];
} }
allowCustomDir(tag: string) { allowCustomDir(tag: string) {
if (this.downloads.configuration['CREATE_CUSTOM_DIRS'] == 'true') { if (this.downloads.configuration['CREATE_CUSTOM_DIRS']) {
return tag; return tag;
} }
return false; return false;