AXCWG преди 2 месеца
родител
ревизия
ff35bd11e7
променени са 7 файла, в които са добавени 373 реда и са изтрити 27 реда
  1. 43 16
      README.md
  2. 10 0
      Singletons.js
  3. 202 0
      UserInteractions.js
  4. 19 1
      main.js
  5. 80 1
      package-lock.json
  6. 1 1
      package.json
  7. 18 8
      test.js

+ 43 - 16
README.md

@@ -7,25 +7,52 @@ Not aimed for public. Classified documents contained.
     Specification 1.0 for InstruNet
     
     - DB
-    Column:
-    uuid - Unique ID per entry
-    song_name - Stands for the name of the song of the entry
-    album_name - Stands for the name of the album of the entry
-    link_to - Stands for the metadata information of the entry
-    databinary - Stands for the binary data of the entry
-    artist - Stands for the artist for the entry
-    kind - Describes the kind of the entry.
-    albumcover - Binary data for album cover. 
-    kind (enum) :
-    0 - Full removal of vocal
-    1 - Karaoke--keeps the harmony
-    2 - vocal only 
-    3 - bass
-    4 - drums
-    5 - vocal only (Alternate model)
+        Column:
+            uuid - Unique ID per entry
+            song_name - Stands for the name of the song of the entry
+            album_name - Stands for the name of the album of the entry
+            link_to - Stands for the metadata information of the entry
+            databinary - Stands for the binary data of the entry
+            artist - Stands for the artist for the entry
+            kind - Describes the kind of the entry.
+            albumcover - Binary data for album cover. 
+        kind (enum) :
+        0 - Full removal of vocal
+        1 - Karaoke--keeps the harmony
+        2 - vocal only 
+        3 - bass
+        4 - drums
+        5 - vocal only (Alternate model)
     6 - Guitar
 
 
+## Specification for Playlist
+
+    Specification 0.1 for InstruNet.Playlist System
+    
+    - DB
+    Column:
+    owner - Owner of the playlist
+    uuid - uuid of the playlist
+    content - Content of the playlist -- Songs and shit.
+        Object.
+    
+    private - Enum, whether private or not.
+        0 - Public
+        1 - Private
+    tmb - Thumbnail
+    title: Title of the playlist. 
+
+## Specification for Playlist.content 0.1
+```javascript
+[
+  uuid // uuid of the song
+]
+```
+
+
+
+
 ## TODO 
 
 - [ ] 名称排序

+ 10 - 0
Singletons.js

@@ -0,0 +1,10 @@
+import minimist from "minimist";
+
+let argv = minimist(process.argv.slice(2));
+export default {
+
+        "Access-Control-Allow-Origin": argv.https === true ? "https://andyxie.cn:4000" : "http://localhost:5173",
+        "Access-Control-Allow-Credentials": true,
+        "Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Credentials"
+
+};

+ 202 - 0
UserInteractions.js

@@ -0,0 +1,202 @@
+import bodyParser from "body-parser";
+
+import express from "express";
+
+import session from "express-session";
+
+import cwh from "./Singletons.js";
+
+export default function UserInteractions(opts) {
+    async function sha256(message) {
+        // encode as UTF-8
+        const msgBuffer = new TextEncoder().encode(message);
+
+        // hash the message
+        const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
+
+        // convert ArrayBuffer to Array
+        const hashArray = Array.from(new Uint8Array(hashBuffer));
+
+        // convert bytes to hex string
+        const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
+        return hashHex;
+    }
+
+    let app = opts.app;
+    app.use(session({
+        secret: "rtifhg5878fj",
+        resave: false,
+        saveUninitialized: false,
+        cookie: {
+            sameSite: "lax", secure: "auto"
+        }
+    }))
+    app.use(bodyParser.json({"limit": "200mb"}));
+    app.use(express.json());
+
+    let db = opts.db;
+
+    app.options("/*", (req, res) => {
+
+        res.set(cwh).end("FUCK YOU CORS")
+    })
+    app.get("/delacc", async (req, res) => {
+        if (!req.session.uuid) {
+            res.set(cwh).status(500).json({code: 500, R: "IO"})
+            return
+        }
+        let uuid = req.session.uuid;
+        db.execute("DELETE FROM user WHERE uuid = ?", [uuid], async (err, rows) => {
+            if (err) {
+                res.set(cwh).status(500).json({code: 500, R: "ERR"})
+                req.session.destroy()
+            } else {
+                req.session.destroy()
+                res.set(cwh).status(200).json({code: 200, R: "SUCCESS"})
+
+            }
+
+        })
+    })
+    app.get("/userapi", (req, res) => {
+        if(req.query.getname){
+            db.execute("SELECT username from user where uuid = ?", [req.query.uuid], (err, result) => {
+                res.set(cwh).end(result[0].username);
+
+            })
+        }else{
+            if (!req.session.uuid) {
+                res.set(cwh).status(500).json({code: 500, R: "IO"})
+                return
+            }
+            db.execute("SELECT username, email from user where uuid = ?", [req.session.uuid], (err, result) => {
+                res.set(cwh).end(JSON.stringify({
+                    uuid: req.session.uuid,
+                    username: result[0].username,
+                    email: result[0].email,
+                }));
+
+            })
+        }
+
+    })
+    app.get("/logout", (req, res) => {
+        req.session.destroy()
+        res.set(cwh).status(200)
+    })
+    app.post("/login", async function (req, res) {
+        if (!req.body.username || !req.body.password) {
+            res.set(cwh).status(500).json({code: 500, R: "IO"})
+
+        } else {
+            db.execute("SELECT uuid from user where username = ? and password = ?", [req.body.username, await sha256(req.body.password)], function (err, result) {
+                if (result.length === 0) {
+                    res.set(cwh).status(500).json({code: 500, R: "DNE"})
+                    return;
+                }
+                req.session.uuid = result[0].uuid;
+
+                res.set(cwh).status(200).json({
+                    code: 200,
+                    R: "SS",
+                    uid: result[0].uuid
+                });
+            })
+
+        }
+
+    })
+    app.post("/playlist-owned", async function (req, res) {
+        if (!req.session.uuid) {
+            res.set(cwh).status(500).json({code: 500, R: "AD"})
+            return;
+        }
+        db.execute("SELECT * from playlist where owner = ?", [req.session.uuid], function (err, result) {
+            if (err) {
+                console.log(err);
+                res.set(cwh).status(500).json({code: 500, R: "AD"})
+                return;
+            }
+            res.set(cwh).json(result);
+
+        })
+
+
+    })
+    app.get("/playlist", async function (req, res) {
+
+        db.execute("SELECT * FROM playlist where uuid=?", [req.query.playlistuuid], function (err, result) {
+            if (err) {
+                console.log(err);
+                res.set(cwh).status(500).json({code: 500, R: "AD"})
+                return;
+            }
+            if(result.length === 0) {
+                res.set(cwh).status(404).json({code: 404, R: "DNF"})
+                return;
+            }
+            res.set(cwh).json(result[0]);
+
+        })
+    })
+
+    app.post("/upload-playlist", async function (req, res) {
+        if (!req.session.uuid) {
+            res.set(cwh).status(500).json({code: 500, R: "AD"})
+            return;
+        }
+        if(req.body.playlistuuid){
+            db.execute("SELECT owner FROM playlist WHERE uuid = ?", [req.body.playlistuuid], function (err, result) {
+                if(result[0].owner === req.session.uuid){
+                    db.execute("UPDATE playlist SET content = ?, private = ?, tmb = ?, title = ? WHERE uuid = ?", [req.body.content, req.body.private, req.body.tmb, req.body.title, req.body.playlistuuid], (err, result) => {
+                        if (err) {
+                            console.log(err);
+                            res.set(cwh).status(500).json({code: 500, R: "Err"})
+
+                        }
+                    })
+                }else{
+                    res.set(cwh).status(403).json({code: 403, R: "Forbidden"})
+                }
+            })
+        }else{
+            let key = crypto.randomUUID();
+            db.execute("INSERT INTO playlist(owner, uuid, content, private, tmb, title) values (?,?,?,?,?,?)", [req.session.uuid,key , req.body.content, req.body.private, req.body.tmb, req.body.title], function (err, result) {
+                if (err) {
+                    console.log(err);
+                    res.set(cwh).status(500).json({code: 500, R: "AD"})
+                    return;
+                }
+
+                res.set(cwh).json({
+                    code: 200, R: "SS", UUID:key
+                });
+
+            })
+        }
+
+    })
+
+    app.post("/register", function (req, res) {
+        db.execute("SELECT uuid FROM user WHERE username = ?", [req.body.username], async function (err, rows) {
+            if (!req.body.username || !req.body.password) {
+                res.set(cwh).status(500).json({code: 500, R: "PE"})
+                return;
+            }
+            if (err) {
+                console.log(err);
+                res.set(cwh).status(500).json({code: 500, R: "UNE"});
+                return;
+            }
+            if (rows.length === 0) {
+                db.execute("INSERT INTO user (uuid, username, email, password, avatar, time) values (?,?,?,?,?,?)", [crypto.randomUUID(), req.body.username, !req.body.email ? null : req.body.email, await sha256(req.body.password), null, Date.now()]);
+                res.status(200).set(cwh).json({code: 200, R: "SS"});
+
+                return;
+            }
+            res.set(cwh).status(500).json({code: 500, R: "UE"});
+            return;
+        })
+    })
+}
+

+ 19 - 1
main.js

@@ -45,6 +45,8 @@ const app = express();
 const pool = mysql.createPool({
     keepAliveInitialDelay: 0, enableKeepAlive: true,
 })
+import 'cnchar'
+import 'sort-array'
 
 const Kind = {
     0: "去和声伴奏",
@@ -61,6 +63,9 @@ const {OpenCC} = pkg
 webp.grant_permission()
 let queue = new Queue();
 queue.autoRun = false;
+UserInteractions({
+    app: app, db: db
+})
 const converters2t = new OpenCC('s2t.json')
 const convertert2s = new OpenCC('t2s.json')
 app.use(bodyParser.json({"limit": "200mb"}));
@@ -68,6 +73,7 @@ app.use(express.json());
 app.use(SendSeekable)
 
 
+
 const transporter = nodemailer.createTransport({
     host: 'smtp.qq.com', port: 465, secure: true, auth: {
         user: '3095864740@qq.com', pass: 'caemyuagapsadfff',
@@ -141,7 +147,6 @@ async function Submit(req) {
         currentTask.shift();
         console.log(queue.contents)
         console.log(currentTask)
-        console.log(err)
         queue.next()
         return
     }
@@ -241,7 +246,10 @@ async function Submit(req) {
                 if (err) {
                     console.log(err);
                 }
+
+
             })
+
         } catch (err) {
             console.log(err);
         }
@@ -615,6 +623,15 @@ setInterval(() => {
     })
 }, 10000)
 
+app.get("/avatar", async (req, res) => {
+    if(!req.query.uuid){
+        res.set(cwh).status(500).json({code: 500, R: "IO"})
+        return
+    }
+    db.execute("SELECT avatar from user where uuid = ?", [req.query.uuid], (err, result) => {
+        res.set(cwh).end(result[0].avatar);
+    })
+})
 // Fetch
 app.get('/:uuid', async function (req, res) {
 
@@ -650,6 +667,7 @@ app.get('/:uuid', async function (req, res) {
                 })
 
 
+
             }
 
 

+ 80 - 1
package-lock.json

@@ -16,6 +16,7 @@
         "body-parser": "^1.20.3",
         "cnchar": "^3.2.6",
         "express": "^4.21.1",
+        "express-session": "^1.18.1",
         "fluent-ffmpeg": "^2.1.3",
         "html-entities": "^2.5.2",
         "https": "^1.0.0",
@@ -23,7 +24,6 @@
         "js-queue": "^2.0.2",
         "minimist": "^1.2.8",
         "mysql2": "^3.11.5",
-        "node-fetch": "^2.7.0",
         "node-run-cmd": "^1.0.1",
         "nodemailer": "^6.9.16",
         "opencc": "github:BYVoid/OpenCC",
@@ -972,6 +972,55 @@
         "node": ">= 0.10.0"
       }
     },
+    "node_modules/express-session": {
+      "version": "1.18.1",
+      "resolved": "https://registry.npmjs.org/express-session/-/express-session-1.18.1.tgz",
+      "integrity": "sha512-a5mtTqEaZvBCL9A9aqkrtfz+3SMDhOVUnjafjo+s7A9Txkq+SVX2DLvSp1Zrv4uCXa3lMSK3viWnh9Gg07PBUA==",
+      "license": "MIT",
+      "dependencies": {
+        "cookie": "0.7.2",
+        "cookie-signature": "1.0.7",
+        "debug": "2.6.9",
+        "depd": "~2.0.0",
+        "on-headers": "~1.0.2",
+        "parseurl": "~1.3.3",
+        "safe-buffer": "5.2.1",
+        "uid-safe": "~2.1.5"
+      },
+      "engines": {
+        "node": ">= 0.8.0"
+      }
+    },
+    "node_modules/express-session/node_modules/cookie": {
+      "version": "0.7.2",
+      "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz",
+      "integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==",
+      "license": "MIT",
+      "engines": {
+        "node": ">= 0.6"
+      }
+    },
+    "node_modules/express-session/node_modules/cookie-signature": {
+      "version": "1.0.7",
+      "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.7.tgz",
+      "integrity": "sha512-NXdYc3dLr47pBkpUCHtKSwIOQXLVn8dZEuywboCOJY/osA0wFSLlSawr3KN8qXJEyX66FcONTH8EIlVuK0yyFA==",
+      "license": "MIT"
+    },
+    "node_modules/express-session/node_modules/debug": {
+      "version": "2.6.9",
+      "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
+      "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
+      "license": "MIT",
+      "dependencies": {
+        "ms": "2.0.0"
+      }
+    },
+    "node_modules/express-session/node_modules/ms": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
+      "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==",
+      "license": "MIT"
+    },
     "node_modules/express/node_modules/debug": {
       "version": "2.6.9",
       "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
@@ -2067,6 +2116,15 @@
         "node": ">= 0.8"
       }
     },
+    "node_modules/on-headers": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
+      "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==",
+      "license": "MIT",
+      "engines": {
+        "node": ">= 0.8"
+      }
+    },
     "node_modules/once": {
       "version": "1.4.0",
       "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
@@ -2234,6 +2292,15 @@
         "url": "https://github.com/sponsors/ljharb"
       }
     },
+    "node_modules/random-bytes": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz",
+      "integrity": "sha512-iv7LhNVO047HzYR3InF6pUcUsPQiHTM1Qal51DcGSuZFBil1aBBWG5eHPNek7bvILMaYJ/8RU1e8w1AMdHmLQQ==",
+      "license": "MIT",
+      "engines": {
+        "node": ">= 0.8"
+      }
+    },
     "node_modules/range-parser": {
       "version": "1.2.1",
       "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
@@ -2864,6 +2931,18 @@
         "node": ">=12.17"
       }
     },
+    "node_modules/uid-safe": {
+      "version": "2.1.5",
+      "resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz",
+      "integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==",
+      "license": "MIT",
+      "dependencies": {
+        "random-bytes": "~1.0.0"
+      },
+      "engines": {
+        "node": ">= 0.8"
+      }
+    },
     "node_modules/unique-filename": {
       "version": "1.1.1",
       "resolved": "https://registry.npmjs.org/unique-filename/-/unique-filename-1.1.1.tgz",

+ 1 - 1
package.json

@@ -20,6 +20,7 @@
     "body-parser": "^1.20.3",
     "cnchar": "^3.2.6",
     "express": "^4.21.1",
+    "express-session": "^1.18.1",
     "fluent-ffmpeg": "^2.1.3",
     "html-entities": "^2.5.2",
     "https": "^1.0.0",
@@ -27,7 +28,6 @@
     "js-queue": "^2.0.2",
     "minimist": "^1.2.8",
     "mysql2": "^3.11.5",
-    "node-fetch": "^2.7.0",
     "node-run-cmd": "^1.0.1",
     "nodemailer": "^6.9.16",
     "opencc": "github:BYVoid/OpenCC",

+ 18 - 8
test.js

@@ -1,8 +1,18 @@
-let queue = require('js-queue')
-const req = require("express/lib/request");
-let queue1 = new queue();
-queue1.autoRun = false;
-const cnchar = require('cnchar');
-const sortArray = require("sort-array");
-
-console.log( URL.canParse("data: "))
+
+
+async function sha256(message) {
+    // encode as UTF-8
+    const msgBuffer = new TextEncoder().encode(message);
+
+    // hash the message
+    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
+
+    // convert ArrayBuffer to Array
+    const hashArray = Array.from(new Uint8Array(hashBuffer));
+
+    // convert bytes to hex string
+    return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
+}
+sha256("212121").then((hash) => {
+    console.log(hash);
+})