chore: fix some files

master
ogomez92 2023-06-11 17:18:44 +02:00
parent ccee09189a
commit 9616995616
8 changed files with 6350 additions and 1719 deletions

4
.gitignore vendored
View File

@ -1,3 +1,7 @@
node_modules node_modules
packs/** packs/**
build/** build/**
._*
build

View File

@ -1,12 +1,31 @@
"use strict"; "use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a; var _a;
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const pack_service_1 = __importDefault(require("./services/pack_service"));
const zip = require("express-zip");
class PackAPI { class PackAPI {
} }
exports.default = PackAPI; exports.default = PackAPI;
_a = PackAPI; _a = PackAPI;
PackAPI.getPacks = async (req, res, next) => { PackAPI.getPacks = async (req, res, next) => {
return res.status(200).json({ return res.status(200).json({
message: "hello all packs", packs: JSON.stringify(await pack_service_1.default.getPackList()),
}); });
}; };
PackAPI.downloadSinglePack = async (req, res, next) => {
if (!req.query.name) {
return res.status(500).json({
message: "pack name not specified",
});
}
const foundPack = pack_service_1.default.findByName(req.query.name.toString());
if (!foundPack) {
return res.status(500).json({
message: "Pack does not exist",
});
}
return res.zip(foundPack.files);
};

View File

@ -6,5 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.packRoutes = void 0; exports.packRoutes = void 0;
const express_1 = __importDefault(require("express")); const express_1 = __importDefault(require("express"));
const pack_api_1 = __importDefault(require("../pack_api")); const pack_api_1 = __importDefault(require("../pack_api"));
const pack_upload_1 = __importDefault(require("../pack_upload"));
exports.packRoutes = express_1.default.Router(); exports.packRoutes = express_1.default.Router();
exports.packRoutes.get("/packs", pack_api_1.default.getPacks); exports.packRoutes.get("/packs", pack_api_1.default.getPacks);
exports.packRoutes.get("/getpack", pack_api_1.default.downloadSinglePack);
exports.packRoutes.post("/upload", pack_upload_1.default.uploadPack);

View File

@ -6,15 +6,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
const http_1 = __importDefault(require("http")); const http_1 = __importDefault(require("http"));
const express_1 = __importDefault(require("express")); const express_1 = __importDefault(require("express"));
const morgan_1 = __importDefault(require("morgan")); const morgan_1 = __importDefault(require("morgan"));
const os_1 = __importDefault(require("os"));
const packs_1 = require("./routes/packs"); const packs_1 = require("./routes/packs");
const global_1 = require("./routes/global");
const pack_service_1 = __importDefault(require("./services/pack_service")); const pack_service_1 = __importDefault(require("./services/pack_service"));
const router = (0, express_1.default)(); const router = (0, express_1.default)();
const fileUpload = require("express-fileupload");
/** Logging */ /** Logging */
router.use((0, morgan_1.default)("dev")); router.use((0, morgan_1.default)("dev"));
/** Parse the request */ /** Parse the request */
router.use(express_1.default.urlencoded({ extended: false })); router.use(express_1.default.urlencoded({ extended: false }));
/** Takes care of JSON data */ /** Takes care of JSON data */
router.use(express_1.default.json()); router.use(express_1.default.json());
/** File Upload */
router.use(fileUpload({
useTempFiles: true,
tempFileDir: `${os_1.default.homedir()}/beatloads/tmp`,
createParentPath: true,
uriDecodeFileNames: true,
safeFileNames: true,
preserveExtension: true,
debug: true,
}));
/** RULES OF OUR API */ /** RULES OF OUR API */
router.use((req, res, next) => { router.use((req, res, next) => {
// set the CORS policy // set the CORS policy
@ -30,6 +43,7 @@ router.use((req, res, next) => {
}); });
/** Routes */ /** Routes */
router.use("/", packs_1.packRoutes); router.use("/", packs_1.packRoutes);
router.use("/", global_1.globalRoutes);
/** Error handling */ /** Error handling */
router.use((req, res, next) => { router.use((req, res, next) => {
const error = new Error("not found"); const error = new Error("not found");
@ -40,6 +54,8 @@ router.use((req, res, next) => {
/** Server */ /** Server */
const httpServer = http_1.default.createServer(router); const httpServer = http_1.default.createServer(router);
const PORT = process.env.PORT ?? 6060; const PORT = process.env.PORT ?? 6060;
const start = performance.now();
pack_service_1.default.getInitialList().then(() => { pack_service_1.default.getInitialList().then(() => {
httpServer.listen(PORT, () => console.log(`The server is running on port ${PORT}`)); httpServer.listen(PORT, () => console.log(`The server is running on port ${PORT}`));
console.log("initialized with ", pack_service_1.default.getPackList().length, "packs in ", Math.round(performance.now() - start), "ms");
}); });

View File

@ -1,17 +1,83 @@
"use strict"; "use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) { var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
var _a; var _a;
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs")); const fs_1 = __importDefault(require("fs"));
const PACK_BASE_PATH = "packs"; const fsWalk = __importStar(require("@nodelib/fs.walk"));
const os_1 = __importDefault(require("os"));
const PACK_BASE_PATH = `${os_1.default.homedir()}/beatpacks`;
class PackService { class PackService {
} }
exports.default = PackService; exports.default = PackService;
_a = PackService; _a = PackService;
PackService.packList = [];
PackService.getPackList = () => _a.packList; PackService.getPackList = () => _a.packList;
PackService.getInitialList = async () => { PackService.getInitialList = async () => {
const packs = await fs_1.default.readdirSync(PACK_BASE_PATH); let packs = await fs_1.default.readdirSync(PACK_BASE_PATH, { withFileTypes: true });
packs.forEach((pack) => { }); packs = packs.filter((pack) => pack.isDirectory());
PackService.packList = await Promise.all(packs.map((pack) => PackService.getPackInfo(pack.name)));
};
PackService.getPackInfo = async (packName) => {
let files = [];
const name = packName;
let bpm = "";
let levels = 0;
// Setup walk
const noDots = (entry) => !entry.name.startsWith(".");
const packFiles = await fsWalk.walkSync(`${PACK_BASE_PATH}/${packName}`, {
entryFilter: noDots,
concurrency: 4,
});
const promises = await Promise.all(packFiles.map(async (file) => {
files.push({
path: file.path,
name: file.name,
});
if (file.name === "bpm.txt") {
bpm = await fs_1.default.readFileSync(file.path).toString();
}
if (file.name.includes('music')) {
levels++;
}
}));
if (bpm === "") {
console.log("pack without bpm found", name);
}
const currentPack = {
name: name,
bpm: bpm,
files: files,
levels: levels,
};
return currentPack;
};
PackService.findByName = (name) => {
for (let i = 0; i < PackService.packList.length; i++) {
let pack = PackService.packList[i];
if (pack.name === name) {
return pack;
}
}
return undefined;
}; };

4523
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

482
yarn.lock

File diff suppressed because it is too large Load Diff