Skip to content

Commit

Permalink
added option to use output dir for web downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
xnetcat committed Dec 19, 2022
1 parent 7903364 commit 8f73d76
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
11 changes: 11 additions & 0 deletions spotdl/utils/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,17 @@ def parse_web_options(parser: _ArgumentGroup):
help="The allowed origins for the web server.",
)

# Add use output directory argument
parser.add_argument(
"--web-use-output-dir",
action="store_const",
const=True,
help=(
"Use the output directory instead of the session directory for downloads. ("
"This might cause issues if you have multiple users using the web-ui at the same time)"
),
)


def parse_misc_options(parser: _ArgumentGroup):
"""
Expand Down
1 change: 1 addition & 0 deletions spotdl/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,5 @@ def get_config() -> Dict[str, Any]:
"playlist_numbering": False,
"preserve_original_audio": False,
"scan_for_songs": False,
"web_use_output_dir": False,
}
34 changes: 23 additions & 11 deletions spotdl/utils/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import asyncio
import logging
import os
from pathlib import Path
from typing import Dict, Any, List, Optional

import mimetypes
Expand Down Expand Up @@ -200,7 +201,7 @@ async def websocket_endpoint(websocket: WebSocket, client_id: str):

await asyncio.sleep(1)

# Wait 5 seconds before shutting down
# Wait 1 second before shutting down
# This is to prevent the server from shutting down when a client
# disconnects and reconnects quickly (e.g. when refreshing the page)
if len(app_state.ws_instances) == 0:
Expand Down Expand Up @@ -270,9 +271,12 @@ async def download_url(
- returns the file path if the song was downloaded.
"""

state.downloader.output = str(
(get_spotdl_path() / f"web/sessions/{client_id}").absolute()
)
if state.settings.get("web_use_output_dir", False):
state.downloader.output = state.settings["output"]
else:
state.downloader.output = str(
(get_spotdl_path() / f"web/sessions/{client_id}").absolute()
)

ws_instance = WSProgressHandler.get_instance(client_id)
if ws_instance is not None:
Expand All @@ -296,10 +300,7 @@ async def download_url(
status_code=500, detail=f"Error downloading: {song.name}"
)

# Strip Filename
filename = os.path.basename(path)

return filename
return str(path.absolute())

except Exception as exception:
state.logger.error(f"Error downloading! {exception}")
Expand All @@ -310,7 +311,9 @@ async def download_url(


@router.get("/api/download/file")
async def download_file(file: str, client_id: str) -> FileResponse:
async def download_file(
file: str, state: ApplicationState = Depends(get_current_state)
) -> FileResponse:
"""
Download file using path.
Expand All @@ -321,9 +324,18 @@ async def download_file(file: str, client_id: str) -> FileResponse:
- returns the file response, filename specified to return as attachment.
"""

expected_path = str((get_spotdl_path() / "web/sessions").absolute())
if state.settings.get("web_use_output_dir", False):
expected_path = str(Path(state.settings["output"].split("{", 1)[0]).absolute())

if (not file.endswith(state.settings["format"])) or (
not file.startswith(expected_path)
):
raise HTTPException(status_code=400, detail="Invalid download path.")

return FileResponse(
str((get_spotdl_path() / f"web/sessions/{client_id}/{file}").absolute()),
filename=file,
file,
filename=os.path.basename(file),
)


Expand Down

0 comments on commit 8f73d76

Please sign in to comment.