123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- const fs = require('fs');
- const express = require('express');
- const app = express();
- const app_serve = express();
- const mysql = require('mysql2')
- const pool = mysql.createPool({
- keepAliveInitialDelay: 10000,
- enableKeepAlive: true,
- })
- const db = mysql.createConnection({
- host: 'mc.andyxie.cn', user: 'instrunet', password: 'Moyingren2015', database: "instrunet_data", pool: pool,
- })
- const bodyParser = require("body-parser");
- const nrc = require('node-run-cmd')
- const https = require("node:https");
- app.use(bodyParser.json({"limit": "200mb"}));
- app.use(express.json());
- app_serve.use(bodyParser.json({"limit": "200mb"}));
- app_serve.use(express.json());
- app.post('/submit', function (req, res) {
- let uuid = crypto.randomUUID()
- db.connect(function (err) {
- console.log(err);
- })
- res.header("Access-Control-Allow-Origin", "*");
- try {
- fetch(req.body.file).then(res => {
- res.arrayBuffer().then(r => fs.writeFileSync("./"+uuid, Buffer.from(r)));
- })
- res.end("api_success");
- } catch (err) {
- res.status(500).send("Server Error");
- return
- }
- const callback = function (d) {
- console.log(d.toString());
- }
- const errcb = function (d) {
- console.log(d.toString());
- }
- let kind_of = [];
- switch (req.body.kind) {
- case 0:
- 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`
- kind_of[1] = `./output/${uuid}_(Instrumental)_UVR-MDX-NET-Inst_HQ_5.mp3`
- kind_of[2] = `${uuid}_(Instrumental)_UVR-MDX-NET-Inst_HQ_5.mp3`
- break;
- case 1:
- 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`
- kind_of[1] = `./output/${uuid}_(Instrumental)_UVR_MDXNET_KARA.mp3`
- kind_of[2] = `${uuid}_(Instrumental)_UVR_MDXNET_KARA.mp3`
- break;
- }
- nrc.run(["pip install audio-separator", kind_of[0]], {
- onData: callback,
- onError: errcb
- }).then(() => {
- 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])
- db.unprepare()
- fs.rm("output/"+kind_of[2], {
- recursive: true,
- }, (err) => {
- console.log(err);
- })
- fs.rm(uuid, (err) => {
- console.log(err);
- })
- })
- })
- app.options('/submit', function (req, res) {
- res.header("Access-Control-Allow-Origin", "*");
- res.header("Access-Control-Allow-Headers", "Content-Type");
- res.end()
- })
- app.post('/search_api', function (req, res) {
- function OnFetched(err, rows) {
- try {
- res.header("Access-Control-Allow-Origin", "*");
- res.end(JSON.stringify(rows));
- } catch (e) {
- }
- }
- db.execute(`SELECT uuid, song_name, album_name, artist, kind
- FROM instrunet_entry
- WHERE song_name like '%${req.body.searchStr}%'
- or album_name like '%${req.body.searchStr}%'
- or artist like '%${req.body.searchStr}%'`, OnFetched)
- db.unprepare()
- })
- app.options('/search_api', function (req, res) {
- res.header("Access-Control-Allow-Origin", "*");
- res.header("Access-Control-Allow-Headers", "Content-Type");
- res.end()
- })
- app_serve.get('/:uuid', function (req, res) {
- async function Provider(err, rows) {
- try {
- res.header("Access-Control-Allow-Origin", "*");
- /** @type {ArrayBuffer}*/
- let buffer = rows[0].databinary
- res.setHeader('Content-Length', buffer.byteLength);
- db.execute(`SELECT song_name
- FROM instrunet_entry
- WHERE uuid = '${req.params.uuid}'`, (err, row) => {
- res.setHeader('Content-Disposition', 'attachment; filename=' + encodeURI(row[0].song_name) + '.mp3');
- res.send(buffer)
- res.end()
- })
- db.unprepare()
- } catch (e) {
- console.log(e)
- console.log("Triggered err");
- }
- }
- db.execute(`SELECT databinary
- FROM instrunet_entry
- WHERE uuid = '${req.params.uuid}'`, Provider)
- db.unprepare()
- })
- https.createServer({
- key: fs.readFileSync('andyxie.cn.key'),
- cert: fs.readFileSync('andyxie.cn.pem')
- }, app).listen(8080)
- // // app.listen(8080)
- // app_serve.listen(8079)
- https.createServer({
- key: fs.readFileSync('andyxie.cn.key'),
- cert: fs.readFileSync('andyxie.cn.pem')
- }, app_serve).listen(8079)
|