~hristoast/mousikofidi

mousikofidi: Improve the file type checks v1 APPLIED

File type checks now use lists of extensions instead of a sequence of
hardcoded and's.
This makes it easier to extend the list, and make it
configurable in the future.

Note that cover art handling is untouched
yet, but I think people should be able to keep it in .bmp if they really
want to. ;)

I've verified that the patch cherry-picks cleanly into
the dev branch.

Daniil Baturin (1):
  Refactor the file extension checks to be more extensible and consice.

 mousikofidi/mousikofidi.py | 37 +++++++++++++++++++++++--------------
 1 file changed, 23 insertions(+), 14 deletions(-)

-- 
2.22.0
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~hristoast/mousikofidi/patches/9129/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH mousikofidi 1/1] Refactor the file extension checks to be more extensible and consice. Export this patch

From: Daniil Baturin <daniil@baturin.org>

This also enables making them configurable in the future.
---
 mousikofidi/mousikofidi.py | 37 +++++++++++++++++++++++--------------
 1 file changed, 23 insertions(+), 14 deletions(-)

diff --git a/mousikofidi/mousikofidi.py b/mousikofidi/mousikofidi.py
index d219a74..6e76285 100644
--- a/mousikofidi/mousikofidi.py
+++ b/mousikofidi/mousikofidi.py
@@ -60,6 +60,10 @@ THEMES = {
    "terminal-solarized": "/terminal-solarized",
}

AUDIO_FILE_EXTENSIONS = [".mp3", ".ogg", ".flac"]

VIDEO_FILE_EXTENSIONS = [".mp4", ".webm"]

try:
    local_config_file = os.path.join(
        os.path.abspath(os.path.dirname(os.getenv("FLASK_APP"))), "fidi.yml"
@@ -73,20 +77,31 @@ err = app.logger.error
wrn = app.logger.warning


def has_extension(filename: str, extensions) -> bool:
    # Support a single extension
    if type(extensions) != list:
        extensions = [extensions]

    _, extension = os.path.splitext(filename)
    extension = extension.lower()

    return extension in extensions


def quote(string: str) -> str:
    return urllib.parse.quote(string, safe="")


def file_metadata(file_path: str) -> dict:
    ft = None
    if file_path.lower().endswith(".flac"):
    if has_extension(file_path, ".flac"):
        try:
            data_string = FLAC(file_path).pprint()
            ft = "flac"
        except FLACNoHeaderError:
            wrn("This file is not a valid flac file: " + file_path)
            return {}
    elif file_path.lower().endswith(".mp3"):
    elif has_extension(file_path, ".mp3"):
        try:
            data_string = MP3(file_path).pprint()
            ft = "mp3"
@@ -94,7 +109,7 @@ def file_metadata(file_path: str) -> dict:
            wrn("This file is not a valid mp3 file: " + file_path)
            return {}

    elif file_path.lower().endswith(".ogg"):
    elif has_extension(file_path, ".ogg"):
        try:
            data_string = OggVorbis(file_path).tags.pprint()
            ft = "ogg"
@@ -102,7 +117,7 @@ def file_metadata(file_path: str) -> dict:
            wrn("This file is not a valid ogg vorbis file: " + file_path)
            return {}

    elif file_path.lower().endswith(".mp4"):
    elif has_extension(file_path, ".mp4"):
        try:
            data_string = MP4(file_path).pprint().strip("\xa9").rstrip("\xa9")
            ft = "mp4"
@@ -934,17 +949,11 @@ this ticket</a> for more information about how this will be fixed.</p>"""


def is_audio_file(file_path: str) -> bool:
    return os.path.isfile(file_path) and (
        file_path.endswith(".flac")
        or file_path.endswith(".mp3")
        or file_path.endswith(".ogg")
    )
    return os.path.isfile(file_path) and has_extension(file_path, AUDIO_FILE_EXTENSIONS)


def is_video_file(file_path: str) -> bool:
    return os.path.isfile(file_path) and (
        file_path.endswith(".mp4") or file_path.endswith(".webm")
    )
    return os.path.isfile(file_path) and has_extension(file_path, VIDEO_FILE_EXTENSIONS)


def is_valid_path(request_context: dict, path: str) -> bool:
@@ -1000,8 +1009,8 @@ def paths_list(music_dirs: list) -> list:
def search_files(q: str, only_audio: bool, only_video: bool) -> list:
    hits = []
    mdirs = app.fidiConfig["config"]["music_dirs"]
    audio_exts = ("flac", "mp3", "ogg")
    video_exts = ("mp4", "webm")
    audio_exts = AUDIO_FILE_EXTENSIONS
    video_exts = VIDEO_FILE_EXTENSIONS
    _exts = audio_exts + video_exts
    for topdir in mdirs:
        for dirpath, dirnames, files in os.walk(topdir):
-- 
2.22.0