mirror of
https://github.com/NeteaseCloudMusicApiEnhanced/api-enhanced.git
synced 2026-03-21 11:03:15 +00:00
fix(cloud): 解决临时文件清理和异步操作问题
- 添加异步临时文件清理函数避免资源泄露 - 将同步文件操作改为异步操作提高性能 - 在令牌分配失败时执行临时文件清理 - 在上传失败时确保临时文件被清理 - 使用 Promise 包装 MD5 计算操作 - 统一临时文件清理逻辑到 finally 块
This commit is contained in:
parent
2d6173b2aa
commit
33ccc83615
@ -32,9 +32,32 @@ module.exports = async (query, request) => {
|
||||
let fileSize = query.songFile.size
|
||||
let fileMd5 = query.songFile.md5
|
||||
|
||||
const cleanupTempFile = async () => {
|
||||
if (useTempFile) {
|
||||
const stats = fs.statSync(query.songFile.tempFilePath)
|
||||
try {
|
||||
await fs.promises.unlink(query.songFile.tempFilePath)
|
||||
} catch (e) {
|
||||
logger.info('Temp file cleanup failed:', e.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (useTempFile) {
|
||||
try {
|
||||
const stats = await fs.promises.stat(query.songFile.tempFilePath)
|
||||
fileSize = stats.size
|
||||
} catch (e) {
|
||||
logger.error('Failed to stat temp file:', e.message)
|
||||
await cleanupTempFile()
|
||||
return Promise.reject({
|
||||
status: 500,
|
||||
body: {
|
||||
code: 500,
|
||||
msg: '临时文件访问失败',
|
||||
detail: e.message,
|
||||
},
|
||||
})
|
||||
}
|
||||
if (!fileMd5) {
|
||||
fileMd5 = await new Promise((resolve, reject) => {
|
||||
const hash = crypto.createHash('md5')
|
||||
@ -46,7 +69,11 @@ module.exports = async (query, request) => {
|
||||
}
|
||||
} else {
|
||||
if (!fileMd5) {
|
||||
fileMd5 = crypto.createHash('md5').update(query.songFile.data).digest('hex')
|
||||
fileMd5 = await new Promise((resolve) => {
|
||||
setImmediate(() => {
|
||||
resolve(crypto.createHash('md5').update(query.songFile.data).digest('hex'))
|
||||
})
|
||||
})
|
||||
}
|
||||
fileSize = query.songFile.data.byteLength
|
||||
}
|
||||
@ -109,6 +136,7 @@ module.exports = async (query, request) => {
|
||||
|
||||
if (!tokenRes.body.result || !tokenRes.body.result.resourceId) {
|
||||
logger.error('Token allocation failed:', tokenRes.body)
|
||||
await cleanupTempFile()
|
||||
return Promise.reject({
|
||||
status: 500,
|
||||
body: {
|
||||
@ -126,19 +154,15 @@ module.exports = async (query, request) => {
|
||||
logger.info('Upload completed:', uploadInfo?.body?.result?.resourceId)
|
||||
} catch (uploadError) {
|
||||
logger.error('Upload failed:', uploadError)
|
||||
await cleanupTempFile()
|
||||
return Promise.reject(uploadError)
|
||||
} finally {
|
||||
if (useTempFile && fs.existsSync(query.songFile.tempFilePath)) {
|
||||
fs.unlinkSync(query.songFile.tempFilePath)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logger.info('File already exists, skip upload')
|
||||
if (useTempFile && fs.existsSync(query.songFile.tempFilePath)) {
|
||||
fs.unlinkSync(query.songFile.tempFilePath)
|
||||
}
|
||||
}
|
||||
|
||||
await cleanupTempFile()
|
||||
|
||||
const res2 = await request(
|
||||
`/api/upload/cloud/info/v2`,
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user