var express = require('express'); var app = express(); var fs = require("fs"); const sqlite3 = require('sqlite3').verbose(); const db = new sqlite3.Database('./database.db'); const jwt = require("jsonwebtoken"); const bodyParser = require("body-parser"); const {spawn} = require("child_process"); const nrc = require('node-run-cmd') const https = require("node:https"); const {encode} = require("html-entities"); const querystring = require("node:querystring"); // var key = fs.readFileSync('./andyxie.cn.key') // var cert = fs.readFileSync('./andyxie.cn.pem') console.log(crypto.randomUUID()) app.use(bodyParser.json({"limit": "200mb"})); app.use(express.json()); const stmt = db.prepare("INSERT INTO instrument_entry (uuid, song_name, album_name, link_to, binary, artist) VALUES (?,?,?,?,?,?)") app.post('/submit', function (req, res) { res.header("Access-Control-Allow-Origin", "*"); try { fetch(req.body.file).then(res => { res.arrayBuffer().then(r => fs.writeFileSync("./audio_file", Buffer.from(r))); }) res.end("api_success"); } catch (err) { res.status(500).send("Server Error"); return } var callback = function (d) { console.log(d.toString()); } var errcb = function (d) { console.log(d.toString()); } nrc.run(["pip install audio-separator", "audio-separator ./audio_file --model_filename UVR_MDXNET_KARA.onnx --mdx_segment_size 4000 --mdx_overlap 0.85 --output_format MP3 --mdx_batch_size 20 --output_dir output "], { onData: callback, onError: errcb }).then(r => { stmt.run(crypto.randomUUID(), req.body.name, req.body.albumName, req.body.link, fs.readFileSync('./output/audio_file_(Instrumental)_UVR_MDXNET_KARA.mp3'), req.body.artist) }) }) 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", "*"); console.log("Triggered"); res.end(JSON.stringify(rows)); } catch (e) { } } console.log(req.body.searchStr) db.all(`SELECT id, uuid, song_name, album_name, artist FROM instrument_entry WHERE song_name like '%${req.body.searchStr}%' or album_name like '%${req.body.searchStr}%' or artist like '%${req.body.searchStr}%'`, OnFetched) }) app.get('/:uuid', function (req, res) { async function Provider(err, rows) { try { res.header("Access-Control-Allow-Origin", "*"); /** @type {ArrayBuffer}*/ var buffer = rows[0].binary res.setHeader('Content-Length', buffer.byteLength); db.all(`SELECT "song_name" FROM instrument_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() }) } catch (e) { console.log(e) console.log("Triggered err"); } } db.all(`SELECT "binary" FROM instrument_entry WHERE uuid = '${req.params.uuid}'`, Provider) }) app.post('/fetch', function (req, res) { function Provider(err, rows) { try { res.header("Access-Control-Allow-Origin", "*"); console.log(rows[0].binary) var str = URL.createObjectURL(new Blob(rows[0].binary)) console.log(str) res.end(JSON.stringify({data: Buffer.from(rows[0].binary).toString('base64')})); } catch (e) { console.log(e) console.log("Triggered err"); } } console.log(req.body.uuid) db.all(`SELECT "binary" FROM instrument_entry WHERE uuid = '${req.body.uuid}'`, Provider) }) app.options('/fetch', function (req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Content-Type"); res.end() }) app.options('/search_api', function (req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Content-Type"); res.end() }) app.listen(8080)