main.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. break;
  47. case 1:
  48. 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`
  49. kind_of[1] = `./output/${uuid}_(Instrumental)_UVR_MDXNET_KARA.mp3`
  50. break;
  51. }
  52. nrc.run(["pip install audio-separator", kind_of[0]], {
  53. onData: callback,
  54. onError: errcb
  55. }).then(() => {
  56. 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])
  57. db.unprepare()
  58. fs.rm("output/"+uuid, {
  59. recursive: true,
  60. }, (err) => {
  61. console.log(err);
  62. })
  63. fs.rm(uuid, (err) => {
  64. console.log(err);
  65. })
  66. })
  67. })
  68. app.options('/submit', function (req, res) {
  69. res.header("Access-Control-Allow-Origin", "*");
  70. res.header("Access-Control-Allow-Headers", "Content-Type");
  71. res.end()
  72. })
  73. app.post('/search_api', function (req, res) {
  74. function OnFetched(err, rows) {
  75. try {
  76. res.header("Access-Control-Allow-Origin", "*");
  77. res.end(JSON.stringify(rows));
  78. } catch (e) {
  79. }
  80. }
  81. db.execute(`SELECT uuid, song_name, album_name, artist, kind
  82. FROM instrunet_entry
  83. WHERE song_name like '%${req.body.searchStr}%'
  84. or album_name like '%${req.body.searchStr}%'
  85. or artist like '%${req.body.searchStr}%'`, OnFetched)
  86. db.unprepare()
  87. })
  88. app.options('/search_api', function (req, res) {
  89. res.header("Access-Control-Allow-Origin", "*");
  90. res.header("Access-Control-Allow-Headers", "Content-Type");
  91. res.end()
  92. })
  93. app_serve.get('/:uuid', function (req, res) {
  94. async function Provider(err, rows) {
  95. try {
  96. res.header("Access-Control-Allow-Origin", "*");
  97. /** @type {ArrayBuffer}*/
  98. let buffer = rows[0].databinary
  99. res.setHeader('Content-Length', buffer.byteLength);
  100. db.execute(`SELECT song_name
  101. FROM instrunet_entry
  102. WHERE uuid = '${req.params.uuid}'`, (err, row) => {
  103. res.setHeader('Content-Disposition', 'attachment; filename=' + encodeURI(row[0].song_name) + '.mp3');
  104. res.send(buffer)
  105. res.end()
  106. })
  107. db.unprepare()
  108. } catch (e) {
  109. console.log(e)
  110. console.log("Triggered err");
  111. }
  112. }
  113. db.execute(`SELECT databinary
  114. FROM instrunet_entry
  115. WHERE uuid = '${req.params.uuid}'`, Provider)
  116. db.unprepare()
  117. })
  118. https.createServer({
  119. key: fs.readFileSync('andyxie.cn.key'),
  120. cert: fs.readFileSync('andyxie.cn.pem')
  121. }, app).listen(8080)
  122. // app.listen(8080)
  123. app_serve.listen(8079)