main.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. const fs = require('fs');
  2. const express = require('express');
  3. const app = express();
  4. const app_serve = express();
  5. const mysql = require('mysql2')
  6. const pool = mysql.createPool({
  7. keepAliveInitialDelay: 10000,
  8. enableKeepAlive: true,
  9. })
  10. const db = mysql.createConnection({
  11. host: 'mc.andyxie.cn', user: 'instrunet', password: 'Moyingren2015', database: "instrunet_data", pool: pool,
  12. })
  13. const bodyParser = require("body-parser");
  14. const nrc = require('node-run-cmd')
  15. const https = require("node:https");
  16. app.use(bodyParser.json({"limit": "200mb"}));
  17. app.use(express.json());
  18. app_serve.use(bodyParser.json({"limit": "200mb"}));
  19. app_serve.use(express.json());
  20. app.post('/submit', function (req, res) {
  21. let uuid = crypto.randomUUID()
  22. db.connect(function (err) {
  23. console.log(err);
  24. })
  25. res.header("Access-Control-Allow-Origin", "*");
  26. try {
  27. fetch(req.body.file).then(res => {
  28. res.arrayBuffer().then(r => fs.writeFileSync("./"+uuid, Buffer.from(r)));
  29. })
  30. res.end("api_success");
  31. } catch (err) {
  32. res.status(500).send("Server Error");
  33. return
  34. }
  35. const callback = function (d) {
  36. console.log(d.toString());
  37. }
  38. const errcb = function (d) {
  39. console.log(d.toString());
  40. }
  41. let kind_of = [];
  42. switch (req.body.kind) {
  43. case 0:
  44. kind_of[0] = `audio-separator ./${uuid} --model_filename UVR-MDX-NET-Inst_HQ_5.onnx --mdx_segment_size 4000 --mdx_overlap 0.75 --output_format MP3 --mdx_batch_size 200 --output_dir output`
  45. kind_of[1] = `./output/${uuid}_(Instrumental)_UVR-MDX-NET-Inst_HQ_5.mp3`
  46. kind_of[2] = `${uuid}_(Instrumental)_UVR-MDX-NET-Inst_HQ_5.mp3`
  47. break;
  48. case 1:
  49. kind_of[0] = `audio-separator ./${uuid} --model_filename UVR_MDXNET_KARA.onnx --mdx_segment_size 4000 --mdx_overlap 0.75 --output_format MP3 --mdx_batch_size 200 --output_dir output`
  50. kind_of[1] = `./output/${uuid}_(Instrumental)_UVR_MDXNET_KARA.mp3`
  51. kind_of[2] = `${uuid}_(Instrumental)_UVR_MDXNET_KARA.mp3`
  52. break;
  53. }
  54. nrc.run(["pip install audio-separator", kind_of[0]], {
  55. onData: callback,
  56. onError: errcb
  57. }).then(() => {
  58. db.execute(("INSERT INTO instrunet_entry (uuid, song_name, album_name, link_to, databinary, artist,kind) VALUES (?,?,?,?,?,?,?)"), [uuid, req.body.name, req.body.albumName, req.body.link, fs.readFileSync(kind_of[1]), req.body.artist, req.body.kind])
  59. db.unprepare()
  60. fs.rm("output/"+kind_of[2], {
  61. recursive: true,
  62. }, (err) => {
  63. console.log(err);
  64. })
  65. fs.rm(uuid, (err) => {
  66. console.log(err);
  67. })
  68. })
  69. })
  70. app.options('/submit', function (req, res) {
  71. res.header("Access-Control-Allow-Origin", "*");
  72. res.header("Access-Control-Allow-Headers", "Content-Type");
  73. res.end()
  74. })
  75. app.post('/search_api', function (req, res) {
  76. function OnFetched(err, rows) {
  77. try {
  78. res.header("Access-Control-Allow-Origin", "*");
  79. res.end(JSON.stringify(rows));
  80. } catch (e) {
  81. }
  82. }
  83. db.execute(`SELECT uuid, song_name, album_name, artist, kind
  84. FROM instrunet_entry
  85. WHERE song_name like '%${req.body.searchStr}%'
  86. or album_name like '%${req.body.searchStr}%'
  87. or artist like '%${req.body.searchStr}%'`, OnFetched)
  88. db.unprepare()
  89. })
  90. app.options('/search_api', function (req, res) {
  91. res.header("Access-Control-Allow-Origin", "*");
  92. res.header("Access-Control-Allow-Headers", "Content-Type");
  93. res.end()
  94. })
  95. app_serve.get('/:uuid', function (req, res) {
  96. async function Provider(err, rows) {
  97. try {
  98. res.header("Access-Control-Allow-Origin", "*");
  99. /** @type {ArrayBuffer}*/
  100. let buffer = rows[0].databinary
  101. res.setHeader('Content-Length', buffer.byteLength);
  102. db.execute(`SELECT song_name
  103. FROM instrunet_entry
  104. WHERE uuid = '${req.params.uuid}'`, (err, row) => {
  105. res.setHeader('Content-Disposition', 'attachment; filename=' + encodeURI(row[0].song_name) + '.mp3');
  106. res.send(buffer)
  107. res.end()
  108. })
  109. db.unprepare()
  110. } catch (e) {
  111. console.log(e)
  112. console.log("Triggered err");
  113. }
  114. }
  115. db.execute(`SELECT databinary
  116. FROM instrunet_entry
  117. WHERE uuid = '${req.params.uuid}'`, Provider)
  118. db.unprepare()
  119. })
  120. https.createServer({
  121. key: fs.readFileSync('andyxie.cn.key'),
  122. cert: fs.readFileSync('andyxie.cn.pem')
  123. }, app).listen(8080)
  124. // // app.listen(8080)
  125. // app_serve.listen(8079)
  126. https.createServer({
  127. key: fs.readFileSync('andyxie.cn.key'),
  128. cert: fs.readFileSync('andyxie.cn.pem')
  129. }, app_serve).listen(8079)