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
+10 -3
View File
@@ -214,10 +214,17 @@ async def tts_endpoint(req: TTSRequest):
# 编码 WAV 到内存 # 编码 WAV 到内存
try: try:
import tempfile
import soundfile as sf import soundfile as sf
bio = BytesIO() # soundfile 不支持直接写入 BytesIO,需要用临时文件
sf.write(bio, wav_data, sr, format="WAV") with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
audio_bytes = bio.getvalue() 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: except Exception as e:
logger.exception("音频编码失败") logger.exception("音频编码失败")
raise HTTPException(status_code=500, detail=f"音频编码失败: {e}") raise HTTPException(status_code=500, detail=f"音频编码失败: {e}")
+12 -8
View File
@@ -42,6 +42,7 @@
<div v-if="loading" class="univer-overlay-loading"> <div v-if="loading" class="univer-overlay-loading">
<div class="loading-spinner"></div> <div class="loading-spinner"></div>
<div class="loading-text">正在初始化 Univer 引擎...</div> <div class="loading-text">正在初始化 Univer 引擎...</div>
<div class="loading-hint">提示当前为空白预览模式</div>
</div> </div>
<div v-if="error" class="univer-overlay-error"> <div v-if="error" class="univer-overlay-error">
<div class="error-card"> <div class="error-card">
@@ -49,7 +50,7 @@
<div class="error-title">预览初始化失败</div> <div class="error-title">预览初始化失败</div>
<div class="error-msg">{{ error }}</div> <div class="error-msg">{{ error }}</div>
<button class="retry-btn" @click="retry">重试</button> <button class="retry-btn" @click="retry">重试</button>
<p class="error-hint">提示纯前端模式下仅支持空白文档创建如需加载实际的 Office 文件内容请确保后端服务已配置导入功能</p> <p class="error-hint">提示当前为空白文档预览模式Univer 开源版本不支持直接加载 Office 文件内容</p>
</div> </div>
</div> </div>
</div> </div>
@@ -129,32 +130,35 @@ const loadDocumentIntoUniver = async (instance, blob, fileName, fmt) => {
const lowerFmt = (fmt || '').toLowerCase(); const lowerFmt = (fmt || '').toLowerCase();
console.log(`[Univer] 开始加载文档: ${fileName}, 格式: ${lowerFmt}`); 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 { try {
// 尝试创建对应格式的空白文档
if (lowerFmt === 'xlsx') { if (lowerFmt === 'xlsx') {
await univerAPI.createWorkbook({}); await univerAPI.createWorkbook({});
console.log('[Univer] 创建空白 Excel 工作簿'); console.log('[Univer] 创建空白 Excel 工作簿');
} else if (lowerFmt === 'pptx') { } else if (lowerFmt === 'pptx') {
// PPTX: 尝试使用 Slides API,如果不可用则降级到 Docs // 尝试 Slides API
if (typeof univerAPI.createUniverSlide === 'function') { if (typeof univerAPI.createUniverSlide === 'function') {
await univerAPI.createUniverSlide({}); await univerAPI.createUniverSlide({});
console.log('[Univer] 创建空白 PPT 演示文稿'); console.log('[Univer] 创建空白 PPT 演示文稿');
} else { } else {
await univerAPI.createUniverDoc({}); await univerAPI.createUniverDoc({});
console.log('[Univer] Slides API 不可用,创建空白文档'); console.log('[Univer] 创建空白文档(Slides API 不可用');
} }
} else { } else {
// DOCX 和默认情况
await univerAPI.createUniverDoc({}); await univerAPI.createUniverDoc({});
console.log('[Univer] 创建空白 Word 文档'); console.log('[Univer] 创建空白 Word 文档');
} }
} catch (e) { } catch (e) {
console.error('[Univer] 创建文档失败:', e); console.error('[Univer] 创建文档失败:', e);
throw new Error(`无法创建 ${lowerFmt.toUpperCase()} 文档: ${e.message}`); throw new Error(`无法创建文档: ${e.message}`);
} }
}; };