feat: 补充播客上传声音接口 #1789

This commit is contained in:
binaryify 2023-08-24 16:38:10 +08:00
parent d7cf9d1c2f
commit 954093864d
9 changed files with 178 additions and 9 deletions

66
module/audio_upload.js Normal file
View File

@ -0,0 +1,66 @@
const { default: axios } = require('axios')
var xml2js = require('xml2js')
var parser = new xml2js.Parser(/* options */)
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, '_')
query.cookie.os = 'pc'
query.cookie.appver = '2.9.7'
if (!query.songFile) {
return Promise.reject({
status: 500,
body: {
msg: '请上传音乐文件',
code: 500,
},
})
}
const tokenRes = await request(
'POST',
`https://music.163.com/weapi/nos/token/alloc`,
{
bucket: 'ymusic',
ext: ext,
filename: filename,
local: false,
nos_product: 0,
type: 'other',
},
{ crypto: 'weapi', cookie: query.cookie, proxy: query.proxy },
)
const objectKey = tokenRes.body.result.objectKey.replace('/', '%2F')
const res = await axios({
method: 'post',
url: `https://ymusic.nos-hz.163yun.com/${objectKey}?uploads`,
headers: {
'x-nos-token': tokenRes.body.result.token,
'Content-Type': 'audio/mpeg',
},
data: null,
})
// return xml
const res2 = await parser.parseStringPromise(res.data)
await axios({
method: 'put',
url: `https://ymusic.nos-hz.163yun.com/${objectKey}?partNumber=1&uploadId=${res2.InitiateMultipartUploadResult.UploadId[0]}`,
headers: {
'x-nos-token': tokenRes.body.result.token,
},
data: query.songFile.data,
})
return {
status: 200,
body: {
code: 200,
},
}
}

View File

@ -72,6 +72,7 @@
"qrcode": "^1.4.4",
"safe-decode-uri-component": "^1.2.1",
"tunnel": "^0.0.6",
"xml2js": "^0.6.2",
"yargs": "^17.1.1"
},
"devDependencies": {

84
public/audio_upload.html Normal file
View File

@ -0,0 +1,84 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>播客上传</title>
</head>
<body>
<input id="file" type="file" multiple />
<script src="https://cdn.jsdelivr.net/npm/axios@0.26.1/dist/axios.min.js"></script>
<script>
const phone = '' // 这里填手机号
const password = '' // 这里填密码
const fileUpdateTime = {}
let fileLength = 0
let cookieToken = ''
if (!phone || !password) {
const msg = '请设置你的手机号码和密码'
alert(msg)
throw new Error(msg)
}
login()
main()
async function login() {
const res = await axios({
url: `/login/cellphone?phone=${phone}&password=${encodeURIComponent(
password,
)}`,
withCredentials: true, //跨域的话必须设置
})
cookieToken = res.data.cookie
}
function main() {
document
.querySelector('input[type="file"]')
.addEventListener('change', function (e) {
console.log(this.files)
let currentIndx = 0
fileLength = this.files.length
for (const item of this.files) {
currentIndx += 1
upload(item, currentIndx)
}
})
}
function upload(file, currentIndx) {
var formData = new FormData()
formData.append('songFile', file)
axios({
method: 'post',
url: `/audio/upload?time=${Date.now()}&cookie=${cookieToken}`,
headers: {
'Content-Type': 'multipart/form-data',
},
data: formData,
})
.then((res) => {
console.log(`${file.name} 上传成功`)
if (currentIndx >= fileLength) {
console.log('上传完毕')
}
})
.catch(async (err) => {
console.log(err)
console.log(fileUpdateTime)
fileUpdateTime[file.name]
? (fileUpdateTime[file.name] += 1)
: (fileUpdateTime[file.name] = 1)
if (fileUpdateTime[file.name] >= 4) {
console.error(`上传失败:${file.name}`)
return
} else {
console.error(`${file.name} 失败 ${fileUpdateTime[file.name]} 次`)
}
// await login()
upload(file, currentIndx)
})
}
</script>
</body>
</html>

View File

@ -34,14 +34,14 @@
)
const res = await axios({
url: `/user/detail?uid=32953014&timestamp=${Date.now()}`,
withCredentials: true, //关键
withCredentials: true, //跨域的话必须设置
})
document.querySelector('#avatar').src = res.data.profile.avatarUrl
}
async function login() {
const res = await axios({
url: `/login/cellphone?phone=${phone}&password=${password}`,
withCredentials: true, //关键
withCredentials: true, //跨域的话必须设置
})
cookieToken = res.data.cookie
}

View File

@ -27,7 +27,7 @@
async function login() {
const res = await axios({
url: `/login/cellphone?phone=${phone}&password=${encodeURIComponent(password)}`,
withCredentials: true, //关键
withCredentials: true, //跨域的话必须设置
})
cookieToken = res.data.cookie
}

View File

@ -48,7 +48,7 @@
async function login() {
const res = await axios({
url: `/login/cellphone?phone=${phone}&password=${password}`,
withCredentials: true, //关键
withCredentials: true, //跨域的话必须设置
})
cookieToken = res.data.cookie
}

View File

@ -23,14 +23,14 @@
$.ajax({
url: `/login/cellphone?phone=${phone}&password=${password}`,
xhrFields: {
withCredentials: true, //关键
withCredentials: true, //跨域的话必须设置
},
success: function (data) {
console.log(data)
$.ajax({
url: `/recommend/resource `,
xhrFields: {
withCredentials: true, //关键
withCredentials: true, //跨域的话必须设置
},
success: function (data) {
console.log(data)
@ -47,12 +47,12 @@
axios({
url: `/login/cellphone?phone=${phone}&password=${password}`,
withCredentials: true, //关键
withCredentials: true, //跨域的话必须设置
}).then(function (res) {
console.log(res.data)
axios({
url: `/recommend/resource`,
withCredentials: true, //关键
withCredentials: true, //跨域的话必须设置
}).then(function (res) {
console.log(res.data)
})

View File

@ -1,5 +1,5 @@
{
"anonymous_token": "bf8bfeabb1aa84f9c8c3906c04a04fb864322804c83f5d607e91a04eae463c9436bd1a17ec353cf780b396507a3f7464e8a60f4bbc019437993166e004087dd32d1490298caf655c2353e58daa0bc13cc7d5c198250968580b12c1b8817e3f5c807e650dd04abd3fb8130b7ae43fcc5b",
"anonymous_token": "de91e1f8119d32e01cc73efcb82c0a30c9137e8d4f88dbf5e3d7bf3f28998f21add2bc8204eeee5e56c0bbb8743574b46ca2c10c35dc172199bef9bf4d60ecdeab066bb4dc737d1c3324751bcc9aaf44c3061cd18d77b7a0",
"resourceTypeMap": {
"0": "R_SO_4_",
"1": "R_MV_5_",

View File

@ -3109,6 +3109,11 @@ safe-decode-uri-component@^1.2.1:
resolved "https://registry.npm.taobao.org/safer-buffer/download/safer-buffer-2.1.2.tgz"
integrity sha1-RPoWGwGHuVSd2Eu5GAL5vYOFzWo=
sax@>=0.6.0:
version "1.2.4"
resolved "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
semver@^5.4.1:
version "5.7.1"
resolved "https://registry.npmmirror.com/semver/-/semver-5.7.1.tgz"
@ -3736,6 +3741,19 @@ wrappy@1:
resolved "https://registry.npm.taobao.org/wrappy/download/wrappy-1.0.2.tgz"
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=
xml2js@^0.6.2:
version "0.6.2"
resolved "https://registry.npmjs.org/xml2js/-/xml2js-0.6.2.tgz#dd0b630083aa09c161e25a4d0901e2b2a929b499"
integrity sha512-T4rieHaC1EXcES0Kxxj4JWgaUQHDk+qwHcYOCFHfiwKz7tOVPLq7Hjq9dM1WCMhylqMEfP7hMcOIChvotiZegA==
dependencies:
sax ">=0.6.0"
xmlbuilder "~11.0.0"
xmlbuilder@~11.0.0:
version "11.0.1"
resolved "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"
integrity sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==
xtend@^4.0.0:
version "4.0.2"
resolved "https://registry.npm.taobao.org/xtend/download/xtend-4.0.2.tgz"