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,2 +1,2 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });

View File

@ -1,12 +1,31 @@
"use strict"; "use strict";
var _a; var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); return (mod && mod.__esModule) ? mod : { "default": mod };
class PackAPI { };
} var _a;
exports.default = PackAPI; Object.defineProperty(exports, "__esModule", { value: true });
_a = PackAPI; const pack_service_1 = __importDefault(require("./services/pack_service"));
PackAPI.getPacks = async (req, res, next) => { const zip = require("express-zip");
return res.status(200).json({ class PackAPI {
message: "hello all packs", }
}); exports.default = PackAPI;
}; _a = PackAPI;
PackAPI.getPacks = async (req, res, next) => {
return res.status(200).json({
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

@ -1,10 +1,13 @@
"use strict"; "use strict";
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 };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); 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"));
exports.packRoutes = express_1.default.Router(); const pack_upload_1 = __importDefault(require("../pack_upload"));
exports.packRoutes.get("/packs", pack_api_1.default.getPacks); 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

@ -1,45 +1,61 @@
"use strict"; "use strict";
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 };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); 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 packs_1 = require("./routes/packs"); const os_1 = __importDefault(require("os"));
const pack_service_1 = __importDefault(require("./services/pack_service")); const packs_1 = require("./routes/packs");
const router = (0, express_1.default)(); const global_1 = require("./routes/global");
/** Logging */ const pack_service_1 = __importDefault(require("./services/pack_service"));
router.use((0, morgan_1.default)("dev")); const router = (0, express_1.default)();
/** Parse the request */ const fileUpload = require("express-fileupload");
router.use(express_1.default.urlencoded({ extended: false })); /** Logging */
/** Takes care of JSON data */ router.use((0, morgan_1.default)("dev"));
router.use(express_1.default.json()); /** Parse the request */
/** RULES OF OUR API */ router.use(express_1.default.urlencoded({ extended: false }));
router.use((req, res, next) => { /** Takes care of JSON data */
// set the CORS policy router.use(express_1.default.json());
res.header("Access-Control-Allow-Origin", "*"); /** File Upload */
// set the CORS headers router.use(fileUpload({
res.header("Access-Control-Allow-Headers", "origin, X-Requested-With,Content-Type,Accept, Authorization"); useTempFiles: true,
// set the CORS method headers tempFileDir: `${os_1.default.homedir()}/beatloads/tmp`,
if (req.method === "OPTIONS") { createParentPath: true,
res.header("Access-Control-Allow-Methods", "GET POST"); uriDecodeFileNames: true,
return res.status(200).json({}); safeFileNames: true,
} preserveExtension: true,
next(); debug: true,
}); }));
/** Routes */ /** RULES OF OUR API */
router.use("/", packs_1.packRoutes); router.use((req, res, next) => {
/** Error handling */ // set the CORS policy
router.use((req, res, next) => { res.header("Access-Control-Allow-Origin", "*");
const error = new Error("not found"); // set the CORS headers
return res.status(404).json({ res.header("Access-Control-Allow-Headers", "origin, X-Requested-With,Content-Type,Accept, Authorization");
message: error.message, // set the CORS method headers
}); if (req.method === "OPTIONS") {
}); res.header("Access-Control-Allow-Methods", "GET POST");
/** Server */ return res.status(200).json({});
const httpServer = http_1.default.createServer(router); }
const PORT = process.env.PORT ?? 6060; next();
pack_service_1.default.getInitialList().then(() => { });
httpServer.listen(PORT, () => console.log(`The server is running on port ${PORT}`)); /** Routes */
}); router.use("/", packs_1.packRoutes);
router.use("/", global_1.globalRoutes);
/** Error handling */
router.use((req, res, next) => {
const error = new Error("not found");
return res.status(404).json({
message: error.message,
});
});
/** 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"; "use strict";
var __importDefault = (this && this.__importDefault) || function (mod) { var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
return (mod && mod.__esModule) ? mod : { "default": mod }; if (k2 === undefined) k2 = k;
}; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var _a; }) : (function(o, m, k, k2) {
Object.defineProperty(exports, "__esModule", { value: true }); if (k2 === undefined) k2 = k;
const fs_1 = __importDefault(require("fs")); o[k2] = m[k];
const PACK_BASE_PATH = "packs"; }));
class PackService { var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
} Object.defineProperty(o, "default", { enumerable: true, value: v });
exports.default = PackService; }) : function(o, v) {
_a = PackService; o["default"] = v;
PackService.getPackList = () => _a.packList; });
PackService.getInitialList = async () => { var __importStar = (this && this.__importStar) || function (mod) {
const packs = await fs_1.default.readdirSync(PACK_BASE_PATH); if (mod && mod.__esModule) return mod;
packs.forEach((pack) => { }); 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 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 () => {
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

3266
yarn.lock

File diff suppressed because it is too large Load Diff