From 5f7b4667b46a03cf2c94f4838038d37c0517cf61 Mon Sep 17 00:00:00 2001 From: bfenty Date: Tue, 2 Jan 2024 13:14:48 -0600 Subject: [PATCH 1/3] Filter out files without extension --- backend/utils/fs.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backend/utils/fs.py b/backend/utils/fs.py index c3c81ad9e..61cef0bfd 100644 --- a/backend/utils/fs.py +++ b/backend/utils/fs.py @@ -231,9 +231,16 @@ def _exclude_files(files, filetype) -> list[str]: filtered_files: list = [] for file in files: - if file.split(".")[-1] in excluded_extensions or file in excluded_names: + # Split the file name to get the extension. + parts = file.split(".") + # Exclude the file if it has no extension or the extension is in the excluded list. + if len(parts) == 1 or parts[-1] in excluded_extensions: + filtered_files.append(file) + # Additionally, check if the entire file name is in the excluded names list. + elif file in excluded_names: filtered_files.append(file) + # Return files that are not in the filtered list. return [f for f in files if f not in filtered_files] From 7a2ae7341debb67e3b7e9e076aee0729f5d1e57f Mon Sep 17 00:00:00 2001 From: bfenty Date: Tue, 2 Jan 2024 13:56:41 -0600 Subject: [PATCH 2/3] added a maximum length to file extensions found that the code errors when there is a period in a filename (such as hidden files in UNIX OSes) and the file doesn't have a proper extension. Apple is really bad about putting these kinds of files in. To fix it, I added a new variable to the _exclude_files function, max_ext_length and set it to default to 4. That way if another function needs a different max it can override the default, but it shouldn't require other code changes. --- backend/utils/fs.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/utils/fs.py b/backend/utils/fs.py index 61cef0bfd..923824e94 100644 --- a/backend/utils/fs.py +++ b/backend/utils/fs.py @@ -225,7 +225,7 @@ def get_roms_structure(fs_slug: str): ) -def _exclude_files(files, filetype) -> list[str]: +def _exclude_files(files, filetype, max_ext_length=4) -> list[str]: # added a variable for maximum length of file extension, defaults to 4 excluded_extensions = config[f"EXCLUDED_{filetype.upper()}_EXT"] excluded_names = config[f"EXCLUDED_{filetype.upper()}_FILES"] filtered_files: list = [] @@ -233,8 +233,8 @@ def _exclude_files(files, filetype) -> list[str]: for file in files: # Split the file name to get the extension. parts = file.split(".") - # Exclude the file if it has no extension or the extension is in the excluded list. - if len(parts) == 1 or parts[-1] in excluded_extensions: + # Exclude the file if it has no extension, has an extension longer than max_ext_length characters, or the extension is in the excluded list. + if len(parts) == 1 or len(parts[-1]) > max_ext_length or parts[-1] in excluded_extensions: filtered_files.append(file) # Additionally, check if the entire file name is in the excluded names list. elif file in excluded_names: From 3bcc4b2aaf1a1edc606185814152095097445378 Mon Sep 17 00:00:00 2001 From: bfenty Date: Tue, 2 Jan 2024 21:26:00 -0600 Subject: [PATCH 3/3] filter files that begin with a period --- backend/utils/fs.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/backend/utils/fs.py b/backend/utils/fs.py index 923824e94..877ff3515 100644 --- a/backend/utils/fs.py +++ b/backend/utils/fs.py @@ -225,20 +225,24 @@ def get_roms_structure(fs_slug: str): ) -def _exclude_files(files, filetype, max_ext_length=4) -> list[str]: # added a variable for maximum length of file extension, defaults to 4 +def _exclude_files(files, filetype) -> list[str]: excluded_extensions = config[f"EXCLUDED_{filetype.upper()}_EXT"] excluded_names = config[f"EXCLUDED_{filetype.upper()}_FILES"] filtered_files: list = [] for file in files: - # Split the file name to get the extension. - parts = file.split(".") - # Exclude the file if it has no extension, has an extension longer than max_ext_length characters, or the extension is in the excluded list. - if len(parts) == 1 or len(parts[-1]) > max_ext_length or parts[-1] in excluded_extensions: - filtered_files.append(file) - # Additionally, check if the entire file name is in the excluded names list. - elif file in excluded_names: + # Exclude files starting with a period. + if file.startswith('.'): filtered_files.append(file) + else: + # Split the file name to get the extension. + parts = file.split(".") + # Exclude the file if it has no extension or the extension is in the excluded list. + if len(parts) == 1 or parts[-1] in excluded_extensions: + filtered_files.append(file) + # Additionally, check if the entire file name is in the excluded names list. + elif file in excluded_names: + filtered_files.append(file) # Return files that are not in the filtered list. return [f for f in files if f not in filtered_files]