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
packs/**
build/**
._*
build

View File

@ -1,12 +1,31 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
const pack_service_1 = __importDefault(require("./services/pack_service"));
const zip = require("express-zip");
class PackAPI {
}
exports.default = PackAPI;
_a = PackAPI;
PackAPI.getPacks = async (req, res, next) => {
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;
const express_1 = __importDefault(require("express"));
const pack_api_1 = __importDefault(require("../pack_api"));
const pack_upload_1 = __importDefault(require("../pack_upload"));
exports.packRoutes = express_1.default.Router();
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 express_1 = __importDefault(require("express"));
const morgan_1 = __importDefault(require("morgan"));
const os_1 = __importDefault(require("os"));
const packs_1 = require("./routes/packs");
const global_1 = require("./routes/global");
const pack_service_1 = __importDefault(require("./services/pack_service"));
const router = (0, express_1.default)();
const fileUpload = require("express-fileupload");
/** Logging */
router.use((0, morgan_1.default)("dev"));
/** Parse the request */
router.use(express_1.default.urlencoded({ extended: false }));
/** Takes care of JSON data */
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 */
router.use((req, res, next) => {
// set the CORS policy
@ -30,6 +43,7 @@ router.use((req, res, next) => {
});
/** Routes */
router.use("/", packs_1.packRoutes);
router.use("/", global_1.globalRoutes);
/** Error handling */
router.use((req, res, next) => {
const error = new Error("not found");
@ -40,6 +54,8 @@ router.use((req, res, next) => {
/** Server */
const httpServer = http_1.default.createServer(router);
const PORT = process.env.PORT ?? 6060;
const start = performance.now();
pack_service_1.default.getInitialList().then(() => {
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";
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) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
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 {
}
exports.default = PackService;
_a = PackService;
PackService.packList = [];
PackService.getPackList = () => _a.packList;
PackService.getInitialList = async () => {
const packs = await fs_1.default.readdirSync(PACK_BASE_PATH);
packs.forEach((pack) => { });
let packs = await fs_1.default.readdirSync(PACK_BASE_PATH, { withFileTypes: true });
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