mirror of
https://github.com/NeteaseCloudMusicApiEnhanced/api-enhanced.git
synced 2026-03-21 11:03:15 +00:00
fix(cloud): 解决临时文件清理和上传流程中的错误处理问题
- 修复了临时文件清理失败时的日志消息本地化 - 移除了重复的临时文件清理调用,统一在 finally 块中处理 - 优化了错误处理逻辑,确保在各种异常情况下正确返回错误响应 - 更新了日志消息为中文描述,提高可读性 - 修复了上传插件中的日志消息本地化问题 - 统一了错误处理流程,避免重复的清理操作
This commit is contained in:
parent
8951e32a0e
commit
4087bafd7d
291
module/cloud.js
291
module/cloud.js
@ -37,168 +37,167 @@ module.exports = async (query, request) => {
|
||||
try {
|
||||
await fs.promises.unlink(query.songFile.tempFilePath)
|
||||
} catch (e) {
|
||||
logger.info('Temp file cleanup failed:', e.message)
|
||||
logger.info('临时文件清理失败:', e.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (useTempFile) {
|
||||
try {
|
||||
if (useTempFile) {
|
||||
try {
|
||||
const stats = await fs.promises.stat(query.songFile.tempFilePath)
|
||||
fileSize = stats.size
|
||||
} catch (e) {
|
||||
logger.error('获取临时文件状态失败:', e.message)
|
||||
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')
|
||||
const stream = fs.createReadStream(query.songFile.tempFilePath)
|
||||
stream.on('data', (chunk) => hash.update(chunk))
|
||||
stream.on('end', () => resolve(hash.digest('hex')))
|
||||
stream.on('error', reject)
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if (!fileMd5) {
|
||||
fileMd5 = crypto.createHash('md5').update(query.songFile.data).digest('hex')
|
||||
}
|
||||
fileSize = query.songFile.data.byteLength
|
||||
}
|
||||
|
||||
query.songFile.md5 = fileMd5
|
||||
query.songFile.size = fileSize
|
||||
|
||||
const res = await request(
|
||||
`/api/cloud/upload/check`,
|
||||
{
|
||||
bitrate: String(bitrate),
|
||||
ext: '',
|
||||
length: fileSize,
|
||||
md5: fileMd5,
|
||||
songId: '0',
|
||||
version: 1,
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
let artist = ''
|
||||
let album = ''
|
||||
let songName = ''
|
||||
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()
|
||||
let metadata
|
||||
if (useTempFile) {
|
||||
metadata = await mm.parseFile(query.songFile.tempFilePath)
|
||||
} else {
|
||||
metadata = await mm.parseBuffer(
|
||||
query.songFile.data,
|
||||
query.songFile.mimetype,
|
||||
)
|
||||
}
|
||||
const info = metadata.common
|
||||
|
||||
if (info.title) {
|
||||
songName = info.title
|
||||
}
|
||||
if (info.album) {
|
||||
album = info.album
|
||||
}
|
||||
if (info.artist) {
|
||||
artist = info.artist
|
||||
}
|
||||
} catch (error) {
|
||||
logger.info('元数据解析错误:', error.message)
|
||||
}
|
||||
const tokenRes = await request(
|
||||
`/api/nos/token/alloc`,
|
||||
{
|
||||
bucket: '',
|
||||
ext: ext,
|
||||
filename: filename,
|
||||
local: false,
|
||||
nos_product: 3,
|
||||
type: 'audio',
|
||||
md5: fileMd5,
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
|
||||
if (!tokenRes.body.result || !tokenRes.body.result.resourceId) {
|
||||
logger.error('Token分配失败:', tokenRes.body)
|
||||
return Promise.reject({
|
||||
status: 500,
|
||||
body: {
|
||||
code: 500,
|
||||
msg: '临时文件访问失败',
|
||||
detail: e.message,
|
||||
msg: '获取上传token失败',
|
||||
detail: tokenRes.body,
|
||||
},
|
||||
})
|
||||
}
|
||||
if (!fileMd5) {
|
||||
fileMd5 = await new Promise((resolve, reject) => {
|
||||
const hash = crypto.createHash('md5')
|
||||
const stream = fs.createReadStream(query.songFile.tempFilePath)
|
||||
stream.on('data', (chunk) => hash.update(chunk))
|
||||
stream.on('end', () => resolve(hash.digest('hex')))
|
||||
stream.on('error', reject)
|
||||
|
||||
if (res.body.needUpload) {
|
||||
logger.info('需要上传,开始上传流程...')
|
||||
try {
|
||||
const uploadInfo = await uploadPlugin(query, request)
|
||||
logger.info('上传完成:', uploadInfo?.body?.result?.resourceId)
|
||||
} catch (uploadError) {
|
||||
logger.error('上传失败:', uploadError)
|
||||
return Promise.reject(uploadError)
|
||||
}
|
||||
} else {
|
||||
logger.info('文件已存在,跳过上传')
|
||||
}
|
||||
|
||||
const res2 = await request(
|
||||
`/api/upload/cloud/info/v2`,
|
||||
{
|
||||
md5: fileMd5,
|
||||
songid: res.body.songId,
|
||||
filename: query.songFile.name,
|
||||
song: songName || filename,
|
||||
album: album || '未知专辑',
|
||||
artist: artist || '未知艺术家',
|
||||
bitrate: String(bitrate),
|
||||
resourceId: tokenRes.body.result.resourceId,
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
|
||||
if (res2.body.code !== 200) {
|
||||
logger.error('云盘信息上传失败:', res2.body)
|
||||
return Promise.reject({
|
||||
status: res2.status || 500,
|
||||
body: {
|
||||
code: res2.body.code || 500,
|
||||
msg: res2.body.msg || '上传云盘信息失败',
|
||||
detail: res2.body,
|
||||
},
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if (!fileMd5) {
|
||||
fileMd5 = crypto.createHash('md5').update(query.songFile.data).digest('hex')
|
||||
}
|
||||
fileSize = query.songFile.data.byteLength
|
||||
}
|
||||
|
||||
query.songFile.md5 = fileMd5
|
||||
query.songFile.size = fileSize
|
||||
|
||||
const res = await request(
|
||||
`/api/cloud/upload/check`,
|
||||
{
|
||||
bitrate: String(bitrate),
|
||||
ext: '',
|
||||
length: fileSize,
|
||||
md5: fileMd5,
|
||||
songId: '0',
|
||||
version: 1,
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
let artist = ''
|
||||
let album = ''
|
||||
let songName = ''
|
||||
try {
|
||||
let metadata
|
||||
if (useTempFile) {
|
||||
metadata = await mm.parseFile(query.songFile.tempFilePath)
|
||||
} else {
|
||||
metadata = await mm.parseBuffer(
|
||||
query.songFile.data,
|
||||
query.songFile.mimetype,
|
||||
)
|
||||
const res3 = await request(
|
||||
`/api/cloud/pub/v2`,
|
||||
{
|
||||
songid: res2.body.songId,
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
...res.body,
|
||||
...res3.body,
|
||||
},
|
||||
cookie: res.cookie,
|
||||
}
|
||||
const info = metadata.common
|
||||
|
||||
if (info.title) {
|
||||
songName = info.title
|
||||
}
|
||||
if (info.album) {
|
||||
album = info.album
|
||||
}
|
||||
if (info.artist) {
|
||||
artist = info.artist
|
||||
}
|
||||
} catch (error) {
|
||||
logger.info('metadata parse error:', error.message)
|
||||
}
|
||||
const tokenRes = await request(
|
||||
`/api/nos/token/alloc`,
|
||||
{
|
||||
bucket: '',
|
||||
ext: ext,
|
||||
filename: filename,
|
||||
local: false,
|
||||
nos_product: 3,
|
||||
type: 'audio',
|
||||
md5: fileMd5,
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
|
||||
if (!tokenRes.body.result || !tokenRes.body.result.resourceId) {
|
||||
logger.error('Token allocation failed:', tokenRes.body)
|
||||
} finally {
|
||||
await cleanupTempFile()
|
||||
return Promise.reject({
|
||||
status: 500,
|
||||
body: {
|
||||
code: 500,
|
||||
msg: '获取上传token失败',
|
||||
detail: tokenRes.body,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if (res.body.needUpload) {
|
||||
logger.info('Need upload, starting upload process...')
|
||||
try {
|
||||
const uploadInfo = await uploadPlugin(query, request)
|
||||
logger.info('Upload completed:', uploadInfo?.body?.result?.resourceId)
|
||||
} catch (uploadError) {
|
||||
logger.error('Upload failed:', uploadError)
|
||||
await cleanupTempFile()
|
||||
return Promise.reject(uploadError)
|
||||
}
|
||||
} else {
|
||||
logger.info('File already exists, skip upload')
|
||||
}
|
||||
|
||||
await cleanupTempFile()
|
||||
|
||||
const res2 = await request(
|
||||
`/api/upload/cloud/info/v2`,
|
||||
{
|
||||
md5: fileMd5,
|
||||
songid: res.body.songId,
|
||||
filename: query.songFile.name,
|
||||
song: songName || filename,
|
||||
album: album || '未知专辑',
|
||||
artist: artist || '未知艺术家',
|
||||
bitrate: String(bitrate),
|
||||
resourceId: tokenRes.body.result.resourceId,
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
|
||||
if (res2.body.code !== 200) {
|
||||
logger.error('Cloud info upload failed:', res2.body)
|
||||
return Promise.reject({
|
||||
status: res2.status || 500,
|
||||
body: {
|
||||
code: res2.body.code || 500,
|
||||
msg: res2.body.msg || '上传云盘信息失败',
|
||||
detail: res2.body,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const res3 = await request(
|
||||
`/api/cloud/pub/v2`,
|
||||
{
|
||||
songid: res2.body.songId,
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
...res.body,
|
||||
...res3.body,
|
||||
},
|
||||
cookie: res.cookie,
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,7 +27,7 @@ module.exports = async (query, request) => {
|
||||
)
|
||||
|
||||
if (!tokenRes.body.result || !tokenRes.body.result.objectKey) {
|
||||
logger.error('Token allocation failed:', tokenRes.body)
|
||||
logger.error('Token分配失败:', tokenRes.body)
|
||||
throw {
|
||||
status: 500,
|
||||
body: {
|
||||
@ -49,7 +49,7 @@ module.exports = async (query, request) => {
|
||||
})
|
||||
).data
|
||||
} catch (error) {
|
||||
logger.error('LBS fetch failed:', error.message)
|
||||
logger.error('LBS获取失败:', error.message)
|
||||
throw {
|
||||
status: 500,
|
||||
body: {
|
||||
@ -61,7 +61,7 @@ module.exports = async (query, request) => {
|
||||
}
|
||||
|
||||
if (!lbs || !lbs.upload || !lbs.upload[0]) {
|
||||
logger.error('Invalid LBS response:', lbs)
|
||||
logger.error('无效的LBS响应:', lbs)
|
||||
throw {
|
||||
status: 500,
|
||||
body: {
|
||||
@ -95,9 +95,9 @@ module.exports = async (query, request) => {
|
||||
maxBodyLength: Infinity,
|
||||
timeout: 300000,
|
||||
})
|
||||
logger.info('Upload success:', filename)
|
||||
logger.info('上传成功:', filename)
|
||||
} catch (error) {
|
||||
logger.error('Upload failed:', {
|
||||
logger.error('上传失败:', {
|
||||
status: error.response?.status,
|
||||
data: error.response?.data,
|
||||
message: error.message,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user