diff --git a/TODO.md b/TODO.md index 972e53c0..d465b2d8 100644 --- a/TODO.md +++ b/TODO.md @@ -5,6 +5,7 @@ - Package jsoni and publish on PyPi - Rewrite stores to use dictionaries instead of list pools - last updated date on tracks added via watchdog is broken +- Disable the watchdog by default, and mark it as experimental # DONE - Support auth headers diff --git a/app/api/stream.py b/app/api/stream.py index 714c3599..ccc030cf 100644 --- a/app/api/stream.py +++ b/app/api/stream.py @@ -3,13 +3,11 @@ Contains all the track routes. """ import os -import time -from flask import Blueprint, send_file, request, Response +from flask import send_file, request, Response from flask_openapi3 import APIBlueprint, Tag from pydantic import BaseModel, Field from app.api.apischemas import TrackHashSchema -from app.lib.pydub.pydub.audio_segment import AudioSegment from app.lib.trackslib import get_silence_paddings from app.store.tracks import TrackStore @@ -25,10 +23,55 @@ class SendTrackFileQuery(BaseModel): ) +@api.get("//classic") +def send_track_file_classic(path: TrackHashSchema, query: SendTrackFileQuery): + """ + Get a playable audio file without Range support + + Returns a playable audio file that corresponds to the given filepath. Falls back to track hash if filepath is not found. + """ + trackhash = path.trackhash + filepath = query.filepath + msg = {"msg": "File Not Found"} + + def get_mime(filename: str) -> str: + ext = filename.rsplit(".", maxsplit=1)[-1] + return f"audio/{ext}" + + # If filepath is provide, try to send that + if filepath is not None: + try: + track = TrackStore.get_tracks_by_filepaths([filepath])[0] + except IndexError: + track = None + + track_exists = track is not None and os.path.exists(track.filepath) + + if track_exists: + audio_type = get_mime(filepath) + return send_file(filepath, mimetype=audio_type) + + # Else, find file by trackhash + tracks = TrackStore.get_tracks_by_trackhashes([trackhash]) + + for track in tracks: + if track is None: + return msg, 404 + + audio_type = get_mime(track.filepath) + + try: + return send_file(track.filepath, mimetype=audio_type) + except (FileNotFoundError, OSError) as e: + return msg, 404 + + return msg, 404 + + @api.get("/") def send_track_file(path: TrackHashSchema, query: SendTrackFileQuery): """ - Get file + Get a playable audio file with Range headers support Returns a playable audio file that corresponds to the given filepath. Falls back to track hash if filepath is not found. """ diff --git a/manage.py b/manage.py index 71e7f3e0..ddf13c76 100644 --- a/manage.py +++ b/manage.py @@ -49,23 +49,24 @@ werkzeug.setLevel(logging.ERROR) # Background tasks -@background -def bg_run_setup(): - run_periodic_scans() +# @background +# def bg_run_setup(): +# pass + # run_periodic_scans() -@background -def start_watchdog(): - WatchDog().run() +# @background +# def start_watchdog(): +# WatchDog().run() @background def run_swingmusic(): log_startup_info() - bg_run_setup() + # bg_run_setup() register_plugins() - start_watchdog() + # start_watchdog() setproctitle.setproctitle(f"swingmusic ::{FLASKVARS.get_flask_port()}") diff --git a/poetry.lock b/poetry.lock index 481d868a..f03244a1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.0 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "altgraph" @@ -1067,6 +1067,16 @@ files = [ {file = "MarkupSafe-2.1.3-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:5bbe06f8eeafd38e5d0a4894ffec89378b6c6a625ff57e3028921f8ff59318ac"}, {file = "MarkupSafe-2.1.3-cp311-cp311-win32.whl", hash = "sha256:dd15ff04ffd7e05ffcb7fe79f1b98041b8ea30ae9234aed2a9168b5797c3effb"}, {file = "MarkupSafe-2.1.3-cp311-cp311-win_amd64.whl", hash = "sha256:134da1eca9ec0ae528110ccc9e48041e0828d79f24121a1a146161103c76e686"}, + {file = "MarkupSafe-2.1.3-cp312-cp312-macosx_10_9_universal2.whl", hash = "sha256:f698de3fd0c4e6972b92290a45bd9b1536bffe8c6759c62471efaa8acb4c37bc"}, + {file = "MarkupSafe-2.1.3-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:aa57bd9cf8ae831a362185ee444e15a93ecb2e344c8e52e4d721ea3ab6ef1823"}, + {file = "MarkupSafe-2.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ffcc3f7c66b5f5b7931a5aa68fc9cecc51e685ef90282f4a82f0f5e9b704ad11"}, + {file = "MarkupSafe-2.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:47d4f1c5f80fc62fdd7777d0d40a2e9dda0a05883ab11374334f6c4de38adffd"}, + {file = "MarkupSafe-2.1.3-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1f67c7038d560d92149c060157d623c542173016c4babc0c1913cca0564b9939"}, + {file = "MarkupSafe-2.1.3-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:9aad3c1755095ce347e26488214ef77e0485a3c34a50c5a5e2471dff60b9dd9c"}, + {file = "MarkupSafe-2.1.3-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:14ff806850827afd6b07a5f32bd917fb7f45b046ba40c57abdb636674a8b559c"}, + {file = "MarkupSafe-2.1.3-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8f9293864fe09b8149f0cc42ce56e3f0e54de883a9de90cd427f191c346eb2e1"}, + {file = "MarkupSafe-2.1.3-cp312-cp312-win32.whl", hash = "sha256:715d3562f79d540f251b99ebd6d8baa547118974341db04f5ad06d5ea3eb8007"}, + {file = "MarkupSafe-2.1.3-cp312-cp312-win_amd64.whl", hash = "sha256:1b8dd8c3fd14349433c79fa8abeb573a55fc0fdd769133baac1f5e07abf54aeb"}, {file = "MarkupSafe-2.1.3-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:8e254ae696c88d98da6555f5ace2279cf7cd5b3f52be2b5cf97feafe883b58d2"}, {file = "MarkupSafe-2.1.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:cb0932dc158471523c9637e807d9bfb93e06a95cbf010f1a38b98623b929ef2b"}, {file = "MarkupSafe-2.1.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9402b03f1a1b4dc4c19845e5c749e3ab82d5078d16a2a4c2cd2df62d57bb0707"}, @@ -2170,12 +2180,13 @@ widechars = ["wcwidth"] [[package]] name = "tinytag" -version = "1.9.0" +version = "1.10.1" description = "Read music meta data and length of MP3, OGG, OPUS, MP4, M4A, FLAC, WMA and Wave files" optional = false python-versions = ">=2.7" files = [ - {file = "tinytag-1.9.0.tar.gz", hash = "sha256:f8d71110e1e680a33d99202e00a5a698481d25d20173b81ba3e863423979e014"}, + {file = "tinytag-1.10.1-py3-none-any.whl", hash = "sha256:e437654d04c966fbbbdbf807af61eb9759f1d80e4173a7d26202506b37cfdaf0"}, + {file = "tinytag-1.10.1.tar.gz", hash = "sha256:122a63b836f85094aacca43fc807aaee3290be3de17d134f5f4a08b509ae268f"}, ] [package.extras] @@ -2504,4 +2515,4 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"] [metadata] lock-version = "2.0" python-versions = ">=3.10,<3.12" -content-hash = "317c4094a6f768467219db94be02a6e2e9f1ed5ca18d929437094bf60be1594d" +content-hash = "5722346cbfc224340877e337d4cee2c8e8a3a7ea68f9a64d9c5806e0ebcf919a" diff --git a/pyproject.toml b/pyproject.toml index 5ff4920b..2cd8ede6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ Pillow = "^9.0.1" "colorgram.py" = "^1.2.0" tqdm = "^4.65.0" rapidfuzz = "^2.13.7" -tinytag = "^1.8.1" +tinytag = "^1.10.1" Unidecode = "^1.3.6" psutil = "^5.9.4" show-in-file-manager = "^1.1.4"