Files
stemengine_old/backend/services/project_service_common.py
2026-03-26 15:14:41 +09:00

69 lines
2.1 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
from .separation.output_layout import parse_stem_label_from_filename
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:
return parse_stem_label_from_filename(file_stem, project_name)
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")