Spaces:
Sleeping

File size: 1,631 Bytes
264fa46
 
2ad6d77
264fa46
 
 
fa9ab61
 
 
 
 
 
 
 
 
 
 
264fa46
 
 
 
 
 
 
fa9ab61
264fa46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
base_tool.py
共通ロジック:タスク状態管理・ZIP化・パス管理
"""
import os
import zipfile
from typing import Dict, TypedDict


# --- タスク状態の型定義 ---
class TaskState(TypedDict, total=False):
    status:       str    # "processing" | "completed" | "failed"
    progress:     int    # 0〜100
    last_log:     str    # ユーザー向けの進捗メッセージ
    error:        str    # エラー時のメッセージ
    completed_at: float  # 完了時刻(time.time())


# --- パス設定 ---
BASE_TEMP_DIR = "/tmp/media_master"
os.makedirs(BASE_TEMP_DIR, exist_ok=True)

# --- タスク状態管理(メモリ上) ---
# app.py からインポートして使うことで、全モジュールで同一インスタンスを共有する
tasks: Dict[str, TaskState] = {}


# --- パスヘルパー ---
def get_session_paths(task_id: str) -> tuple[str, str, str]:
    """(session_path, input_path, output_path) を返す"""
    session_path = os.path.join(BASE_TEMP_DIR, task_id)
    input_path   = os.path.join(session_path, "input")
    output_path  = os.path.join(session_path, "output")
    os.makedirs(output_path, exist_ok=True)
    return session_path, input_path, output_path


# --- ZIP化 ---
def zip_output(session_path: str, output_path: str) -> str:
    """output_path 内のファイルをまとめて result.zip を作成し、そのパスを返す"""
    final_zip = os.path.join(session_path, "result.zip")
    with zipfile.ZipFile(final_zip, "w") as z:
        for f in os.listdir(output_path):
            z.write(os.path.join(output_path, f), f)
    return final_zip