46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
# utils/asset_manager.py
|
|
|
|
import streamlit as st
|
|
from pathlib import Path
|
|
from config import SHARED_ASSETS_DIR
|
|
|
|
|
|
def list_shared_videos() -> list[str]:
|
|
"""列出所有共享影片的檔案名稱。"""
|
|
try:
|
|
if not SHARED_ASSETS_DIR.exists():
|
|
return []
|
|
|
|
return sorted(
|
|
[f.name for f in SHARED_ASSETS_DIR.glob('*') if f.suffix.lower() in ['.mp4', '.mov']]
|
|
)
|
|
except Exception as e:
|
|
# 可以在這裡記錄錯誤日誌
|
|
st.error(f"讀取共享素材庫時出錯: {e}")
|
|
return []
|
|
|
|
def save_uploaded_shared_videos(uploaded_files: list) -> tuple[int, list[str]]:
|
|
"""
|
|
將上傳的檔案儲存到共享素材庫。
|
|
回傳 (成功數量, 錯誤訊息列表)。
|
|
"""
|
|
if not uploaded_files:
|
|
return 0, []
|
|
|
|
saved_count = 0
|
|
errors = []
|
|
|
|
try:
|
|
SHARED_ASSETS_DIR.mkdir(parents=True, exist_ok=True)
|
|
for uploaded_file in uploaded_files:
|
|
try:
|
|
with open(SHARED_ASSETS_DIR / uploaded_file.name, "wb") as f:
|
|
f.write(uploaded_file.getbuffer())
|
|
saved_count += 1
|
|
except Exception as e:
|
|
errors.append(f"儲存 '{uploaded_file.name}' 時出錯: {e}")
|
|
except Exception as e:
|
|
errors.append(f"無法建立共享資料夾 '{SHARED_ASSETS_DIR}': {e}")
|
|
|
|
return saved_count, errors
|