Files
stemengine_old/backend/services/project_service_common.py
T

81 lines
2.4 KiB
Python

from __future__ import annotations
from pathlib import Path
from typing import Optional
from ..config import PROJECTS_DIR
from ..repositories import jobs as jobs_repo
class ProjectOperationError(Exception):
def __init__(self, status_code: int, detail: str):
super().__init__(detail)
self.status_code = status_code
self.detail = detail
def project_output_dir(project: dict) -> Path:
"""Return the project's root directory under MEDIA_DIR."""
sep_job = jobs_repo.get(project["sep_job_id"]) or {}
result = sep_job.get("result") or {}
project_path = result.get("project_path")
if project_path and Path(project_path).is_dir():
return Path(project_path)
return PROJECTS_DIR / project["name"]
def _infer_stem_name(file_stem: str, project_name: str) -> str:
import re
match = re.search(r'\(([^)]+)\)\s*$', file_stem.strip())
if match:
return match.group(1).strip().lower()
trimmed_stem = file_stem.strip()
trimmed_project = project_name.strip()
if trimmed_stem.lower().startswith(trimmed_project.lower()):
remainder = trimmed_stem[len(trimmed_project):].strip(' -_()')
if remainder:
return remainder.lower()
return file_stem.strip().lower()
def _stem_files_from_sep_job(sep_job: dict) -> dict:
return {
file_entry["stem"]: file_entry["path"]
for file_entry in sep_job.get("files", [])
if file_entry.get("type") == "stem"
and file_entry.get("stem")
and Path(file_entry.get("path", "")).exists()
}
def _find_stem_path(stem_files: dict, stem_name: str) -> Optional[str]:
if stem_name in stem_files:
return stem_files[stem_name]
for path in stem_files.values():
path_obj = Path(path)
if path_obj.name == stem_name or path_obj.stem == stem_name:
return path
return None
def _infer_output_format_from_paths(stem_files: dict) -> str:
for path in stem_files.values():
ext = Path(path).suffix.lower().lstrip(".")
if ext in ("mp3", "flac", "wav"):
return ext
return "mp3"
def _static_label(static_option: Optional[str]) -> str:
labels = {
"no_vocals": "No Vocals",
"no_drums": "No Drums",
"no_bass": "No Bass",
"no_vocals_no_bass": "No Vocals No Bass",
"no_drums_no_bass": "No Drums No Bass",
}
return labels.get(static_option or "", "Static Mix")