mirror of
https://github.com/NeteaseCloudMusicApiEnhanced/api-enhanced.git
synced 2026-03-21 11:03:15 +00:00
fix(cloud): 解决云上传功能中的错误处理和代码清理
- 移除注释掉的废弃代码和调试信息 - 添加详细的错误处理和日志记录 - 验证token分配结果并处理失败情况 - 在上传过程中添加异常捕获和错误抛出 - 验证LBS响应的有效性并处理网络请求超时 - 改进上传流程的错误处理机制
This commit is contained in:
parent
4489f10f63
commit
26d55255e0
@ -6,9 +6,6 @@ let mm
|
||||
module.exports = async (query, request) => {
|
||||
mm = require('music-metadata')
|
||||
let ext = 'mp3'
|
||||
// if (query.songFile.name.indexOf('flac') > -1) {
|
||||
// ext = 'flac'
|
||||
// }
|
||||
if (query.songFile.name.includes('.')) {
|
||||
ext = query.songFile.name.split('.').pop()
|
||||
}
|
||||
@ -30,7 +27,6 @@ module.exports = async (query, request) => {
|
||||
})
|
||||
}
|
||||
if (!query.songFile.md5) {
|
||||
// 命令行上传没有md5和size信息,需要填充
|
||||
query.songFile.md5 = md5(query.songFile.data)
|
||||
query.songFile.size = query.songFile.data.byteLength
|
||||
}
|
||||
@ -65,32 +61,8 @@ module.exports = async (query, request) => {
|
||||
if (info.artist) {
|
||||
artist = info.artist
|
||||
}
|
||||
// if (metadata.native.ID3v1) {
|
||||
// metadata.native.ID3v1.forEach((item) => {
|
||||
// // logger.info(item.id, item.value)
|
||||
// if (item.id === 'title') {
|
||||
// songName = item.value
|
||||
// }
|
||||
// if (item.id === 'artist') {
|
||||
// artist = item.value
|
||||
// }
|
||||
// if (item.id === 'album') {
|
||||
// album = item.value
|
||||
// }
|
||||
// })
|
||||
// // logger.info({
|
||||
// // songName,
|
||||
// // album,
|
||||
// // songName,
|
||||
// // })
|
||||
// }
|
||||
// logger.info({
|
||||
// songName,
|
||||
// album,
|
||||
// songName,
|
||||
// })
|
||||
} catch (error) {
|
||||
logger.info(error)
|
||||
logger.info('metadata parse error:', error.message)
|
||||
}
|
||||
const tokenRes = await request(
|
||||
`/api/nos/token/alloc`,
|
||||
@ -106,11 +78,31 @@ module.exports = async (query, request) => {
|
||||
createOption(query),
|
||||
)
|
||||
|
||||
if (res.body.needUpload) {
|
||||
const uploadInfo = await uploadPlugin(query, request)
|
||||
// logger.info('uploadInfo', uploadInfo.body.result.resourceId)
|
||||
if (!tokenRes.body.result || !tokenRes.body.result.resourceId) {
|
||||
logger.error('Token allocation failed:', tokenRes.body)
|
||||
return Promise.reject({
|
||||
status: 500,
|
||||
body: {
|
||||
code: 500,
|
||||
msg: '获取上传token失败',
|
||||
detail: tokenRes.body,
|
||||
},
|
||||
})
|
||||
}
|
||||
// logger.info(tokenRes.body.result)
|
||||
|
||||
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)
|
||||
return Promise.reject(uploadError)
|
||||
}
|
||||
} else {
|
||||
logger.info('File already exists, skip upload')
|
||||
}
|
||||
|
||||
const res2 = await request(
|
||||
`/api/upload/cloud/info/v2`,
|
||||
{
|
||||
@ -125,8 +117,19 @@ module.exports = async (query, request) => {
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
// logger.info({ res2, privateCloud: res2.body.privateCloud })
|
||||
// logger.info(res.body.songId, 'songid')
|
||||
|
||||
if (res2.body.code !== 200 && 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`,
|
||||
{
|
||||
@ -134,13 +137,11 @@ module.exports = async (query, request) => {
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
// logger.info({ res3 })
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
...res.body,
|
||||
...res3.body,
|
||||
// ...uploadInfo,
|
||||
},
|
||||
cookie: res.cookie,
|
||||
}
|
||||
|
||||
@ -3,9 +3,6 @@ const createOption = require('../util/option.js')
|
||||
const logger = require('../util/logger.js')
|
||||
module.exports = async (query, request) => {
|
||||
let ext = 'mp3'
|
||||
// if (query.songFile.name.indexOf('flac') > -1) {
|
||||
// ext = 'flac'
|
||||
// }
|
||||
if (query.songFile.name.includes('.')) {
|
||||
ext = query.songFile.name.split('.').pop()
|
||||
}
|
||||
@ -14,7 +11,6 @@ module.exports = async (query, request) => {
|
||||
.replace(/\s/g, '')
|
||||
.replace(/\./g, '_')
|
||||
const bucket = 'jd-musicrep-privatecloud-audio-public'
|
||||
// 获取key和token
|
||||
const tokenRes = await request(
|
||||
`/api/nos/token/alloc`,
|
||||
{
|
||||
@ -29,15 +25,53 @@ module.exports = async (query, request) => {
|
||||
createOption(query, 'weapi'),
|
||||
)
|
||||
|
||||
// 上传
|
||||
if (!tokenRes.body.result || !tokenRes.body.result.objectKey) {
|
||||
logger.error('Token allocation failed:', tokenRes.body)
|
||||
throw {
|
||||
status: 500,
|
||||
body: {
|
||||
code: 500,
|
||||
msg: '获取上传token失败',
|
||||
detail: tokenRes.body,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const objectKey = tokenRes.body.result.objectKey.replace('/', '%2F')
|
||||
let lbs
|
||||
try {
|
||||
const lbs = (
|
||||
lbs = (
|
||||
await axios({
|
||||
method: 'get',
|
||||
url: `https://wanproxy.127.net/lbs?version=1.0&bucketname=${bucket}`,
|
||||
timeout: 10000,
|
||||
})
|
||||
).data
|
||||
} catch (error) {
|
||||
logger.error('LBS fetch failed:', error.message)
|
||||
throw {
|
||||
status: 500,
|
||||
body: {
|
||||
code: 500,
|
||||
msg: '获取上传服务器地址失败',
|
||||
detail: error.message,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if (!lbs || !lbs.upload || !lbs.upload[0]) {
|
||||
logger.error('Invalid LBS response:', lbs)
|
||||
throw {
|
||||
status: 500,
|
||||
body: {
|
||||
code: 500,
|
||||
msg: '获取上传服务器地址无效',
|
||||
detail: lbs,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await axios({
|
||||
method: 'post',
|
||||
url: `${lbs.upload[0]}/${bucket}/${objectKey}?offset=0&complete=true&version=1.0`,
|
||||
@ -50,10 +84,23 @@ module.exports = async (query, request) => {
|
||||
data: query.songFile.data,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity,
|
||||
timeout: 300000,
|
||||
})
|
||||
logger.info('Upload success:', filename)
|
||||
} catch (error) {
|
||||
logger.info('error', error.response)
|
||||
throw error.response
|
||||
logger.error('Upload failed:', {
|
||||
status: error.response?.status,
|
||||
data: error.response?.data,
|
||||
message: error.message,
|
||||
})
|
||||
throw {
|
||||
status: error.response?.status || 500,
|
||||
body: {
|
||||
code: error.response?.status || 500,
|
||||
msg: '文件上传失败',
|
||||
detail: error.response?.data || error.message,
|
||||
},
|
||||
}
|
||||
}
|
||||
return {
|
||||
...tokenRes,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user