Browse Source

fixed wasted traffic -- cache. && bases and drums

AXCWG 4 months ago
parent
commit
8752841668
4 changed files with 115 additions and 22 deletions
  1. BIN
      .DS_Store
  2. 67 20
      main.js
  3. 45 1
      package-lock.json
  4. 3 1
      package.json

BIN
.DS_Store


+ 67 - 20
main.js

@@ -1,10 +1,11 @@
+const cron = require("node-cron");
 const fs = require('fs');
 const express = require('express');
 const app = express();
 const app_serve = express();
 const mysql = require('mysql2')
 const SendSeekable = require('send-seekable');
-
+const WebSocket = require('ws');
 const pool = mysql.createPool({
     keepAliveInitialDelay: 10000, enableKeepAlive: true,
 })
@@ -16,6 +17,8 @@ const nodemailer = require('nodemailer')
 const bodyParser = require("body-parser");
 const nrc = require('node-run-cmd')
 const https = require("node:https");
+const {WebSocketServer} = require("ws");
+const http = require("node:http");
 
 app.use(bodyParser.json({"limit": "200mb"}));
 app.use(express.json());
@@ -25,15 +28,14 @@ app_serve.use(express.json());
 app_serve.use(SendSeekable)
 
 const transporter = nodemailer.createTransport({
-    host: 'smtp.qq.com',
-    port: 465,
-    secure: true,
-    auth: {
-        user: '3095864740@qq.com',
-        pass: 'caemyuagapsadfff',
+    host: 'smtp.qq.com', port: 465, secure: true, auth: {
+        user: '3095864740@qq.com', pass: 'caemyuagapsadfff',
     }
 })
 
+
+
+
 app.post('/submit', function (req, res) {
     let uuid = crypto.randomUUID()
     db.connect(function (err) {
@@ -77,6 +79,19 @@ app.post('/submit', function (req, res) {
             kind_of[2] = `${uuid}_(Instrumental)_UVR_MDXNET_KARA.mp3`
             kind_of[3] = `./output/${uuid}_(Vocals)_UVR_MDXNET_KARA.mp3`
             break;
+        case 3:
+            kind_of[0] = `audio-separator ./${uuid} --model_filename kuielab_a_bass.onnx  --mdx_segment_size 4000 --mdx_overlap 0.75 --output_format mp3 --mdx_batch_size 300 --output_dir output`
+            kind_of[1] = `./output/${uuid}_(Bass)_kuielab_a_bass.mp3`
+            kind_of[2] = `${uuid}_(Bass)_kuielab_a_bass.mp3`
+            kind_of[3] = `./output/${uuid}_(No Bass)_kuielab_a_bass.mp3`
+            break;
+        case 4:
+            kind_of[0] = `audio-separator ./${uuid} --model_filename kuielab_a_drums.onnx  --mdx_segment_size 4000 --mdx_overlap 0.75 --output_format mp3 --mdx_batch_size 300 --output_dir output`
+            kind_of[1] = `./output/${uuid}_(Drums)_kuielab_a_drums.mp3`
+            kind_of[2] = `${uuid}_(Drums)_kuielab_a_drums.mp3`
+            kind_of[3] = `./output/${uuid}_(No Drums)_kuielab_a_drums.mp3`
+            break;
+
 
     }
     nrc.run([kind_of[0]], {
@@ -93,6 +108,12 @@ app.post('/submit', function (req, res) {
 
             }
         })
+        fs.rm(kind_of[3], (err) => {
+            if (err) {
+                console.log(err);
+
+            }
+        })
         fs.rm(uuid, (err) => {
             if (err) {
                 console.log(err);
@@ -108,10 +129,7 @@ app.post('/submit', function (req, res) {
                 to: req.body.email,
                 subject: "你的音频已处理完成。",
                 text: "你的音频已处理完成。",
-                html:
-                    "<h1>哈喽👋</h1><p>你在伴奏网网站上传的音频已经处理完毕,快来下载吧!<br/>下载链接:<br/><a href='https://andyxie.cn:8201/" + uuid + "'>这里</a><br/><br/>" +
-                    "与其他网站不一样,本邮件发送邮箱真的有个伞兵在后面!<br/>" +
-                    "请随意骚扰!</p>"
+                html: "<h1>哈喽👋</h1><p>你在伴奏网网站上传的音频已经处理完毕,快来下载吧!<br/>下载链接:<br/><a href='https://andyxie.cn:8201/" + uuid + "'>这里</a><br/><br/>" + "与其他网站不一样,本邮件发送邮箱真的有个伞兵在后面!<br/>" + "请随意骚扰!</p>"
             }).then((result) => {
                 console.log("Message sent: %s", result.messageId)
 
@@ -157,7 +175,7 @@ app.options('/search_api', function (req, res) {
 })
 app.get("/getSingle", function (req, res) {
     if (req.query.id) {
-        db.execute(`SELECT song_name, album_name, artist
+        db.execute(`SELECT song_name, album_name, artist, kind
                     FROM instrunet_entry
                     WHERE uuid = "${req.query.id}"`, function (err, rows) {
             if (err) {
@@ -170,7 +188,7 @@ app.get("/getSingle", function (req, res) {
             res.end(JSON.stringify(rows[0]));
         })
 
-    }else{
+    } else {
         res.contentType("application/json");
         res.header("Access-Control-Allow-Origin", "*");
         res.header("Access-Control-Allow-Headers", "Content-Type");
@@ -178,14 +196,24 @@ app.get("/getSingle", function (req, res) {
 
     }
 })
+var availCache = {}
+cron.schedule('*/1 * * * *', ()=>{
+    availCache = {}
+})
 app_serve.get('/:uuid', function (req, res, next) {
-    async function Provider(err, rows) {
+
+    let uuid = req.params.uuid;
+
+    function Provider(err, rows) {
         try {
+            res.contentType("audio/mp3");
             res.header("Access-Control-Allow-Origin", "*");
+            res.header("Access-Control-Allow-Headers", "Content-Type");
+            res.header("Access-Control-Allow-Origin", "*");
+            res.header('Content-Disposition', `attachment; filename="${encodeURI(rows[0].song_name)}"`);
             /** @type {ArrayBuffer}*/
-            let buffer = rows[0].databinary
-            res.sendSeekable(buffer)
-            db.unprepare()
+            availCache[uuid] = rows[0].databinary
+            res.sendSeekable(availCache[uuid])
 
 
         } catch (e) {
@@ -195,12 +223,30 @@ app_serve.get('/:uuid', function (req, res, next) {
         }
     }
 
-    db.execute(`SELECT databinary
+    if (availCache[uuid] !== undefined) {
+        db.execute(`SELECT song_name
                 FROM instrunet_entry
-                WHERE uuid = '${req.params.uuid}'`, Provider)
-    db.unprepare()
+                WHERE uuid = '${uuid}'`, (err, rows) => {
+            res.contentType("audio/mp3");
+            res.header("Access-Control-Allow-Origin", "*");
+            res.header("Access-Control-Allow-Headers", "Content-Type");
+            res.header("Access-Control-Allow-Origin", "*");
+            res.header('Content-Disposition', `attachment; filename="${encodeURI(rows[0].song_name)}"`);
+            res.sendSeekable(availCache[uuid])
+        })
+
+    }else{
+        db.execute(`SELECT song_name, databinary
+                FROM instrunet_entry
+                WHERE uuid = '${uuid}'`, Provider)
+        db.unprepare()
+    }
+
+
+
 })
 
+// TODO Always use HTTPS before publishing.
 
 https.createServer({
     key: fs.readFileSync('andyxie.cn.key'), cert: fs.readFileSync('andyxie.cn.pem')
@@ -213,5 +259,6 @@ https.createServer({
 
 }, app_serve).listen(8079)
 
+
 // app.listen(8080)
 // app_serve.listen(8079)

+ 45 - 1
package-lock.json

@@ -14,12 +14,14 @@
         "html-entities": "^2.5.2",
         "https": "^1.0.0",
         "mysql2": "^3.11.5",
+        "node-cron": "^3.0.3",
         "node-run-cmd": "^1.0.1",
         "nodemailer": "^6.9.16",
         "send-seekable": "^1.0.4",
         "sqlite3": "^5.1.7",
         "swagger-jsdoc": "^6.2.8",
-        "swagger-ui-express": "^5.0.1"
+        "swagger-ui-express": "^5.0.1",
+        "ws": "^8.18.0"
       }
     },
     "node_modules/@apidevtools/json-schema-ref-parser": {
@@ -1687,6 +1689,18 @@
       "integrity": "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==",
       "license": "MIT"
     },
+    "node_modules/node-cron": {
+      "version": "3.0.3",
+      "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz",
+      "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==",
+      "license": "ISC",
+      "dependencies": {
+        "uuid": "8.3.2"
+      },
+      "engines": {
+        "node": ">=6.0.0"
+      }
+    },
     "node_modules/node-gyp": {
       "version": "8.4.1",
       "resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.4.1.tgz",
@@ -2630,6 +2644,15 @@
         "node": ">= 0.4.0"
       }
     },
+    "node_modules/uuid": {
+      "version": "8.3.2",
+      "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
+      "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
+      "license": "MIT",
+      "bin": {
+        "uuid": "dist/bin/uuid"
+      }
+    },
     "node_modules/validator": {
       "version": "13.12.0",
       "resolved": "https://registry.npmjs.org/validator/-/validator-13.12.0.tgz",
@@ -2680,6 +2703,27 @@
       "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
       "license": "ISC"
     },
+    "node_modules/ws": {
+      "version": "8.18.0",
+      "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz",
+      "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==",
+      "license": "MIT",
+      "engines": {
+        "node": ">=10.0.0"
+      },
+      "peerDependencies": {
+        "bufferutil": "^4.0.1",
+        "utf-8-validate": ">=5.0.2"
+      },
+      "peerDependenciesMeta": {
+        "bufferutil": {
+          "optional": true
+        },
+        "utf-8-validate": {
+          "optional": true
+        }
+      }
+    },
     "node_modules/xtend": {
       "version": "4.0.2",
       "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",

+ 3 - 1
package.json

@@ -15,11 +15,13 @@
     "html-entities": "^2.5.2",
     "https": "^1.0.0",
     "mysql2": "^3.11.5",
+    "node-cron": "^3.0.3",
     "node-run-cmd": "^1.0.1",
     "nodemailer": "^6.9.16",
     "send-seekable": "^1.0.4",
     "sqlite3": "^5.1.7",
     "swagger-jsdoc": "^6.2.8",
-    "swagger-ui-express": "^5.0.1"
+    "swagger-ui-express": "^5.0.1",
+    "ws": "^8.18.0"
   }
 }