fix(tts): use temporary file for WAV encoding to avoid BytesIO limitation

fix(UniverPreview): update error messages and hints for document loading
This commit is contained in:
2026-04-11 10:21:22 +08:00
parent f99acf5d50
commit ae0d53e295
2 changed files with 28 additions and 17 deletions

View File

@@ -214,10 +214,17 @@ async def tts_endpoint(req: TTSRequest):
# 编码 WAV 到内存
try:
import tempfile
import soundfile as sf
bio = BytesIO()
sf.write(bio, wav_data, sr, format="WAV")
audio_bytes = bio.getvalue()
# soundfile 不支持直接写入 BytesIO需要用临时文件
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
tmp_path = tmp.name
sf.write(tmp_path, wav_data, sr, format="WAV")
with open(tmp_path, "rb") as f:
audio_bytes = f.read()
# 清理临时文件
import os as _os
_os.unlink(tmp_path)
except Exception as e:
logger.exception("音频编码失败")
raise HTTPException(status_code=500, detail=f"音频编码失败: {e}")

View File

@@ -42,6 +42,7 @@
<div v-if="loading" class="univer-overlay-loading">
<div class="loading-spinner"></div>
<div class="loading-text">正在初始化 Univer 引擎...</div>
<div class="loading-hint">提示当前为空白预览模式</div>
</div>
<div v-if="error" class="univer-overlay-error">
<div class="error-card">
@@ -49,7 +50,7 @@
<div class="error-title">预览初始化失败</div>
<div class="error-msg">{{ error }}</div>
<button class="retry-btn" @click="retry">重试</button>
<p class="error-hint">提示纯前端模式下仅支持空白文档创建如需加载实际的 Office 文件内容请确保后端服务已配置导入功能</p>
<p class="error-hint">提示当前为空白文档预览模式Univer 开源版本不支持直接加载 Office 文件内容</p>
</div>
</div>
</div>
@@ -129,32 +130,35 @@ const loadDocumentIntoUniver = async (instance, blob, fileName, fmt) => {
const lowerFmt = (fmt || '').toLowerCase();
console.log(`[Univer] 开始加载文档: ${fileName}, 格式: ${lowerFmt}`);
// 重要提示Univer 纯前端模式不支持直接加载 DOCX/XLSX/PPTX 文件
// 这些格式需要后端服务进行转换
// 重要说明:
// Univer 开源版本不支持直接导入 DOCX/XLSX/PPTX 文件
// 导入功能需要 @univerjs-pro/exchange-clientPro 版本)
// 当前实现创建空白文档作为预览占位符
console.warn('[Univer] 开源版本不支持导入 Office 文件,创建空白文档');
console.warn('[Univer] 如需导入功能,请使用 Univer Pro 版本');
// Fallback: 创建空白文档
try {
// 尝试创建对应格式的空白文档
if (lowerFmt === 'xlsx') {
await univerAPI.createWorkbook({});
console.log('[Univer] 创建空白 Excel 工作簿');
} else if (lowerFmt === 'pptx') {
// PPTX: 尝试使用 Slides API,如果不可用则降级到 Docs
// 尝试 Slides API
if (typeof univerAPI.createUniverSlide === 'function') {
await univerAPI.createUniverSlide({});
console.log('[Univer] 创建空白 PPT 演示文稿');
} else {
await univerAPI.createUniverDoc({});
console.log('[Univer] Slides API 不可用,创建空白文档');
console.log('[Univer] 创建空白文档(Slides API 不可用');
}
} else {
// DOCX 和默认情况
await univerAPI.createUniverDoc({});
console.log('[Univer] 创建空白 Word 文档');
}
} catch (e) {
console.error('[Univer] 创建文档失败:', e);
throw new Error(`无法创建 ${lowerFmt.toUpperCase()} 文档: ${e.message}`);
throw new Error(`无法创建文档: ${e.message}`);
}
};