main.js 4.3 KB

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