Skip to content

Commit 1a961c9

Browse files
committed
🎈 perf(server): improve audio handling and CORS support
- Updated Content Security Policy to allow media sources - Simplified audio retrieval method by directly sending base64 buffer - Added global CORS headers to support cross-origin requests - Updated database configuration to support custom PostgreSQL port
1 parent 8e4393a commit 1a961c9

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

api-server.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ async function get_api_server(proxy_utils) {
129129
setHeaders: function (res, path, stat) {
130130
res.set(
131131
"Content-Security-Policy",
132-
"default-src 'none'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'none'; connect-src 'self'"
132+
"default-src 'none'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'none'; connect-src 'self'; media-src 'self' blob: data:;"
133133
);
134134
},
135135
})
@@ -435,30 +435,17 @@ async function get_api_server(proxy_utils) {
435435
return res.status(404).json({ success: false, error: "File not found." });
436436
}
437437

438-
// 假设 recording 字段存储的是 Base64 编码的音频数据
439-
const base64Data = bot.recording; // 获取 Base64 数据
440-
const tempFileName = path.join(__dirname, `temp_audio_${req.query.id}.mp3`); // 临时文件名
438+
// 方法1: 直接返回base64数据,让前端处理
439+
res.set("Content-Type", "audio/mpeg");
440+
res.set("Access-Control-Allow-Origin", "*");
441+
res.set(
442+
"Access-Control-Allow-Headers",
443+
"Origin, X-Requested-With, Content-Type, Accept"
444+
);
441445

442-
try {
443-
// 将 Base64 数据写入临时文件
444-
const buffer = Buffer.from(base64Data, "base64");
445-
fs.writeFileSync(tempFileName, buffer);
446-
// 发送文件作为响应
447-
res.set("Content-Type", "audio/mpeg"); // 设置正确的内容类型
448-
res.sendFile(tempFileName, (err) => {
449-
if (err) {
450-
console.error("Error sending file:", err);
451-
}
452-
fs.unlink(tempFileName, (unlinkErr) => {
453-
if (unlinkErr) console.error("Error deleting file:", unlinkErr);
454-
});
455-
});
456-
} catch (error) {
457-
console.error("Error processing audio:", error);
458-
return res
459-
.status(500)
460-
.json({ success: false, error: "Error processing audio." });
461-
}
446+
// 将base64数据转换为二进制并发送
447+
const buffer = Buffer.from(bot.recording, "base64");
448+
return res.send(buffer);
462449
});
463450

464451
app.post(API_BASE_PATH + "/mp3", async (req, res) => {
@@ -842,6 +829,17 @@ async function get_api_server(proxy_utils) {
842829
.end();
843830
}
844831
);
832+
833+
// 添加CORS头部支持
834+
app.use((req, res, next) => {
835+
res.header("Access-Control-Allow-Origin", "*");
836+
res.header(
837+
"Access-Control-Allow-Headers",
838+
"Origin, X-Requested-With, Content-Type, Accept"
839+
);
840+
next();
841+
});
842+
845843
/*
846844
* Handle JSON Schema errors
847845
*/

database.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var sequelize = new Sequelize(
1111
process.env.DATABASE_PASSWORD,
1212
{
1313
host: process.env.DATABASE_HOST,
14+
port: process.env.DATABASE_PORT || 5432,
1415
dialect: "postgres",
1516
benchmark: true,
1617
logging: false,

0 commit comments

Comments
 (0)