main.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. import {Worker} from 'worker_threads'
  2. import Minimist from 'minimist'
  3. import fs from 'fs';
  4. import express from 'express';
  5. import mysql from 'mysql2';
  6. import SendSeekable from 'send-seekable'
  7. import 'cnchar'
  8. import 'sort-array'
  9. import nodemailer from 'nodemailer'
  10. import Queue from 'js-queue'
  11. import process from 'node:process'
  12. try {
  13. fs.renameSync("log/latest.log", `log/${new Date().toISOString().replaceAll(":", ".")}.log`)
  14. } catch (e) {
  15. }
  16. // let original = console.log;
  17. // console.log = (str) => {
  18. // if (!fs.existsSync("log")) {
  19. // fs.mkdirSync("log")
  20. //
  21. // }
  22. // fs.appendFileSync("log/latest.log", "[" + new Date().toISOString() + "] " + str.toString() + "\n");
  23. // original(str);
  24. //
  25. // }
  26. import cwh from "./Singletons.js";
  27. import webp from 'webp-converter'
  28. import sortArray from "sort-array";
  29. import pkg from 'opencc'
  30. import https from "node:https";
  31. import nrc from "node-run-cmd";
  32. import bodyParser from "body-parser";
  33. let argv = Minimist(process.argv.slice(2));
  34. const app = express();
  35. const pool = mysql.createPool({
  36. keepAliveInitialDelay: 0, enableKeepAlive: true,
  37. })
  38. import 'cnchar'
  39. import 'sort-array'
  40. import UserInteractions from "./UserInteractions.js";
  41. import {detectBufferMime} from "mime-detect";
  42. const Kind = {
  43. 0: "去和声伴奏",
  44. 1: "和声伴奏",
  45. 2: "人声",
  46. 3: "贝斯",
  47. 4: "鼓",
  48. 5: "其他",
  49. }
  50. const db = mysql.createConnection({
  51. host: 'andyxie.cn',
  52. user: 'external',
  53. password: 'moyingren2015',
  54. database: "instrunet_data",
  55. pool: pool,
  56. enableKeepAlive: true,
  57. keepAliveInitialDelay: 0,
  58. })
  59. const {OpenCC} = pkg
  60. webp.grant_permission()
  61. let queue = new Queue();
  62. queue.autoRun = false;
  63. UserInteractions({
  64. app: app, db: db
  65. })
  66. const converters2t = new OpenCC('s2t.json')
  67. const convertert2s = new OpenCC('t2s.json')
  68. app.use(bodyParser.json({"limit": "200mb"}));
  69. app.use(express.json());
  70. app.use(SendSeekable)
  71. const transporter = nodemailer.createTransport({
  72. host: 'smtp.qq.com', port: 465, secure: true, auth: {
  73. user: '3095864740@qq.com', pass: 'caemyuagapsadfff',
  74. }
  75. })
  76. let ncmAPIUrl = "http://localhost:5999";
  77. let currentTask = [];
  78. async function Submit(req) {
  79. let uuid = crypto.randomUUID()
  80. db.connect(function (err) {
  81. if (err) {
  82. console.log(err)
  83. }
  84. })
  85. let albumCover = null;
  86. try {
  87. let resource = await fetch(req.body.file)
  88. if (!resource.ok) {
  89. GC()
  90. currentTask.shift();
  91. console.log(queue.contents)
  92. console.log(currentTask)
  93. queue.next()
  94. return
  95. }
  96. let ab = await resource.arrayBuffer()
  97. fs.writeFileSync("./" + uuid, Buffer.from(ab))
  98. if (URL.canParse(req.body.albumCover)) {
  99. console.log("can parse ")
  100. fetch(req.body.albumCover).then(res => {
  101. res.arrayBuffer().then(async r => {
  102. if (r.byteLength === 0) {
  103. GC()
  104. currentTask.shift();
  105. console.log(queue.contents)
  106. console.log(currentTask)
  107. queue.next()
  108. return
  109. }
  110. if (!fs.existsSync("tmp")) {
  111. fs.mkdirSync("tmp")
  112. }
  113. fs.writeFileSync("tmp/" + uuid, new Buffer.from(r));
  114. await webp.cwebp("tmp/" + uuid, "tmp/" + uuid + ".webp", "-q 50 -mt ", "-v")
  115. try {
  116. albumCover = fs.readFileSync("tmp/" + uuid + ".webp")
  117. } catch (e) {
  118. console.log(e)
  119. fs.rmSync("tmp/" + uuid);
  120. albumCover = null;
  121. }
  122. })
  123. })
  124. }
  125. } catch (err) {
  126. GC()
  127. currentTask.shift();
  128. console.log(queue.contents)
  129. console.log(currentTask)
  130. queue.next()
  131. return
  132. }
  133. const callback = function (d) {
  134. console.log(d.toString());
  135. }
  136. const errcb = function (d) {
  137. console.log(d.toString());
  138. }
  139. let kind_of = [];
  140. switch (req.body.kind) {
  141. case 0:
  142. kind_of[0] = `audio-separator ./${uuid} --model_filename UVR-MDX-NET-Inst_HQ_4.onnx --mdx_enable_denoise --mdx_segment_size 4000 --mdx_overlap 0.85 --mdx_batch_size 300 --output_format mp3 --output_dir output`
  143. kind_of[1] = `./output/${uuid}_(Instrumental)_UVR-MDX-NET-Inst_HQ_4.mp3`
  144. kind_of[2] = `${uuid}_(Instrumental)_UVR-MDX-NET-Inst_HQ_4.mp3`
  145. kind_of[3] = `./output/${uuid}_(Vocals)_UVR-MDX-NET-Inst_HQ_4.mp3`
  146. break;
  147. case 1:
  148. kind_of[0] = `audio-separator ./${uuid} --model_filename UVR_MDXNET_KARA_2.onnx --mdx_enable_denoise --mdx_segment_size 4000 --mdx_overlap 0.85 --mdx_batch_size 300 --output_format mp3 --output_dir output`
  149. kind_of[1] = `./output/${uuid}_(Instrumental)_UVR_MDXNET_KARA_2.mp3`
  150. kind_of[2] = `${uuid}_(Instrumental)_UVR_MDXNET_KARA_2.mp3`
  151. kind_of[3] = `./output/${uuid}_(Vocals)_UVR_MDXNET_KARA_2.mp3`
  152. break;
  153. case 2:
  154. kind_of[0] = `audio-separator ./${uuid} --model_filename UVR_MDXNET_KARA.onnx --mdx_enable_denoise --mdx_segment_size 4000 --mdx_overlap 0.85 --mdx_batch_size 300 --output_format mp3 --output_dir output`
  155. kind_of[1] = `./output/${uuid}_(Vocals)_UVR_MDXNET_KARA.mp3`
  156. kind_of[2] = `${uuid}_(Vocals)_UVR_MDXNET_KARA.mp3`
  157. kind_of[3] = `./output/${uuid}_(Instrumental)_UVR_MDXNET_KARA.mp3`
  158. break;
  159. case 3:
  160. kind_of[0] = `audio-separator ./${uuid} --model_filename kuielab_a_bass.onnx --mdx_enable_denoise --mdx_segment_size 4000 --mdx_overlap 0.85 --output_format mp3 --mdx_batch_size 300 --output_dir output`
  161. kind_of[1] = `./output/${uuid}_(Bass)_kuielab_a_bass.mp3`
  162. kind_of[2] = `${uuid}_(Bass)_kuielab_a_bass.mp3`
  163. kind_of[3] = `./output/${uuid}_(No Bass)_kuielab_a_bass.mp3`
  164. break;
  165. case 4:
  166. kind_of[0] = `audio-separator ./${uuid} --model_filename kuielab_a_drums.onnx --mdx_enable_denoise --mdx_segment_size 4000 --mdx_overlap 0.85 --output_format mp3 --mdx_batch_size 300 --output_dir output`
  167. kind_of[1] = `./output/${uuid}_(Drums)_kuielab_a_drums.mp3`
  168. kind_of[2] = `${uuid}_(Drums)_kuielab_a_drums.mp3`
  169. kind_of[3] = `./output/${uuid}_(No Drums)_kuielab_a_drums.mp3`
  170. break;
  171. case 5:
  172. kind_of[0] = `audio-separator ./${uuid} --model_filename UVR_MDXNET_Main.onnx --mdx_enable_denoise --mdx_segment_size 4000 --mdx_overlap 0.85 --mdx_batch_size 300 --output_format mp3 --output_dir output`
  173. kind_of[1] = `./output/${uuid}_(Vocals)_UVR_MDXNET_Main.mp3`
  174. kind_of[2] = `${uuid}_(Vocals)_UVR_MDXNET_Main.mp3`
  175. kind_of[3] = `./output/${uuid}_(Instrumental)_UVR_MDXNET_Main.mp3`
  176. break;
  177. case 6:
  178. kind_of[0] = `audio-separator ./${uuid} -m htdemucs_6s.yaml --output_format mp3 --output_dir output --single_stem guitar`
  179. kind_of[1] = `./output/${uuid}_(Guitar)_htdemucs_6s.mp3`
  180. kind_of[2] = `${uuid}_(Guitar)_htdemucs_6s.mp3`
  181. kind_of[3] = `./output/${uuid}_(Others)_UVR_MDXNET_Main.mp3`
  182. break;
  183. }
  184. function GC() {
  185. try {
  186. fs.rm(kind_of[1], (err) => {
  187. if (err) {
  188. console.log(err);
  189. }
  190. })
  191. fs.rm(kind_of[3], (err) => {
  192. if (err) {
  193. console.log(err);
  194. }
  195. })
  196. fs.rm(uuid, (err) => {
  197. if (err) {
  198. console.log(err);
  199. }
  200. })
  201. fs.rm("tmp/" + uuid, (err) => {
  202. if (err) {
  203. console.log(err);
  204. }
  205. })
  206. fs.rm("tmp/" + uuid + ".webp", (err) => {
  207. if (err) {
  208. console.log(err);
  209. }
  210. })
  211. } catch (err) {
  212. console.log(err);
  213. }
  214. }
  215. try {
  216. nrc.run([kind_of[0]], {
  217. onData: callback, onError: errcb
  218. }).then(() => {
  219. try {
  220. db.execute(("INSERT INTO instrunet_entry (uuid, song_name, album_name, link_to, databinary, artist,kind, albumcover, email, epoch) VALUES (?,?,?,?,?,?,?,?,?, ?)"), [uuid, req.body.name, req.body.albumName, req.body.link, fs.readFileSync(kind_of[1]), req.body.artist, req.body.kind, albumCover === null ? new Buffer.from([]) : albumCover, (req.body.email === null || req.body.email === undefined || req.body.email === "") ? "" : req.body.email, Date.now()])
  221. } catch (err) {
  222. GC();
  223. currentTask.shift();
  224. console.log(queue.contents)
  225. console.log(currentTask)
  226. queue.next()
  227. return;
  228. }
  229. GC();
  230. try{
  231. if (req.body.email) {
  232. console.log(req.body.email)
  233. transporter.sendMail({
  234. from: '"xiey0" <xiey0@qq.com>',
  235. to: req.body.email,
  236. subject: "你的音频已处理完成。",
  237. text: "你的音频已处理完成。",
  238. html: fs.readFileSync("./Template.html", "utf8").toString().replace("{song_name}", req.body.name).replace("{href_link}", "https://andyxie.cn:4000/player?play=" + uuid).replace("{kind}", Kind[req.body.kind]).replace("{album}", req.body.albumName)
  239. }).then((result) => {
  240. console.log("Message sent: %s", result.messageId)
  241. })
  242. }
  243. }catch(e){
  244. console.log(e)
  245. }
  246. currentTask.shift();
  247. console.log(queue.contents)
  248. console.log(currentTask)
  249. queue.next()
  250. })
  251. } catch (e) {
  252. GC()
  253. currentTask.shift();
  254. console.log(queue.contents)
  255. console.log(currentTask)
  256. queue.next()
  257. console.error(e)
  258. }
  259. }
  260. app.get('/queue', (req, res) => {
  261. res.header("Access-Control-Allow-Origin", "*");
  262. res.end(JSON.stringify(currentTask));
  263. })
  264. app.post('/submit', SubmitWrapper)
  265. async function SubmitWrapper(req, res) {
  266. // TODO
  267. // May extract logic for dupe check in the future.
  268. if (req.body.file === undefined || req.body.file === null || req.body.file === "" || req.body.kind === undefined || req.body.kind === null || req.body.kind === "") {
  269. res.status(500).header("Access-Control-Allow-Origin", "*").end("想都别想");
  270. return
  271. }
  272. if (req.body.artist === undefined || req.body.albumName === undefined || req.body.artist === null || req.body.albumName === null || req.body.artist === "" || req.body.albumName === "") {
  273. req.body.artist = "未知艺术家"
  274. req.body.albumName = "未知专辑"
  275. }
  276. if (req.body.name === undefined || req.body.name === null || req.body.name === "") {
  277. res.status(500).header("Access-Control-Allow-Origin", "*").end("想都别想");
  278. return
  279. }
  280. if (!URL.canParse(req.body.file)) {
  281. res.status(500).header("Access-Control-Allow-Origin", "*").end("想都别想");
  282. return
  283. }
  284. db.execute(("SELECT uuid, song_name, album_name, artist, kind FROM instrunet_entry WHERE song_name = ? and artist = ? and kind = ?"), [req.body.name, req.body.artist, req.body.kind], async (err, rowsO) => {
  285. db.execute(("SELECT uuid, song_name, album_name, artist, kind FROM instrunet_entry WHERE song_name = ? and artist = ? and kind = ?"), [await converters2t.convertPromise(req.body.name), await converters2t.convertPromise(req.body.artist), req.body.kind], async (err, rowsT) => {
  286. db.execute("SELECT uuid, song_name, album_name, artist, kind FROM instrunet_entry WHERE song_name = ? and artist = ? and kind = ?", [await convertert2s.convertPromise(req.body.name), await convertert2s.convertPromise(req.body.artist), req.body.kind], (err, rowsS) => {
  287. // Feb 1 2025 I give up.
  288. let dedupe = [];
  289. rowsT.forEach(row => {
  290. dedupe = dedupe.concat(row);
  291. })
  292. rowsS.forEach(row => {
  293. if (JSON.stringify(dedupe).indexOf(JSON.stringify(row)) === -1) {
  294. dedupe = dedupe.concat(row);
  295. }
  296. })
  297. rowsO.forEach(row => {
  298. if (JSON.stringify(dedupe).indexOf(JSON.stringify(row)) === -1) {
  299. dedupe = dedupe.concat(row);
  300. }
  301. })
  302. if (dedupe.length === 0) {
  303. // Verify
  304. if (req.body.file.substring(0, 5) !== "data:") {
  305. res.status(500).header("Access-Control-Allow-Origin", "*").end("想都别想");
  306. return
  307. }
  308. for (const item of currentTask) {
  309. if (req.body.name === item.name) {
  310. if (req.body.albumName === item.albumName) {
  311. if (req.body.kind === item.kind) {
  312. res.header("Access-Control-Allow-Origin", "*");
  313. res.statusCode = 500
  314. res.end("和数据库中条目重复")
  315. return
  316. }
  317. }
  318. }
  319. }
  320. for (let prop of [req.body.name, req.body.albumName, req.body.artist]) {
  321. if (prop === undefined || prop === "" || prop === null) {
  322. res.status(500).header("Access-Control-Allow-Origin", "*").end("想都别想");
  323. return
  324. }
  325. }
  326. if (req.body.link === undefined) {
  327. req.body.link = ""
  328. }
  329. queue.add(() => {
  330. Submit(req)
  331. })
  332. currentTask.push({
  333. name: req.body.name,
  334. albumName: req.body.albumName,
  335. kind: req.body.kind,
  336. artist: req.body.artist,
  337. email: req.body.email !== undefined ? req.body.email : null,
  338. })
  339. console.log(queue.contents)
  340. console.log(currentTask)
  341. if (currentTask.length === 1) {
  342. queue.next()
  343. }
  344. res.header("Access-Control-Allow-Origin", "*");
  345. res.end("api_success")
  346. } else {
  347. res.header("Access-Control-Allow-Origin", "*");
  348. res.statusCode = 500
  349. res.end("和数据库中条目重复")
  350. }
  351. })
  352. })
  353. })
  354. }
  355. app.post('/lyric', async (req, res) => {
  356. let name = req.body.name
  357. let artist = req.body.artist;
  358. let album = req.body.albumName;
  359. let lrc = null;
  360. await fetchThatShit()
  361. async function fetchThatShit() {
  362. try {
  363. lrc = (await (await fetch(`http://andyxie.cn:28883/jsonapi?title=${convertert2s.convertSync(name)}&artist=${convertert2s.convertSync(artist)}&album=${convertert2s.convertSync(album)}`)).json())
  364. } catch (err) {
  365. await fetchThatShit()
  366. }
  367. }
  368. res.header("Access-Control-Allow-Origin", "*");
  369. res.end(JSON.stringify(lrc))
  370. })
  371. app.options('/lyric', async (req, res) => {
  372. res.header("Access-Control-Allow-Origin", "*");
  373. res.header("Access-Control-Allow-Headers", "Content-Type");
  374. res.end()
  375. })
  376. app.options('/submit', function (req, res) {
  377. res.header("Access-Control-Allow-Origin", "*");
  378. res.header("Access-Control-Allow-Headers", "Content-Type");
  379. res.end()
  380. })
  381. app.post('/search_api', async function (req, res) {
  382. if (req.body.searchStr !== undefined) {
  383. req.body.searchStr = req.body.searchStr.trim();
  384. db.execute("SELECT uuid, song_name, album_name, artist, kind FROM instrunet_entry WHERE song_name like ? or album_name like ? or artist like ?", [`%${req.body.searchStr}%`, `%${req.body.searchStr}%`, `%${req.body.searchStr}%`], async (err, rowsO) => {
  385. if (err) {
  386. console.log(err)
  387. }
  388. db.execute("SELECT uuid, song_name, album_name, artist, kind FROM instrunet_entry WHERE song_name like ? or album_name like ? or artist like ?", [`%${await converters2t.convertPromise(req.body.searchStr)}%`, `%${await converters2t.convertPromise(req.body.searchStr)}%`, `%${await converters2t.convertPromise(req.body.searchStr)}%`], async (err, rowsT) => {
  389. if (err) {
  390. console.log(err)
  391. }
  392. db.execute("SELECT uuid, song_name, album_name, artist, kind FROM instrunet_entry WHERE song_name like ? or album_name like ? or artist like ?", [`%${await convertert2s.convertPromise(req.body.searchStr)}%`, `%${await convertert2s.convertPromise(req.body.searchStr)}%`, `%${await convertert2s.convertPromise(req.body.searchStr)}%`], (err, rowsS) => {
  393. if (err) {
  394. console.log(err)
  395. }
  396. try {
  397. let prepare = [];
  398. rowsT.forEach(row => {
  399. prepare = prepare.concat(row);
  400. })
  401. rowsS.forEach(row => {
  402. if (JSON.stringify(prepare).indexOf(JSON.stringify(row)) === -1) {
  403. prepare = prepare.concat(row);
  404. }
  405. })
  406. rowsO.forEach((row) => {
  407. if (JSON.stringify(prepare).indexOf(JSON.stringify(row)) === -1) {
  408. prepare = prepare.concat(row);
  409. }
  410. })
  411. for (let obj of prepare) {
  412. obj.stroke = obj.song_name[0].stroke() // Problematic as shit. Do not use.
  413. obj.spell = obj.song_name.spell()
  414. }
  415. sortArray(prepare, {
  416. by: 'spell'
  417. })
  418. res.header("Access-Control-Allow-Origin", "*");
  419. res.json(prepare);
  420. } catch (e) {
  421. console.log(e)
  422. }
  423. });
  424. })
  425. })
  426. }
  427. })
  428. app.options('/search_api', function (req, res) {
  429. res.header("Access-Control-Allow-Origin", "*");
  430. res.header("Access-Control-Allow-Headers", "Content-Type");
  431. res.end()
  432. })
  433. app.get("/getSingle", function (req, res) {
  434. if (req.query.id) {
  435. if (req.query.albumcover === "true") {
  436. db.execute("SELECT albumcover FROM instrunet_entry WHERE uuid = ?", [req.query.id], async function (err, rows) {
  437. if (err) {
  438. console.log(err);
  439. }
  440. res.contentType("application/json");
  441. res.header("Access-Control-Allow-Origin", "*");
  442. res.header("Access-Control-Allow-Headers", "Content-Type");
  443. res.end(JSON.stringify(rows[0]));
  444. })
  445. } else
  446. db.execute("SELECT song_name, album_name, artist, kind FROM instrunet_entry WHERE uuid = ?", [req.query.id], async function (err, rows) {
  447. if (err) {
  448. console.log(err);
  449. }
  450. res.contentType("application/json");
  451. res.header("Access-Control-Allow-Origin", "*");
  452. res.header("Access-Control-Allow-Headers", "Content-Type");
  453. res.end(JSON.stringify(rows[0]));
  454. })
  455. } else {
  456. res.contentType("application/json");
  457. res.header("Access-Control-Allow-Origin", "*");
  458. res.header("Access-Control-Allow-Headers", "Content-Type");
  459. res.end("{}")
  460. }
  461. })
  462. // 163
  463. app.options('/ncm/url', function (req, res) {
  464. res.header("Access-Control-Allow-Origin", "*");
  465. res.header("Access-Control-Allow-Headers", "Content-Type");
  466. res.end()
  467. })
  468. app.post("/ncm/url", function (req, res) {
  469. if (Number.isNaN(Number(req.body.id))) {
  470. res.status(500).header("Access-Control-Allow-Origin", "*").end("格式错误");
  471. return
  472. }
  473. if (req.body.id && (req.body.kind || req.body.kind === 0)) {
  474. try {
  475. let id = req.body.id
  476. fetch(ncmAPIUrl + "/song/download/url/v1?id=" + id + "&level=hires", {
  477. headers: {
  478. Cookie: "MUSIC_U=0087F9D8E102A1C1661EBE1792412F3351DA64D1BD3D862BA77E45E9024524725F3A1983345D9B5A4014C725D19C069DD71081F6FE3659F9E1FD412DC427FB809FAF7789AEEA10E9DE6F06C58D1959BA209D2A83C3FA753261036C4CFD0D143B6C7748B8A6D2DD5C2E96E75D1E847E4AAE035CB2C86B175D9AFC6A164C522ED76E24AE654740AB6BAF5B29597F7E3B0158B2EC1C37F2688279871873FA7ADAEF8280A059E84C4BBFB9E4F225F9A2065DF652247D5496587A7B1E3D35DB0CD3F825C06FE5BFE5CFEF1770847099704360504B73C9B396E37CECE4F9DDEE6001588C3C4F5B2861D9ADF339FC47DD480858CA800620785EA032215B63B81025304DB3331F384793FF8EE681247E34C7931176F2F618B66C122F0602F1EA15F963E422DEC79C257F3577A197BECE71E316C751C3B9F5F3CD07BFDC0270A287A1BB6576"
  479. }
  480. }).then(async result => {
  481. if (result.status === 200 || result.status === 304) {
  482. let result_json = null;
  483. try {
  484. result_json = await result.json()
  485. } catch (e) {
  486. res.status(500).header("Access-Control-Allow-Origin", "*").end("格式错误");
  487. return
  488. }
  489. if (result_json.data.url !== null) {
  490. let infos = await (await fetch(ncmAPIUrl + "/song/detail?ids=" + id)).json();
  491. req.body.file = "data:audio/flac;base64," + Buffer.from(await (await fetch(result_json.data.url)).arrayBuffer()).toString("base64")
  492. req.body.name = infos.songs[0].name;
  493. req.body.albumName = infos.songs[0].al.name
  494. req.body.albumCover = infos.songs[0].al.picUrl
  495. req.body.link = result_json.data.url
  496. req.body.artist = infos.songs[0].ar[0].name
  497. /// Complete
  498. SubmitWrapper(req, res)
  499. } else {
  500. res.status(500).header("Access-Control-Allow-Origin", "*").end("不存在");
  501. }
  502. } else {
  503. res.status(404).header("Access-Control-Allow-Origin", "*").send("未找到或出现错误")
  504. }
  505. })
  506. } catch (err) {
  507. res.status(500).header("Access-Control-Allow-Origin", "*");
  508. console.log(err)
  509. }
  510. } else {
  511. res.contentType("application/json");
  512. res.header("Access-Control-Allow-Origin", "*");
  513. res.header("Access-Control-Allow-Headers", "Content-Type");
  514. res.end("傻逼。")
  515. }
  516. })
  517. app.get("/favicon.ico", function (req, res) {
  518. res.end("");
  519. })
  520. let availCache = {};
  521. setInterval(() => {
  522. db.execute("SELECT 1", (err, result) => {
  523. })
  524. }, 10000)
  525. app.get("/avatar", async (req, res) => {
  526. if (!req.query.uuid) {
  527. res.set(cwh).status(500).json({code: 500, R: "IO"})
  528. return
  529. }
  530. db.execute("SELECT avatar from user where uuid = ?", [req.query.uuid], (err, result) => {
  531. res.set(cwh).end(result[0].avatar);
  532. })
  533. })
  534. app.post("/avatar", async (req, res) => {
  535. if (!req.session.uuid) {
  536. res.set(cwh).status(500).json({code: 500, R: "IO"})
  537. return
  538. }
  539. db.execute("UPDATE user set avatar = ? where uuid = ?", [Buffer.from(Object.values(req.body.avatar)), req.session.uuid], (err, result) => {
  540. if (err) {
  541. console.log(err)
  542. res.set(cwh).status(500).json({code: 500, R: "UE"})
  543. return
  544. }
  545. res.set(cwh).status(200).json({code: 200, R: "SS"})
  546. })
  547. })
  548. // Fetch
  549. app.get('/:uuid', async function (req, res) {
  550. let uuid = req.params.uuid;
  551. let pitch = req.query.pitch;
  552. async function Provider(b) {
  553. try {
  554. /** @type {ArrayBuffer}*/
  555. if (!pitch) {
  556. res.contentType("audio/mp3");
  557. res.header("Access-Control-Allow-Origin", "*");
  558. res.header("Access-Control-Allow-Headers", "Content-Type");
  559. res.header('Content-Disposition', `attachment; filename="${encodeURI(availCache[uuid].song_name)}.mp3"`);
  560. res.sendSeekable(availCache[uuid].databinary)
  561. } else {
  562. const worker = new Worker('./pitched.js', {
  563. workerData: {
  564. binary: availCache[uuid].databinary, pitch: pitch
  565. }
  566. })
  567. worker.on("message", message => {
  568. res.contentType("audio/wav");
  569. res.header("Access-Control-Allow-Origin", "*");
  570. res.header("Access-Control-Allow-Headers", "Content-Type");
  571. res.header("Access-Control-Allow-Origin", "*");
  572. res.sendSeekable(Buffer.from(message))
  573. })
  574. }
  575. } catch (e) {
  576. res.contentType("text/plain");
  577. res.status(500).header("Access-Control-Allow-Origin", "*").end("Err.");
  578. console.log(e)
  579. console.log("Triggered err");
  580. }
  581. }
  582. if (availCache[uuid]) {
  583. await Provider();
  584. } else {
  585. db.execute("SELECT song_name, databinary FROM instrunet_entry WHERE uuid = ?", [uuid], (err, rows) => {
  586. availCache[uuid] = rows[0]
  587. Provider()
  588. })
  589. }
  590. })
  591. if (argv.https === "true") {
  592. https.createServer({
  593. key: fs.readFileSync('andyxie.cn.key'), cert: fs.readFileSync('andyxie.cn.pem')
  594. }, app).listen(8080)
  595. console.log("Listening on port 8080 with TLS")
  596. } else {
  597. app.listen(8080)
  598. console.log("Listening on port 8080")
  599. }