songUpload.js 1.6 KB

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