forked from Binaryify/NeteaseCloudMusicApi
-
Notifications
You must be signed in to change notification settings - Fork 17
/
songUpload.js
57 lines (56 loc) · 1.51 KB
/
songUpload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const { default: axios } = require('axios')
module.exports = async (query, request) => {
let ext = 'mp3'
if (query.songFile.name.indexOf('flac') > -1) {
ext = 'flac'
}
const filename = query.songFile.name
.replace('.' + ext, '')
.replace(/\s/g, '')
.replace(/\./g, '_')
const bucket = 'jd-musicrep-privatecloud-audio-public'
// 获取key和token
const tokenRes = await request(
'POST',
`https://music.163.com/weapi/nos/token/alloc`,
{
bucket: bucket,
ext: ext,
filename: filename,
local: false,
nos_product: 3,
type: 'audio',
md5: query.songFile.md5,
},
{ crypto: 'weapi', cookie: query.cookie, proxy: query.proxy },
)
// 上传
const objectKey = tokenRes.body.result.objectKey.replace('/', '%2F')
try {
const lbs = (
await axios({
method: 'get',
url: `https://wanproxy.127.net/lbs?version=1.0&bucketname=${bucket}`,
})
).data
await axios({
method: 'post',
url: `http://${lbs.upload[0]}/${bucket}/${objectKey}?offset=0&complete=true&version=1.0`,
headers: {
'x-nos-token': tokenRes.body.result.token,
'Content-MD5': query.songFile.md5,
'Content-Type': 'audio/mpeg',
'Content-Length': String(query.songFile.size),
},
data: query.songFile.data,
maxContentLength: Infinity,
maxBodyLength: Infinity,
})
} catch (error) {
console.log('error', error.response)
throw error.response
}
return {
...tokenRes,
}
}